My first idea was using lvalue subs, but it didn't work in the debugger REPL so I thought this might not work ... ( bug?¹ )

Trying to find out why revealed that it works pretty well from a source file.

script:

use strict; use warnings; use Data::Dump; $\="\n"; our $x='X'; our $y='Y'; our (@x,@y,@z); # return lvalue glob sub lg :lvalue { *x }; print '--- make $x alias of $y' ; lg=\$y; dd \*x; print '$x= ',"'$x'"; $y='YY'; print '$x= ',"'$x'"; print '--- make *x alias of *z' ; lg=*z; dd \*x; our $z='Z'; print '$x= ',"'$x'"; @z=qw(Z ZZ ZZZ); print '@x= ',"qw/@x/";

output:

--- make $x alias of $y do { my $a = \*main::x; *{$a} = \"Y"; *{$a} = []; $a; } $x= 'Y' $x= 'YY' --- make *x alias of *z do { my $a = \*main::z; *{$a} = []; $a; } $x= 'Z' @x= qw/Z ZZ ZZZ/

UPDATE

output from Data::Dump seems correct... globs are just confusing

UPDATE

changed dd *a to dd \*a to make output of Data::Dump less confusing

UPDATE

²) not a bug, just a problem with my debugger settings!

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)


In reply to Re: Returning and using a glob from a sub. (lvalue sub) by LanX
in thread Returning and using a glob from a sub. by mephtu

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.