What happens in my declaration?

I know the name sounds funny, but somewhere I found this snippet:

my MyPackage $p1 = MyPackage->new;

What role does the package name play before $p1?

EDIT: I am running perl 5.10.1.

+5
source share
3 answers

From http://perldoc.perl.org/functions/my.html :

my TYPE EXPR: ATTRS

A mine declares the listed variables local (lexically) to the enclosing block, file, or eval. If more than one value is specified, the list should be placed in parentheses.

TYPE ATTRS . TYPE , pragma Perl 5.8.0 Attribute:: Handlers.

+6

fields pragma, .

:

package MyPackage;
use fields qw/ foo bar /;
sub new { fields::new(shift) }

,

use MyPackage;
my MyPackage $p1 = MyPackage->new;
print $p1->{notpresent}, "\n";

No such class field "notpresent" in variable $p1 of type MyPackage at ...
+11

In addition to using margins, the lexical type is used by experimental pragma types (available from CPAN).

0
source

All Articles