Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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!

Replies are listed 'Best First'.
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 (Chancellor) 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.


    _($_=" "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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://654259]
and a moth chases the moon...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2017-06-27 04:28 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (598 votes). Check out past polls.