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

Like this sentence:

use Test::Simple tests => 1;

Sometimes I see there is a fat comma on use directive Of course I would think they are functions if it looked like:

use Foo::Bar qw(func1 func2);

But in the first case, '1' cannot be a function name. So what is that '1'? Is it a parameter to the function 'tests' or a parameter to the module Test::Simple or what???

Replies are listed 'Best First'.
Re: comma(or fat comma) in 'use' directive
by AnomalousMonk (Archbishop) on Dec 22, 2014 at 04:35 UTC

    When a module is use-ed, it can be passed a list of scalar values, strings or numbers, for the module to do with as it pleases (well, as the programmer has defined, hopefully). So in the statement
        use Foo::Bar qw(func1 func2);
    the Foo::Bar module is passed the list of strings 'func1' and 'func2'. In the case of
        use Test::Simple tests => 1;
    the module Test::Simple is passed the values 'tests' and 1 (although the latter value may be passed as a string; I'm uncertain on this point). The  => (fat comma) simply insures that  tests is interpreted by the compiler as a string when generating the list. (In fact, I think the statement
        use Test::Simple qw(tests 1);
    would work just as well, but I have not tested this.)

    Update: See Perl Modules in perlmod; also see perlmodlib.

Re: comma(or fat comma) in 'use' directive
by LanX (Saint) on Dec 22, 2014 at 05:30 UTC
    The short version of what AnomalousMonk explained is that it depends on the used module how its import() deals with passed parameters.

    Even shorter: Read the individual docs, and the author must tell you.

    IOW: see Test::Simple

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: comma(or fat comma) in 'use' directive
by Anonymous Monk on Dec 22, 2014 at 11:20 UTC

    As AnomalousMonk and LanX already mentioned above, the parameters after the module name (and optional version) in use are just the parameters to the import method from the package being used. That means that you can write use Foo "a", "b", "c";, use Foo qw/a b c/;, or use Foo a=>"b", "c";, and all three cases are exactly equivalent to BEGIN { require Foo; Foo->import("a", "b", "c"); }; the fat comma and qw// work in their usual way. So you see import and thereby use actually has as much flexibility with its arguments as any other Perl sub, including passing references or objects to import.

    It is very common for modules to use Exporter, which provides an implementation of import that interprets the parameters as a list of functions to be exported (plus a few other things, such as tags). However, how the import method handles the parameters is totally up to the module, that's why you see it being used for things other than a list of function names sometimes.