Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i found this in a CPAN module :

{ my $singleton = bless \my($x), __PACKAGE__; sub new { return $singleton } sub AUTOLOAD { return $singleton } sub can { return sub { return $singleton } } }

what is the first stmt doing? its' a blessing of something to the current package.

but the $x is NOT declared anywhere in the package. and i don't understand what a reference of the my keyword does?

using Data::Dumper, i get this :

bless ( do{ \( my $o = undef ) }, 'package_name' )

feel free to point me to any documentation. thanks

Replies are listed 'Best First'.
Re: syntax question
by moritz (Cardinal) on Sep 21, 2010 at 07:33 UTC
    but the $x is NOT declared anywhere in the package. and i don't understand what a reference of the my keyword does?

    $x is declared there where it's used. \my ($x) is short for something like (my $x; \$x;)

    So it returns a reference to $x, not to my.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: syntax question
by JavaFan (Canon) on Sep 21, 2010 at 07:34 UTC
    It's creating a lexical variable $x, and taking a reference of it. Now the name disappears when exiting the block, but since $singleton holds a reference, the value (which in this case is undefined) can still be gotten.

    Note that all you need to create an object is a reference. You don't need a reference to a "large" structure like a hash or an array. Any reference will do - including one to a scalar.

Re: syntax question
by GrandFather (Saint) on Sep 21, 2010 at 07:48 UTC

    \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
      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 $
Re: syntax question
by locked_user sundialsvc4 (Abbot) on Sep 21, 2010 at 12:57 UTC

    The “voodoo” here is that new, AUTOLOAD, and can, all become closures.  

    All of these declarations, including $x, $singleton, and the three subs, all occur within a block.   The code executes, defining $x and then $singleton and initializing them both.   The subs are defined as returning them.   Since the subs will survive the block, and they all now have references to $singleton, each one of them will return a reference to the same object.   Once the block ends, the two variables go out-of-scope and thus can no longer be referenced ... except by using the subs provided.   Because their reference counts are not zero, and will never become so, they’re like that line from the song, The Highwaymen:   “and yet, I’m living still ...”

    Voodoo, indeed.

Re: syntax question
by Anonymous Monk on Sep 21, 2010 at 07:28 UTC
    print \my($x),"\n"; print \my($x),"\n"; print \my($x),"\n"; __END__ SCALAR(0x3e8b34) SCALAR(0x98a414) SCALAR(0x98a444)
Re: syntax question
by jakeease (Friar) on Sep 21, 2010 at 18:08 UTC

    The bless operator turns the variable pointed to by the reference into an "instance variable" for an object, thus creating an instance of a class, to use the parlance of Java or C++. __PACKAGE__ is a special variable that contains the current package name, so the singleton is an instance of the current package. The idea of a Singleton class is that there should be only one instance of it.

    As merlyn has pointed out elsewhere, you don't really need to create an instance at all:

    http://www.perlmonks.org/?node_id=183417