Bạn có thể đặt này ở đầu Constants.pm
:
package main;
Trong trường hợp này tất cả các biến bạn xác định sẽ nằm trong main
namespace:
$main::x
hoặc nếu bạn đang cảm thấy dũng cảm :
package;
Trong trường hợp này tất cả các biến bạn xác định bệnh được trong một namespace trống:
$::x
Lưu ý rằng việc sử dụng package
với không gian tên không được khuyến khích, và dường như sẽ hết hạn vào một số phiên bản của Perl. Xem báo giá bên dưới.
Trích dẫn từ man perlfunc
:
package NAMESPACE
package Declares the compilation unit as being in the given
namespace. The scope of the package declaration is
from the declaration itself through the end of the
enclosing block, file, or eval (the same as the "my"
operator). All further unqualified dynamic identifiers
will be in this namespace. A package statement affects
only dynamic variables--including those you've used
"local" on--but not lexical variables, which are cre?
ated with "my". Typically it would be the first decla?
ration in a file to be included by the "require" or
"use" operator. You can switch into a package in more
than one place; it merely influences which symbol table
is used by the compiler for the rest of that block.
You can refer to variables and filehandles in other
packages by prefixing the identifier with the package
name and a double colon: $Package::Variable. If the
package name is null, the "main" package as assumed.
That is, $::sail is equivalent to $main::sail (as well
as to $main'sail, still seen in older code).
If NAMESPACE is omitted, then there is no current pack?
age, and all identifiers must be fully qualified or
lexicals. However, you are strongly advised not to
make use of this feature. Its use can cause unexpected
behaviour, even crashing some versions of Perl. It is
deprecated, and will be removed from a future release.
Edit: Câu hỏi này có thể hữu ích cũng như: How do I use constants from a Perl module?
Nguồn
2009-02-08 11:31:25
Cảm ơn. Những gì tôi thực sự cần là phần về bỏ "chính". – Lev
Tôi tò mò nếu nó thực sự hoạt động. Nếu có, tôi sẽ xóa tuyên bố từ chối trách nhiệm ở đầu câu trả lời. –
Điều này đặt các hằng số trong chính ::, nhưng bây giờ bạn phải tiền tố tất cả các hằng số với chính :: –