jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks
I seem to have a problem with qw
I have this perl line:
@arr = someFunc(@dd, qw(name1, name2, name3) ) ;
Now I have a variable: $all = "name1 name2 name3"
Somehow this doesn't work:
@arr = someFunc(@dd, qw($all) ) ; @arr = someFunc(@dd, $all) ;
I've no idea what qw actually does, so thats probably the main problem....
But how do I do this ??

Thanks a lot
Luca

Replies are listed 'Best First'.
Re: qw() problem
by muntfish (Chaplain) on Oct 10, 2005 at 11:47 UTC

    You should have a look at perlop, specifically the section called "Quote and quote-like operators". This should explain qw etc.

    I'm not 100% sure exactly what you're trying to achieve. Are you trying to make an array from the scalar value $all ? If so then split is your friend:

    $all = "name1 name2 name3"; @arr = someFunc(@dd, split(/ /, $all));

    s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&

      It doesn't matter with this $all, but there is a magical split which splits on all whitespace and trims any off the ends. It's triggered by using a quoted single space as the first argument.

      my @arr = someFunc( @dd, split ' ', $all);
      The action of magical split on a variable is very like that of qw on its literal argument.
      my @args = qw/ name1 name2 name3 /; my $all = "\n name1\n name2\n name3\n"; my @arr = split ' ', $all;

      After Compline,
      Zaxo

      split works!!!, thanks!
Re: qw() problem
by Skeeve (Parson) on Oct 10, 2005 at 11:53 UTC

    qw stands for "quote words". So qw(name1, name2, name3) is equivalent to ('name1,', 'name2,', 'name3')


    $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print

      HI,

      careful with those comma's:

      perl -e '@a = qw(a, b, c);map { print "$_\n"; } @a'

      gives:

      a, b, c

      While:

      perl -e '@a = qw(a b c);map { print "$_\n"; } @a'

      gives:

      a b c

      With qw you can skip the comma...

      UPDATE: sorry for the spelling mistake. It's commas! I've been reading slashdot too much lately.

      --
      if ( 1 ) { $postman->ring() for (1..2); }

        Not can skip but (in most cases) should skip.

        OTOH: You should have replied to the OP. I know of that fact, that's why I said that it's equivalent to

        ('name1,', 'name2,', 'name3')
        and not
        ('name1', 'name2', 'name3')

        Update: Just a side note ;-) Careful with those apostrophes! Generally there is no such thing as a plural apostrophe in any language I know of english and german. So it should be "commas" not "comma's" ;-)

        Update 2: Thanks to Perl Mouse I've learnd that the dutch language has a plural apostrophe in some cases. That's the reason for the correction above...


        $\=~s;s*.*;q^|D9JYJ^^qq^\//\\\///^;ex;print
        A reply falls below the community's threshold of quality. You may see it by logging in.
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: qw() problem
by Zaxo (Archbishop) on Oct 10, 2005 at 13:20 UTC

    Split, already mentioned, is the correct solution. It's possible to coerce qw// to work on a variable using stringy eval. Your unexpected behavior happens because qw , like q, does not interpolate variables before acting. You can fake it by including the 'qw' as part of a string in an interpolating quote,

    my $all = "\nname1 name2\n name3"; my @arr = someFunc(@dd, eval "qw/$all/";
    That's not a recommended way to do it, but it's good to know about. It's sometimes desirable to do that when the arguments of tr/// are not known in advance.

    After Compline,
    Zaxo

      it's worth noting that using qw"$foo" also doesn't work, as much as you might want it to.