Ponieważ stałe są podprogramami i można uzyskać dziedziczenie, nazywając je metodami bit został już pokryty śmiercią, tutaj jest inny spin na rzeczy.
Jeśli wiesz, że pracują tylko w jednym pliku, można używać stałych leksykalnych do pakietów mostowych:
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 ...
Jeśli nie trzeba Perl umrzeć podczas przypisywania do stałej, deklaracja our
dostaje nieco prostsze (i może być my
):
our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL) = (0, 1, 2);
można przywrócić stały charakter, a jednocześnie przy użyciu składni lakoniczny z odrobiną magii:
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 ...
Można oczywiście pominąć $constant
coderef i inline magię:
package Parent;
Internals::SvREADONLY($_, 1)
for our ($NO_LEVEL, $MY_LEVEL, $YOUR_LEVEL) = (0, 1, 2);
+1 za kompletną odpowiedź. To naprawdę pomogło mi zrozumieć sytuację tutaj. Dziękuję Eric! – qodeninja