Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

import list

by 7stud (Deacon)
on Jun 23, 2012 at 18:55 UTC ( [id://978000]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

Can you point me to anything that documents a minus sign in an import list, e.g.:

use English qw( -no_match_vars );

Thanks.

Replies are listed 'Best First'.
Re: import list
by moritz (Cardinal) on Jun 23, 2012 at 19:31 UTC
Re: import list
by Perlbotics (Archbishop) on Jun 23, 2012 at 19:39 UTC

    I think it's mainly defined by pure convention as intended by the module author's idea of the modules interface. Seems, the module author's intention here was to let the argument resemble a command-line option switch.

    Basically use Module qw(a b c d); is translated to BEGIN{ require Module; Module->import(qw(a b c d)); } (see use).

    In Module::import(), the author has the freedom to do anything with the given list (after removing the module's name from the parameter list) like using it as-is ... @list = qw(a b c d) ... or interpreting it as a flattened hash ... %pairs = (a => 'b', c => 'd') ... or as a parameter list ... my($a,$b,$c,$d) = qw(a b c d) ... and so on. Then s/he can treat the arguments as needed, like filtering, grouping, normalizing, etc.

    In this case: less `perldoc -l English`

    ... # Grandfather $NAME import sub import { my $this = shift; # 'English' my @list = grep { ! /^-no_match_vars$/ } @_ ; # anything that is +not '-no_match_vars' local $Exporter::ExportLevel = 1; if ( @_ == @list ) { ...

Re: import list
by tobyink (Canon) on Jun 23, 2012 at 20:28 UTC

    It doesn't have any special meaning per se, but by convention the arguments passed to a module are normally a list of subroutines that you wish to import into your namespace. Thus if a module wants to allow you to specify other options, it makes things clearer if those arguments don't "look like" subroutine names. So you often see such options starting with characters that are not allowed in subroutine names, such as '-', '!', ':' or '+'.

    The minus sign has an added advantage over other characters that due to fairly convoluted Perl parsing rules, the following:

    use Foo "-bar";

    ... can be written as:

    use Foo -bar;

    ... without violating use strict. In fact, you can even write it as:

    use Foo-bar;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: import list
by chromatic (Archbishop) on Jun 23, 2012 at 19:08 UTC

    perldoc perlop's Symbolic Unary Operators section explains this unary minus:

    Unary "-" performs arithmetic negation if the operand is numeric, including any string that looks like a number. If the operand is an identifier, a string consisting of a minus sign concatenated with the identifier is returned.

    In effect, it makes a bareword an unambiguous string constant. There's nothing special about this syntax when used in import lists (except that you'd have to write something like use English '-no_match_vars'; as an equivalent.)


    Improve your skills with Modern Perl: the free book.

      Actually the - is just a character inside a qw() so it has no special meaning and no syntax beyond the fact that it isn't a white space character and isn't the closing character for the qw() applies.

      True laziness is hard work

        Yes, true.

Re: import list
by kcott (Archbishop) on Jun 24, 2012 at 13:48 UTC

    Exporter - Specialised Import Lists shows non-alphanumeric characters which do have a special meaning in this position. The minus sign is not among them: it uses "!" for negation.

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://978000]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-24 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found