"Convert" perl command line to simple script

B

bluez34me

I've looked over the Perl FAQs, this list, and done a good bit of
googling, but haven't found an answer to this...

I have some commands that I issue via the command line that I want to
convert to scripts, but as a complete Perl newb, I haven't a clue how
to go about it. I was hoping that there was some simple way, like
saving the text as a .pl and then calling perl <script>.pl, but
obviously it doesn't work quite that way.

Here's an example of a script that I'm running to clean up html pages
that I'm generating automatically:

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,
or do I need to write a complete script that handles looping through
the files, does the regex handling and all that?

Any help greatly appreciated.
 
J

Jürgen Exner

Here's an example of a script that I'm running to clean up html pages
that I'm generating automatically:

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,
or do I need to write a complete script that handles looping through
the files, does the regex handling and all that?

Well, let's check what those command line parameters do (from perldoc
perlrun):
-e *commandline*
may be used to enter one line of program. If -e is given, Perl will
not look for a filename in the argument list. Multiple -e commands
may be given to build up a multi-line script. Make sure to use
semicolons where you would in a normal program.

Ok, that's trivial.

-i[*extension*]
specifies that files processed by the "<>" construct are to be
edited in-place. It does this by renaming the input file, opening
the output file by the original name, and selecting that output
file as the default for print() statements. [...]

Seems this is one you will have to code yourself.

-p causes Perl to assume the following loop around your program, which
makes it iterate over filename arguments somewhat like sed:

LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}

This one you will have to implement yourself, too (although that should be
rather trivial given this template).

And of course you will need to loop through all the file names resulting
from the shell glob expansion.

jue
 
P

Paul Lalli

I've looked over the Perl FAQs, this list, and done a good bit of
googling, but haven't found an answer to this...

perldoc perlrun
would be a good place to look.
I have some commands that I issue via the command line that I want to
convert to scripts, but as a complete Perl newb, I haven't a clue how
to go about it. I was hoping that there was some simple way, like
saving the text as a .pl and then calling perl <script>.pl, but
obviously it doesn't work quite that way.

sure it does. What makes you think it's "obvious" that it doesn't?
The code provided to the -e option goes into your main file. Any
other options are given after the shebang.
Here's an example of a script that I'm running to clean up html pages
that I'm generating automatically:

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,
or do I need to write a complete script that handles looping through
the files, does the regex handling and all that?

You can see what those options are doing either by investigating in
perldoc perlrun, as I already suggested, or by using the Deparse
module:

$ perl -MO=Deparse -pi -e 's/..\index/index/g' *.html
LINE: while (defined($_ = <ARGV>)) {
s/..index/index/g;
}
continue {
print $_;
}

If you don't want to put the -pi on the shebang line, just throw those
four lines of code into your .pl file, add the shebang, strict, and
warnings to the top, and call your file:
myfile.pl *.html

Paul Lalli
 
P

Paul Lalli

-i[*extension*]
specifies that files processed by the "<>" construct are to be
edited in-place. It does this by renaming the input file, opening
the output file by the original name, and selecting that output
file as the default for print() statements. [...]

Seems this is one you will have to code yourself.


Not really.
$ perldoc -q -i
Found in /opt2/Perl5_8_4/lib/perl5/5.8.4/pod/perlfaq5.pod
How can I use Perl's "-i" option from within a program?

Paul Lalli
 
P

Paul Lalli

$ perl -MO=Deparse -pi -e 's/..\index/index/g' *.html
LINE: while (defined($_ = <ARGV>)) {
s/..index/index/g;}

continue {
print $_;

}

Hrm. Accidentally truncated my output.... the start of that code
should include
BEGIN { $^I = ""; }

which is the effect of the -i parameter.

Paul Lalli
 
D

Dr.Ruud

(e-mail address removed) schreef:
perl -pi -e 's/..\index/index/g' *.html

That "..\i" most l\ikely doesn't mean what you th\ink \it means\.

(see perldoc -f quotemeta)
 
C

chanio

You say that the perl oneliners cannot translate into a script...
But, you can copy a script to run in the command-line (Linux 'only'):

As always, start with...

perl -e'

Then, press <Enter> and paste the complete script...

End with ' as usual.

Note, you should not have any ' in the script, though...
If the script has something like

$hello = 'Hello' ;

you should change it to...

$hello = q( hello ) ;

If you leave any ' then the script editing will end at that point and
start executing,,,

Linux is very 'perl friendly'.
You could learn a lot from this simetry.
 
B

Brad Baxter

I've looked over the Perl FAQs, this list, and done a good bit of
googling, but haven't found an answer to this...

I have some commands that I issue via the command line that I want to
convert to scripts, but as a complete Perl newb, I haven't a clue how
to go about it. I was hoping that there was some simple way, like
saving the text as a .pl and then calling perl <script>.pl, but
obviously it doesn't work quite that way.

Here's an example of a script that I'm running to clean up html pages
that I'm generating automatically:

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,
or do I need to write a complete script that handles looping through
the files, does the regex handling and all that?

Any help greatly appreciated.

There's a really simple way.

% ls -la perlgo
-rwxrwxr-x ... perlgo
% cat perlgo
perl -ple'}{$_=$.' *.html
% perlgo
107410
% wc -l *html
[...]
107402 total

If you feel that you need to make them into "complete" scripts, then
by all means learn how. It's a lot of fun.
 
P

Peter Scott

perl -pi -e 's/..\index/index/g' *.html

Is there some "standard" and simple way to get this into script form,

Yes. perldoc B::Deparse.

$ perl -MO=Deparse -pi -e 's/..\index/index/g'
BEGIN { $^I = ""; }
LINE: while (defined($_ = <ARGV>)) {
s/..index/index/g;
}
continue {
print $_;
}
-e syntax OK

So:

$ echo '#!'`which perl` > htmlchg
$ perl -MO=Deparse -pi -e 's/..\index/index/g' >> htmlchg
-e syntax OK
$ chmod +x htmlchg
$ ./htmlchg *html

Voila.
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top