Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by Anonymous Monk
on Apr 12, 2011 at 18:49 UTC ( #899027=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^3: perl -pi -e s'/^\s+//'g $file
by goibhniu (Hermit) on Apr 12, 2011 at 19:12 UTC


    #my sig used to say 'I humbly seek wisdom. '. Now it says:
    use strict;
    use warnings;
    I humbly seek wisdom.
Re^3: perl -pi -e s'/^\s+//'g $file
by Anonymous Monk on Apr 26, 2011 at 11:57 UTC

      Actually you'd need

      local @ARGV = @_; require ARGV::readonly; ARGV::readonly->import; ## THIS!!!

      But if you're going to write all that, which might confuse some, you might as well just steal ARGV::readonly verbatim

      local @ARGV = @_; # Tom Christiansen in Message-ID: <24692.1217339882@chthon> # reccomends essentially the following: for (@ARGV){ s/^(\s+)/.\/$1/; # leading whitespace preserved s/^/< /; # force open for input $_.=qq/\0/; # trailing whitespace preserved & pipes forbidden };

      Actually ARGV::readonly isn't needed with -i.orig (or -pi.orig) because perl does a stat first

      $ perl -pe 1 "echo shabba|" shabba $ perl -Tpe 1 "echo shabba|" Insecure $ENV{PATH} while running with -T switch. $ perl -Tpi.orig -e 1 "echo shabba|" Can't open echo shabba|: Invalid argument.

      Happens around

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://899027]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-04-23 09:56 GMT
Find Nodes?
    Voting Booth?