The construct [ @list ] creates a new array reference, into which @list is expanded (i.e. copied), so that's bloat.

Are there any potentially adverse consequences of holding on to references to static variables?

No, since the my @list isn't static. That container will be empty at each subroutine invocation, and it's contents will live in a brand new reference which you can take with \@list...

use strict; use warnings; sub foo { my @list if 0; print "list: (@list)\n"; push @list, @_; \@list; } my $ref1 = foo(1..3); push @$ref1, 'a', 'b'; my $ref2 = foo(4..6); print "first ref: (@$ref1)\n"; print "last list: (@$ref2)\n"; __END__ list: () list: () first ref: (1 2 3 a b) final list: (4 5 6)

...except if you play tricks to the compiler (allocate a private @list on the scratchpad, but don't generate the opcode to clear that list) like so:

use strict; use warnings; sub foo { my @list if 0; print "list: (@list)\n"; push @list, @_; \@list; } my $ref1 = foo(1..3); push @$ref1, 'a', 'b'; my $ref2 = foo(4..6); print "first ref: (@$ref1)\n"; print "final list: (@$ref2)\n"; __END__ list: () list: (1 2 3 a b) first ref: (1 2 3 a b 4 5 6) last list: (1 2 3 a b 4 5 6)

But that's deprecated. For statics (previous perl 5.10) you'd use a block around the sub

use strict; use warnings; { my @list; sub foo { print "list: (@list)\n"; push @list, @_; \@list; } } my $ref1 = foo(1..3); push @$ref1, 'a', 'b'; my $ref2 = foo(4..6); print "first ref: (@$ref1)\n"; print "final list: (@$ref2)\n";

which produces the same output like the infamous 'my if 0' hack above, but you get at a glance what is going on: the @list is allocated in the same scope into which sub foo has been compiled, but @list isn't re-initialized inside the sub.


In reply to Re: returning ref to static var vs. copying by shmem
in thread returning ref to static var vs. copying by perl5ever

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.