Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

qw with anonomous list ref

by John M. Dlugosz (Monsignor)
on Oct 28, 2002 at 22:36 UTC ( #208626=perlquestion: print w/ replies, xml ) Need Help??
John M. Dlugosz has asked for the wisdom of the Perl Monks concerning the following question:

Which is better:
[qw(a b c)] \qw(a b c)
I was surprised to see that, with Deparse, they produced different internal parses, and that the bottom one did what I thought the top one would, and the top one seemed optimized or more direct.

What is the real distinction in the internal representations, and is it meaningful for anything?

Comment on qw with anonomous list ref
Download Code
Re: qw with anonomous list ref
by Ovid (Cardinal) on Oct 28, 2002 at 22:54 UTC

    The bottom one creates a list of references, not a reference to a list.

    C:\>perl -e "($x,$y)=\qw(a b);print $$y" b

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Ooff, just like I thought. I would have been really confused if it hadn't been this way.

      BTW, OP: an excellent helper tool to see what Perl does to your data, is the standard module Data::Dumper:

      use Data::Dumper; print Dumper [qw(a b c)], \qw(a b c);
      Result:
      $VAR1 = [ 'a', 'b', 'c' ]; $VAR2 = \'a'; $VAR3 = \'b'; $VAR4 = \'c';
      The "$VARn" notation is the default variable name that Data::Dumper makes for each argument passed to the sub Dumper. So here you have 4 arguments: the first is for [qw(a b c)], the other for each item in \qw(a b c). It is indeed a list of references, with 3 items.
Re: qw with anonomous list ref
by fruiture (Curate) on Oct 28, 2002 at 22:56 UTC

    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";

    HTH

    --
    http://fruiture.de
      I didn't realize that \LIST went "hyper" and applied the ref element-wise. Thanks.

      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?

      Jenda

        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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://208626]
Approved by Tanalis
Front-paged by jarich
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-08-21 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (140 votes), past polls