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

Re: Precedence of qw

by doc_faustroll (Scribe)
on Apr 04, 2006 at 17:03 UTC ( #541188=note: print w/replies, xml ) Need Help??

in reply to Precedence of qw

seems like you are getting too complicated and choosing the wrong operator here. Tell me why you want to use the qw operator?
from perldoc perlop

qw/STRING/ Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. It can be under- stood as being roughly equivalent to: split( , q/STRING/); the differences being that it generates a real list at compile time, and in scalar context it returns the last element in the list.

So why in the heck would you use this operator on one value in the middle of a push statement. The interpreter does its best but you are making it hemmorage here. use the q operator.

#This works: push @array, q(X) x 2; print @array, "\n";

Replies are listed 'Best First'.
Re^2: Precedence of qw
by eff_i_g (Curate) on Apr 04, 2006 at 17:14 UTC

    I was looking at qw() for maintenance purposes; so some one would be able to type in a new word if need be instead of the extra comma, quote, quote--Yes, picky, I know. But, again, this is used a lot in Perl to allow for easy modification.

    Your code will not work because q(X) x 2 results in ('XX') not ('X', 'X') as you can see via:
    #!/usr/bin/perl -w use strict; my @array; push @array, q(X) x 2; print join ' - ', @array, "\n"; @array = (); push @array, ('X') x 2; print join ' - ', @array, "\n";
      (q(X)) x 2 would give ('X', 'X')
      aha. I see. thanks.
      although if you are using single words? I can see the need for a list context with a list of things.

      anyhoo, glad the fix is in release, and my compliments to you for a clever username. one of the more clever ones on perlmonks.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://541188]
[Discipulus]: go to sleep LanX I'll give you a phone when it will happens..
LanX really needs a new phone
Discipulus LanX is obsessed by Brexit..
[LanX]: I Can't Wait ...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2018-02-23 11:20 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (301 votes). Check out past polls.