Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: qw with anonomous list ref

by fruiture (Curate)
on Oct 28, 2002 at 22:56 UTC ( #208632=note: print w/replies, xml ) Need Help??

in reply to qw with anonomous list ref

None is better, because they do different things! qw// is basically a compile-time construct that (as Deparse shows) returns a LIST. \ LIST is a special form of reference-list constrctor and returns a LIST where each element is a reference to the element of the original list:

my @x = \qw(a b c); print "$_ $$_ \n" for @x;

[] creates an anonymous ARRAY-ref, which turns out to be a scalar value in the end:

my @x = [qw(a b c)]; #@x has one element print "$_ @$_\n" for @x;

So when you evaluate \LIST in scalar context, you get what you alway get when forcing a list to scalar context: the last element:

my $x = \qw(a b c); print "$x $$x\n";



Replies are listed 'Best First'.
Re: Re: qw with anonomous list ref
by John M. Dlugosz (Monsignor) on Oct 29, 2002 at 15:14 UTC
    I didn't realize that \LIST went "hyper" and applied the ref element-wise. Thanks.
Re: Re: qw with anonomous list ref
by Jenda (Abbot) on Oct 29, 2002 at 21:15 UTC

    Does anyone have any idea what would this be good for? I can't imagine any use for \(list).

    The only thing that comes to mind is

    $ref = \($hashref->{key}->{subkey});
    In this case the
    $ref = \{$hashref->{key}->{subkey}};
    would mean something totaly different ($ref would be a reference to an anonymous hash) and
    $ref = \$hashref->{key}->{subkey};
    is not very clear.

    So are there any other uses?


      I have used it once for setting up an AoA structure.

      I filled 3 seperate arrays from reading and processing 3 different files and then combined them

      my @AoA = \(@a, @b, @c);

      Slightly easier than initialising (or pushing) 3 anon arrays and then filling them with push @{$AoA[n]}, $stuff; or whatever.

      Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
      Most often I use this to do something to several arrays at once:
      my %silly_example; for(\(@couple, @of, @arrays, @here)) { $silly_example{shift @$_}++; splice @$_, 7, 1; push @$_, shift @$_; }
      Occasionally it comes it handy for a similar use of map or some such.

      Makeshifts last the longest.

      If you're using DBI, then code like this:   $st->bind_columns(undef, \$a, \$b, \$c)
      can be written as   $st->bind_columns(undef, \($a, $b, $c))
      That may be slightly easier to maintain.

      However, two caveats:

      • The \(LIST) form is confusing to many who read it, so make sure you are very consistent.
      • Most code I've seen use bind_columns() is less readable than fetch_hashref(), so if you're thinking of changing your coding style, think about dropping bind_columns() altogether.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://208632]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-02-18 06:07 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (250 votes). Check out past polls.