1 2 3 4 5 6 7 8 9 10 11 12
use strict; use warnings; use 5.010; use utf8; package A; our $test = 42; say ${A::test}; 1; say $test;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/usr/bin/perl use strict; use warnings; use 5.020; package A; my $test = 42; say $test; 1; package main; say $test;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#! /usr/bin/perl use strict; use warnings; use 5.020; { # Beware: Package enclosed in Block package A; my $test = 42; say $test; 1; } package main; say $test;
1 2 3 4 5 6 7 8
use strict; use 5.010; package A { our $test = 42; say ${A::test}; 1; } say $test;
1 2 3 4 5 6
sub AUTOLOAD{ my $self = shift; my $fname = do{ our $AUTOLOAD =~ /(\w+)$/; $1; };
perldoc varsNOTE: For use with variables in the current package for a single scope, the functionality provided by this pragma has been superseded by our declarations, available in Perl v5.6.0 or later, and use of this pragma is discouraged. See our.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
use warnings; use strict; use 5.010; package A; our $test = "Alpha"; package B; our $test = "Beta"; package main; say '$A::test = ', $A::test; # Alpha say '$B::test = ', $B::test; # Beta say '$test = ', $test; # Beta say '$main::test = ', $main::test; # Uninitiaized
2018-07-02T11:15:06 hajLazyness. Um was schneller zu schrieben.Nun braucht man ja our eher selten, und es ist auch nur einer der Fallstricke, wenn man mehrere package Deklarationen in einer Datei hat.
Man sollte wirklich einen triftigen Grund haben, von der Konvention abzuweichen.
QuoteJa, in dem Fall auch.Das Verhalten von our halte ich auch für sehr ... gewöhnungsbedürftig, um es schonend zu sagen
QuoteSagte Gabor in https://perlmaven.com/package-variables-and-lexica...In the next example we don't have curly braces and thus the declaration our $x = 23; will be intact even after switching namespaces. This can lead to very unpleasant situations. My recommendation is to avoid using our (you almost always need to use my anyway) and to put every package in its own file.
1 2 3 4 5 6 7 8 9
package My::Foo::Bar::Helper::Module; use strict; use warnings; # type long name at least once $My::Foo::Bar::Helper::Module::OPTION1 = 0; $My::Foo::Bar::Helper::Module::OPTION2 = 0; # use long names later on in this package/file
1 2 3 4 5 6 7 8
package My::Foo::Bar::Helper::Module; use strict; use warnings; our $OPTION1 = 0; our $OPTION2 = 0; # use short OPTION1/2 later on in this package/file
1 2
use My::Foo::Bar::Helper::Module; $My::Foo::Bar::Helper::Module::OPTION1 = 1;