Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Use modules with an import list on the command line

by Ovid (Cardinal)
on Jan 12, 2002 at 03:14 UTC ( #138176=perltutorial: print w/replies, xml ) Need Help??

Short and to the point. This is probably too simple to deserve the title of "Tutorial", but this is where this is where some Monks look.

I've noticed that occassionally people try to use the command line, but don't quite "get" how to use modules and the import list. It's fairly simple. Use the -M command line switch, followed by the module name. For example, the following are equivalent:

use LWP::Simple; getprint( "" ); # command line: perl -MLWP::Simple -e 'getprint( "" )'

Some modules either require an import list be specified or are easier to use with an import list. In this case, use the -M switch followed by an '=', with import items separated by commas.

use CGI qw/:standard *table/; print start_table( { border => 2 } ); # command line perl -MCGI=:standard,*table -e 'print start_table({-border=>2})'

There, that wasn't too painful, was it?

Replies are listed 'Best First'.
Re: Use modules with an import list on the command line
by blakem (Monsignor) on Jan 12, 2002 at 04:03 UTC
    Might also point out that the specifics are documented in this section of perlrun:

      -M[-]'module ...'
        -mmodule executes `use' module `();' before executing your program.

        -Mmodule executes `use' module `;' before executing your program. You can use quotes to add extra code after the module name, e.g., `'-Mmodule qw(foo bar)''.

        If the first character after the -M or -m is a dash (`-') then the 'use' is replaced with 'no'.

        A little builtin syntactic sugar means you can also say -mmodule=foo,bar or -Mmodule=foo,bar as a shortcut for `'-Mmodule qw(foo bar)''. This avoids the need to use quotes when importing symbols. The actual code generated by -Mmodule=foo,bar is `use module split(/,/,q{foo,bar})'. Note that the `=' form removes the distinction between -m and -M.


Re: Use modules with an import list on the command line
by VSarkiss (Monsignor) on Jan 12, 2002 at 21:27 UTC

    Just a small additional caveat: if you have shell metacharacters that may be expanded, be careful to quote them. Your CGI example may be better as: perl '-MCGI=:standard,*table' -e 'print start_table({-border=>2})'

    I admit it's unlikely you'll have a file in your current directory named "-MCGI=" something, but other patterns could be a problem.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://138176]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2023-09-24 07:55 GMT
Find Nodes?
    Voting Booth?

    No recent polls found