# "Convert" perl command line to simple script

Discussion in 'Perl Misc' started by bluez34me@gmail.com, Jun 28, 2007.

1. ### Guest

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.

, Jun 28, 2007

2. ### Jürgen ExnerGuest

wrote:
> 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 Jürgen Exner, Jun 28, 2007 1. ### Advertisements 3. ### Paul LalliGuest On Jun 28, 11:44 am, wrote: > 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

Paul Lalli, Jun 28, 2007
4. ### Paul LalliGuest

On Jun 28, 12:28 pm, "Jürgen Exner" <> wrote:

> -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 Paul Lalli, Jun 28, 2007 5. ### Paul LalliGuest On Jun 28, 12:47 pm, Paul Lalli <> wrote: >$ 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 Paul Lalli, Jun 28, 2007 6. ### Dr.RuudGuest 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) -- Affijn, Ruud "Gewoon is een tijger." Dr.Ruud, Jun 28, 2007 7. ### chanioGuest 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. chanio, Jun 29, 2007 8. ### Tad McClellanGuest Tad McClellan, Jun 29, 2007 9. ### Brad BaxterGuest On Jun 28, 11:44 am, wrote: > 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. -- Brad Brad Baxter, Jun 29, 2007 10. ### Peter ScottGuest On Thu, 28 Jun 2007 15:44:07 +0000, bluez34me wrote: > perl -pi -e 's/..\index/index/g' *.html > > Is there some "standard" and simple way to get this into script form, Yes. perldoc B:eparse.$ 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.

--
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/

Peter Scott, Jun 29, 2007