Why would you want such a thing?

Probably the simpest example I can think of is when you "need" to pass more than one array or hash (or one of each) to a function. Then you need to pass a reference, and sometimes you are either getting the array from a function or want to hard code it. So you have...

# Passing array refs from function returns... foo([split(',', $a)], [split('/', $b)]); # Passing hard coded data (used in CGI etc.) bar([1..4], { 1 => "foo", 2 => "bar", 3 => "abcd", 4 => "xyz"});
What's the scope on an anonymous thingie?

Probably the easiest thing to do is think of them as named variables, but the names are not in scope, so...

# "anonymous" reference to array of 8 my $foo = [1..8]; ## Is the same as... my $foo = undef; # New scope... { # Array of 8 my @foo = (1..8); # Create reference to array of 8 $foo = \@foo; }

In both cases doing a $foo = undef; will get rid of the last reference to the array.

-- James Antill

In reply to Re: Anonymous Thingies by nevyn
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.