perl -p with a use

N

Nicolas Fleury

Hi,
I make a script to run on Windows with ActivePerl and I would like to
make the script run with a -p or -n argument, but I want the script to
be called by its extension, which means I cannot do as on Unix and use
the #! syntax.
Is there a way to specify these parameters with a use clause, as with
"use warnings" for "perl -w"? Is the only solution to add other
extensions, like .plp and .pln, for scripts to be called with "perl -p"
and "perl -n" (or write it by hand)?

Thx
Nicolas Fleury
 
S

Steve Grazzini

Nicolas Fleury said:
I make a script to run on Windows with ActivePerl and I would like to
make the script run with a -p or -n argument, but I want the script to
be called by its extension, which means I cannot do as on Unix and use
the #! syntax.

Perl still reads the shebang line, even if the OS doesn't use
it to find the script's interpreter.
Is there a way to specify these parameters with a use clause, as with
"use warnings" for "perl -w"? Is the only solution to add other
extensions, like .plp and .pln, for scripts to be called with "perl -p"
and "perl -n" (or write it by hand)?

Writing it by hand is trivial, though... The -n switch just inserts
a "while (<>) {" before your program text, and a "}" after. The -p
switch inserts "} continue { print }" after.
 
G

Greg Bacon

: I make a script to run on Windows with ActivePerl and I would
: like to make the script run with a -p or -n argument, but I want the
: script to be called by its extension, which means I cannot do as on
: Unix and use the #! syntax.

Yes, you can:

C:\Temp>type try.pl
#! perl -p

$_ = "PREFIX: $_";

C:\Temp>try.pl try.pl
PREFIX: #! perl -p
PREFIX:
PREFIX: $_ = "PREFIX: $_";

: Is there a way to specify these parameters with a use clause, as
: with "use warnings" for "perl -w"? Is the only solution to add other
: extensions, like .plp and .pln, for scripts to be called with "perl
: -p" and "perl -n" (or write it by hand)?

Careful: there are subtle but important differences between -w and
C<use warnings>.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top