in reply to perl -pi -e s'/^\s+//'g $file
I often build up proofs-of-concept from one-liners before I finally write them into a "real" script. invaluable to me in this process is perlrun. Perlrun has "actual" code that is equivalent to many of the commandline switches. For instance, under the section for -i[extension]:
From the shell, saying
$ perl -p -i.orig -e "s/foo/bar/; ... "
is the same as using the program:
#!/usr/bin/perl -pi.orig s/foo/bar/;
which is equivalent to
#!/usr/bin/perl $extension = '.orig'; LINE: while (<>) { if ($ARGV ne $oldargv) { if ($extension !~ /\*/) { $backup = $ARGV . $extension; } else { ($backup = $extension) =~ s/\*/$ARGV/g; } rename($ARGV, $backup); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT);
except that the -i form doesn't need to compare $ARGV to $oldargv to know when the filename has changed. It does, however, use ARGVOUT for the selected filehandle. Note that STDOUT is restored as the default output filehandle after the loop.
This is a great starting place for converting your on-liner and also a great place to learn how to do it from scratch the next time.
The only other thing I would urge you to do is not to forget your strictures when you start making it a longer script. your code example:
#!/usr/bin/perl $file="test"; $cmd="perl -pi -e s\'\/^\\s+\/\/'g $file"; print "$cmd\n"; system($cmd);
What I want you to do:
#!/usr/bin/perl use strict; use warnings; $file="test"; $cmd="perl -pi -e s\'\/^\\s+\/\/'g $file"; print "$cmd\n"; system($cmd);
#my sig used to say 'I humbly seek wisdom. '. Now it says:
use strict;
use warnings;
I humbly seek wisdom.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: perl -pi -e s'/^\s+//'g $file
by Anonymous Monk on Apr 12, 2011 at 18:49 UTC | |
by goibhniu (Hermit) on Apr 12, 2011 at 19:12 UTC | |
by Anonymous Monk on Apr 26, 2011 at 11:57 UTC | |
by Anonymous Monk on Mar 22, 2012 at 10:55 UTC | |
by Anonymous Monk on Nov 23, 2012 at 07:34 UTC |
In Section
Seekers of Perl Wisdom