चूंकि स्थिरांक subroutines हैं और आप उन्हें बिट को पहले से ही मौत के रूप में बुलाकर विरासत प्राप्त कर सकते हैं, यहां चीजों पर एक अलग स्पिन है।
क्या आप जानते हैं आप केवल एक ही फाइल में काम कर रहे हैं, तो आप पुल संकुल को शाब्दिक स्थिरांक का उपयोग कर सकते हैं:
package Parent;
our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL);
*NO_LEVEL = \0; # this split declaration installs aliases to numbers
*MY_LEVEL = \1; # into the lexicals. since numbers are constants
*YOUR_LEVEL = \2; # to perl, the aliased names are also constants
package Child;
# just to demonstrate that anything below can access the constants
sub printAll {
print "$NO_LEVEL $MY_LEVEL $YOUR_LEVEL\n";
}
Child->printAll; # 0 1 2
eval {$NO_LEVEL = 3} or print "error: [email protected]\n";
# error: Modification of a read-only value attempted at ...
आप पर्ल की जरूरत नहीं है जब लगातार करने के लिए बताए मरने के लिए, our
घोषणा थोड़ा आसान हो जाता है (और हो सकता है एक my
):
our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL) = (0, 1, 2);
, जबकि अभी भी एक छोटे से जादू के साथ संक्षिप्त सिंटैक्स का उपयोग आप निरंतर प्रकृति वापस ला सकता है:
0,123,
my $constant = sub {Internals::SvREADONLY($_[$_], 1) for 0 .. $#_};
package Parent;
$constant->(our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL) = (0, 1, 2));
package Child;
# just to demonstrate that anything below can access the constants
sub printAll {
print "$NO_LEVEL $MY_LEVEL $YOUR_LEVEL\n"; # interpolates :)
}
Child->printAll; # 0 1 2
eval {$NO_LEVEL = 3} or print "error: [email protected]\n";
# error: Modification of a read-only value attempted at ...
आप निश्चित रूप से $constant
coderef छोड़ देते हैं और जादू इनलाइन कर सकते हैं:
package Parent;
Internals::SvREADONLY($_, 1)
for our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL) = (0, 1, 2);
+1 होने के लिए +1। इससे वास्तव में मुझे स्थिति को समझने में मदद मिली। धन्यवाद एरिक! – qodeninja