Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

perl -pi -e s'/^\s+//'g $file

by david_lyon (Sexton)
on Apr 08, 2011 at 00:55 UTC ( #898233=perlquestion: print w/ replies, xml ) Need Help??
david_lyon has asked for the wisdom of the Perl Monks concerning the following question:

I have a bad habit of throwing in a one line command wihtin perl code like this:
#!/usr/bin/perl $file="test"; $cmd="perl -pi -e s\'\/^\\s+\/\/'g $file"; print "$cmd\n"; system($cmd);
Is there a quick and proper way of doing the above in a script without having something like this:
open(DATA, "$file"); while(<DATA>){ chomp; $line=$_; $line=~ s/^\s+//g; print "$line\n"; }
Thanks again for your help much appreciated.

Comment on perl -pi -e s'/^\s+//'g $file
Select or Download Code
Re: perl -pi -e s'/^\s+//'g $file
by wind (Priest) on Apr 08, 2011 at 01:41 UTC
    The equivalent of your one-liner without a system call:
    { local @ARGV = ($file); local $^I = '.bac'; while(<>){ s/^\s+//; print; } }
      Thanks again wind very nicely done....I will start using it.
Re: perl -pi -e s'/^\s+//'g $file
by duelafn (Priest) on Apr 08, 2011 at 02:57 UTC

    That is why we have subroutines:

    sub pie(&@) { my ($cb, $file) = @_; rename $file, "$file~" or die "Error backing up $file: $!"; open my $IN, "<", "$file~" or die "Error reading $file~: $!"; open my $OUT, ">", $file or die "Error writing $file: $!"; local $_; while (defined( $_ = <$IN> )) { $cb->(); print $OUT $_; } } pie { s/^\s+// } "myfile.txt";

    Good Day,
        Dean

Re: perl -pi -e s'/^\s+//'g $file
by InfiniteSilence (Curate) on Apr 08, 2011 at 03:43 UTC
    Another Monk said 'that's why we have subroutines'. I'll add to that and say, that is why we have libraries and modules:
    perl -MMyPersonalFancyModule -e 'print qq|RTFM| x 99;'

    Celebrate Intellectual Diversity

Re: perl -pi -e s'/^\s+//'g $file
by goibhniu (Hermit) on Apr 12, 2011 at 18:37 UTC

    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.
      Having written the exact code once myself, I find it disgusting :P use B::Deparse , then transform the results
      $ perl -MO=Deparse -p -i.orig -e "s/foo/bar/; ... " BEGIN { $^I = ".orig"; } LINE: while (defined($_ = <ARGV>)) { s/foo/bar/; die 'Unimplemented'; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK
      into a template
      #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Main { RuinSomeFilesOrig(@_); } sub RuinSomeFilesOrig { local *ARGV; local $^I = ".orig"; local @ARGV = @_; LINE: while (defined($_ = <ARGV>)) { s/foo/bar/; ...; } continue { die "-p destination: $!\n" unless print $_; } }
      then turn it into a distribution with scriptdist :D

        ++


        #my sig used to say 'I humbly seek wisdom. '. Now it says:
        use strict;
        use warnings;
        I humbly seek wisdom.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://898233]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2014-12-27 15:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls