Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Reading from an array using angle brackets

by bounsy (Novice)
on Jan 23, 2013 at 21:31 UTC ( #1015029=perlquestion: print w/ replies, xml ) Need Help??
bounsy has asked for the wisdom of the Perl Monks concerning the following question:

Based on a question from a coworker that is learning Perl, I discovered today that it is possible to read from an array using angle brackets like this:

#!/usr/bin/perl use strict; use warnings; my @A = qw/a b c/; while (my $x = <@A>) { print "$x\n"; } print "@A\n";

This script prints each element of the list on its own line followed by the entire array on the last line (to prove to myself that the original array was still intact).

So here's the questions:

  1. Is this documented anywhere?
  2. Are there any limitations, gotchas, or risks to using this syntax? (And, if so, what are they?)
  3. Has anyone ever used this syntax in their scripts before?

It seems like it could be handy in some cases (maybe?). I've just never seen it before and used to think that it wouldn't work. Now I'm wondering if there are good cases to use it.

Comment on Reading from an array using angle brackets
Download Code
Re: Reading from an array using angle brackets
by LanX (Canon) on Jan 23, 2013 at 21:47 UTC
    Don't!

    > Are there any limitations, gotchas, or risks to using this syntax? (And, if so, what are they?)

    yes

    DB<109> @A = qw/{a,c}{b,d} x/; => ("{a,c}{b,d}", "x") DB<110> print "$x\n" while ($x = <@A>) ab ad cb cd x

    > Is this documented anywhere?

    yes

           glob EXPR
           glob    In list context, returns a (possibly empty) list of filename
                   expansions on the value of EXPR such as the standard Unix shell
                   bincsh would do. In scalar context, glob iterates through
                   such filename expansions, returning undef when the list is
                   exhausted. This is the internal function implementing the
                   "<*.c>" operator, but you can use it directly. If EXPR is
                   omitted, $_ is used.  The "<*.c>" operator is discussed in more
                   detail in "I/O Operators" in perlop.
    

    > Has anyone ever used this syntax in their scripts before?

    yes --> Re: Substitution of a particular letter in all combinations of positions in word (see inside)

    Cheers Rolf

Re: Reading from an array using angle brackets
by toolic (Chancellor) on Jan 23, 2013 at 21:57 UTC

    Use B::Deparse (Tip #6 from the Basic debugging checklist) to see how perl treats your code:

    use warnings; use strict 'refs'; my(@A) = ('a', 'b', 'c'); use File::Glob (); while (defined(my $x = glob(join($", @A)))) { do { print "$x\n" }; } print "@A\n";

      I knew that the angle brackets could do glob, I just didn't realize that Perl was doing a glob in this case. I guess I need to use B::Deparse more often. Thanks.

Re: Reading from an array using angle brackets
by thundergnat (Deacon) on Jan 23, 2013 at 21:58 UTC

    The array inside the angle brackets is a glob, (see perldoc -f glob) and while you probably could get away with using that for an array of alpha-numeric strings, things will behave much differently if there are any shell expansion metacharacters in it.

    You probably will not get what you expect if you use anything more complicated than a single array of alpha-numeric strings.

    use strict; use warnings; my @A = qw/a b c/; my @B = qw/1 2 3/; print "$_\n" for <@A,@B>; print '*' x 10, "\n"; print "$_\n" for (@A,@B);
    a
    b
    c,1
    2
    3
    **********
    a
    b
    c
    1
    2
    3
    

    So,

    1. Yes it is documented
    2. Yes, there are gotchas, especially if you are abusing it for the wrong reasons.
    3. Yes, I have used globs in several scripts, though I used them for their correct purpose, not for their side effects

Re: Reading from an array using angle brackets
by DrHyde (Prior) on Jan 24, 2013 at 11:57 UTC
    $ touch food fool $ perl -e '@foos=qw(foo* bar*);print join(", ", <@foos>)."\n"'; food, fool
    Oops.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (10)
As of 2014-07-30 06:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls