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

It must be a silly question, but I know you (all the perl enlighted) must have the answer...

why do people use things like this :

use vars qw(@array);

rather than

use vars(@array);


I think I've read in the camel book that qw() is equal to ().
So is there a special reason to use qw() except for style (even if for my part : shorter is nicer...)

Replies are listed 'Best First'.
Re: why qw() rather than ()
by jeroenes (Priest) on Jan 22, 2001 at 18:15 UTC
    AFIC, qw() allows you to separate the entries with whitespace. That's pretty convenient. Look at this:
    $ perl -e 'print join(", ",qw(1 3 6 2));' 1, 3, 6, 2
    But trying that without the qw gives:
    [jeroen@rulffk] perl -e 'print join(", ",(1 3 6 2));' Number found where operator expected at -e line 1, near "1 3" (Missing operator before 3?) syntax error at -e line 1, near "1 3" Number found where operator expected at -e line 1, near "3 6" (Missing operator before 6?) Number found where operator expected at -e line 1, near "6 2" (Missing operator before 2?) Execution of -e aborted due to compilation errors.
    Hope this helps.

    Jeroen
    "We are not alone"(FZ)

      Ok ! thanks for the answer ! I've totally miss the separator problem !

      So, if I've understood

      use Digest::MD5  qw(md5 md5_hex md5_base64);

      is exactly the same as

      use Digest::MD5(md5,md5_hex,md5_base64);


        Well, if you add a space after MD5 ;-).

        But that's not the whole thing. From the manpage:

        =item qw/STRING/ Returns a list of the words extracted out of STRING, using embedded whitespace as the word delimiters. It is exactly equivalent to split(' ', q/STRING/); Some frequently seen examples: use POSIX qw( setlocale localeconv ) @EXPORT = qw( foo bar baz ); A common mistake is to try to separate the words with comma or to put comments into a multi-line qw-string. For this reason the C<-w> switch produce warnings if the STRING contains the "," or the "#" character.
        So, there is a difference, although small.

        Jeroen
        "We are not alone"(FZ)

        No.

        Using strict would point out the error for you.

Re: why qw() rather than ()
by mwp (Hermit) on Jan 22, 2001 at 18:32 UTC
    use vars qw(@array %hash $scalar); Is actually equivalent to: use vars ('@array', '%hash', '$scalar');

    qw() is great for defining lists, especially of single-word strings, because you don't have to quote and comma delimit the list. It's usefulness becomes even more apparent when you have rather complicated code and if you can reduce punctuation you'll save on aspirin bills.

Re: why qw() rather than ()
by chipmunk (Parson) on Jan 22, 2001 at 19:28 UTC
    The first snippet of code declares a variable named '@array'. The second snippet of code declares the variables named by the elements of @array, whatever they may be.

    #!perl -w use strict; use vars qw(@array); # same as: use vars '@array'; BEGIN { # the use vars below happens at compile time, # so @array must be set at compile time @array = ('$scalar', '%hash'); } use vars (@array); # $scalar and %hash are now declared $scalar = 7; %hash = (key => 'value');
    qw/STRING/ is actually the same as split ' ', 'STRING'.