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

I understand that
@test = qw/words go here/;

puts the elements into the array in seperate indexes, but what does it mean when someone uses qw in conjunction with use My perl book doesn't describe it well and some googling and super searching here didn't quite define it... Many thanks for the information..

Replies are listed 'Best First'.
Re: qw and its lovely magic
by Zaxo (Archbishop) on Jun 24, 2003 at 20:53 UTC

    That's simply one convenient way of providing a list of arguments to the module. use strict qw/vars subs/; is equivalent to use strict 'vars', 'subs';.

    Arguments to module loading are often handled by Exporter.pm to guide function import by the use-ing namespace.

    After Compline,
    Zaxo

Re: qw and its lovely magic
by chromatic (Archbishop) on Jun 24, 2003 at 20:52 UTC

    qw() creates a list by splitting the contents on whitespace. You can use it any place you can use a list. See "Regexp Quote-Like Operators" in perlop.

Re: qw and its lovely magic
by theorbtwo (Prior) on Jun 24, 2003 at 21:38 UTC

    The answer is quite simple: qw forms a list, not an array. That list can be assigned to an array, of course.

    @test = qw/words go here/; is exactly equivlent to @test = ('words', 'go', 'here');. As another poster mentioned, use takes a list argument, which is passed to the import method of the module being used.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: qw and its lovely magic
by Tomte (Priest) on Jun 24, 2003 at 20:48 UTC

    perldoc -f use

    use Module VERSION LIST
    use Module VERSION
    use Module LIST
    use Module
    use VERSION
    Imports some semantics into the current package from the named module, generally by aliasing certain subroutine or variable names into your package.

    perldoc perldata

    the resulting list value is interpolated into LIST just as if each individual element were a member of LIST. Thus arrays and hashes lose their identity in a LIST--the list
    (@foo,@bar,&SomeSub,%glarch)
    contains all the elements of @foo followed by all the elements of @bar,

    Update:So what happens is, that the array build using qw is, don't know how to put it, transformed into a list if given as an argument to use.
    ++theorbtwo, missed to scann the relevant docs :)
    perldoc perlop

    qw/STRING/
    Evaluates to a list of the words extracted out of STRING, using embedded whitespace as the word delimiters.

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

Re: qw and its lovely magic
by demerphq (Chancellor) on Jun 24, 2003 at 23:43 UTC

    Just to add to the comments about qw() and lists and import. There are many modules and situations where it is common not to use qw(), prefering to use key=>VALUE, pairs like

    use constant DEBUG=>1;
    and the quoting usual for that style. However, Exporter's behaviour and the general needs of the import interface often mean that that style is not convenient, and so qw() becomes common. There are other Exporter style modules that have different syntaxes, where I imagine qw() ends up being used less often.


    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: qw and its lovely magic
by tcf22 (Priest) on Jun 24, 2003 at 23:48 UTC
    You see it a lot when you need a function that isn't exported by default. For example
    use CGI qw/escape unescape/;
    allows you to use the escape and unescape functions as if you had defined them in your namespace.
    use strict; use CGI qw/escape unescape/; my $escval = escape('test script'); print "Escaped: $escval\n"; my $val = unescape($escval); print "Unescaped: $val\n";
    Outputs
    Escaped: test%20script Unescaped: test script
      Thank you guys and gals for such a thorough explanation of the many uses of qw.... Thanks for taking a simple question so seriously.