Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Anonymous Thingies

by hanenkamp (Pilgrim)
on Oct 21, 2003 at 20:58 UTC ( [id://301065]=note: print w/replies, xml ) Need Help??


in reply to Anonymous Thingies

Why would you want such a thing?

Frequently. Since Perl flattens lists:

@x = ( (1, 2, 3), ('a', 'b', 'c') );

is equivalent to:

@x = (1, 2, 3, 'a', 'b', 'c');

Anonymous hashes/arrays are essentially used prevent this from being done.

What's the scope on an anonymous thingie?

To quote the Camel Book:

Anonymous: Used to describe a referent that is not directly accessible through a named variable. Such a referent must be indirectly accessible through at least one hard reference. When the last hard reference goes away, the anonymous referent is destroyed without pity.

That is, it goes away immediately if it isn't assigned to be referenced by something.

Will they ever die/get lost/get clobbered?

Perl 5 uses reference counting for garbage collection. As soon as the reference count on the reference goes to zero, it will be "destroyed without pity." Therefore, the statement:

[ qw(I am not assigned to anything.) ];

Get's canned immediately, while:

$var_a = $var_b = $var_c = [ qw(I am referenced thrice!) ];

lasts until all three variables go out of scope or are assigned different values or, ultimately, when the interpreter exits if one of these is global or never goes out of scope and is never reassigned.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://301065]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-18 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found