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.

In reply to Re: perl -pi -e s'/^\s+//'g $file by goibhniu
in thread perl -pi -e s'/^\s+//'g $file by david_lyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.