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.


In reply to Re: Anonymous Thingies by hanenkamp
in thread Anonymous Thingies by Sprad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.