Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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


Comment on Re^2: perl -pi -e s'/^\s+//'g $file
Select or Download Code
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 http://perl5.git.perl.org/perl.git/blob?f=doio.c#l897

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2015-07-30 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls