Như đã nêu trong perlmodlib, bạn nên bắt đầu tên mô-đun bằng chữ hoa:
Perl informally reserves lowercase module names for 'pragma' modules like integer
and strict
. Other modules normally begin with a capital letter and use mixed case with no underscores (need to be short and portable).
Một cách để gọi một phụ quy định tại gói khác là để đủ điều kiện đầy đủ tên của sub rằng khi bạn gọi nó là:
SettingsGeneral::printScreen "important message\n";
Nếu tất cả các bạn muốn là một tham chiếu đến printScreen
, lấy nó với dấu chéo ngược điều hành
my $subref = \&SettingsGeneral::printScreen;
và gọi nó với một trong
&$subref("one\n");
&{$subref}("two\n");
$subref->("three\n");
Bạn có thể tạo một alias trong gói hiện tại của bạn:
*printScreen = \&SettingsGeneral::printScreen;
printScreen("another urgent flash\n");
Bỏ qua dấu ngoặc đơn (cần thiết vì phụ trong gói hiện tại đã không được biết đến tại thời gian biên dịch) bằng cách viết:
use subs 'printScreen';
*printScreen = \&SettingsGeneral::printScreen;
printScreen "the sky is falling!\n";
Các Exporter mô-đun có thể làm việc giam giữ này dành cho bạn:
SettingsGeneral.pm:
package SettingsGeneral;
use Exporter 'import';
our @EXPORT = qw/ printScreen /;
sub printScreen {
print $_[0];
}
1;
chính:
#! /usr/bin/perl
use warnings;
use strict;
use SettingsGeneral;
printScreen "foo!\n";
Nguồn
2010-04-06 13:53:58
trong ví dụ đầu tiên của bạn, 'PrintScreen ("khác urgen đèn flash \ n");' cần Parens –
thx rất nhiều! đã học được điều gì đó;) – Pmarcoen
@Eric Cảm ơn! Đã sửa. –