in reply to syntax question

\my($x) returns a reference to the $x declared by the my. The brackets are required around $x to avoid $x, __PACKAGE__ being treated as a list of variables associated with the my. In fact the effect is the same if the ( is moved to the left of the my.

So, $x is declared, albeit in a slightly disguised fashion. And the \ doesn't act on the my, but on the newly defined $x 'returned' by the my.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: syntax question
by JavaFan (Canon) on Sep 21, 2010 at 09:46 UTC
    Actually, the parens aren't required at all:
    $ perl -MO=Deparse { package With; my $singleton = bless \my($x), __PACKAGE__; sub new { return $singleton } sub AUTOLOAD { return $singleton } sub can { return sub { return $singleton } } } { package WithOut; my $singleton = bless \my $x , __PACKAGE__; sub new { return $singleton } sub AUTOLOAD { return $singleton } sub can { return sub { return $singleton } } } ^D { package With; my $singleton = bless(\my($x), 'With'); sub new { return $singleton; } sub AUTOLOAD { return $singleton; } sub can { return sub { return $singleton; } ; } ; } { package WithOut; my $singleton = bless(\my($x), 'WithOut'); sub new { return $singleton; } sub AUTOLOAD { return $singleton; } sub can { return sub { return $singleton; } ; } ; } - syntax OK $