Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Why should I use perl 5.10?

by Jenda (Abbot)
on Dec 01, 2007 at 03:24 UTC ( #654259=note: print w/ replies, xml ) Need Help??


in reply to Re: Why should I use perl 5.10?
in thread Why should I use perl 5.10?

x operator on a list you can now say my @arr = qw(x y z) x 4

THAT ONE IS DANGEROUS! The current x operator provides scalar context to both its operands (@a = (1,2,3,3,3,3); print @a x 3; ==> 666), the new one will not. While this may seem unimportant since it's unlikely that you'd want to repeat several times the lenght of an array it may easily cause problems with things like foo(...) x 8. Suddenly the foo() will be evaluated in a different context! That might easily lead to hard to find bugs!

Update: Looks like the change is more restricted than I thought so it should not be a problem. Thanks Somni!


Comment on Re^2: Why should I use perl 5.10?
Select or Download Code
Re^3: Why should I use perl 5.10?
by Somni (Friar) on Dec 01, 2007 at 16:17 UTC
    This change applies specifically to qw(...). The code:

    @x = qw(a b c); print @x x 3;
    still prints "333". qw(a b c) x 3 used to be a syntax error.

    What I find confusing is why this is mentioned for 5.10. This was added in 5.8.8 according to my perl588delta.

Re^3: Why should I use perl 5.10?
by shmem (Canon) on Dec 01, 2007 at 16:47 UTC
    The qw() operator provides list context already. So
    print ( qw(a b c) x 3 );

    yields the same result as

    @l = qw(a b c); print ( (@l) x 3);

    which makes sense imho. An array isn't a list, but a qw() expression is.

    update: as a reminder, this is from perlop:

    Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in parentheses or is a list formed by "qw/STRING/", it repeats the list. If the right operand is zero or negative, it returns an empty string or an empty list, depending on the context.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://654259]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (16)
As of 2014-10-30 17:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls