Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Question regarding options like -i -n -p

by Anonymous Monk
on May 29, 2013 at 19:32 UTC ( #1035906=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks :) I understand that option like -i do something like this around the script:
#!/usr/bin/perl while (<>) { if ($ARGV ne $oldargv) { rename($ARGV, $ARGV . '.bak'); open(ARGVOUT, ">$ARGV"); select(ARGVOUT); $oldargv = $ARGV; } s/foo/bar/; } continue { print; # this prints to original filename } select(STDOUT);
My question here is, let's say you want to use -i and to destroy the .bak files generated by your script after it has processed all your files (I am forced to make backups because I'm on windows). How can you "access", I mean write some code after the line: select(STDOUT) in the code above? Thanks a lot!

Comment on Question regarding options like -i -n -p
Download Code
Re: Question regarding options like -i -n -p
by SuicideJunkie (Priest) on May 29, 2013 at 20:25 UTC

    If you want to do more than just simple file munging, then a one-liner on the command prompt is probably not for you.

    Populate a .pl file with the code, and add some extra code to the end, comments and strict and warnings, and even newlines for readability. Then run that script.

    If you tell us what it is you want to do, there may be a trick for it, but in general, just make a "real" script. See below posts for deleting backup files at the end.

Re: Question regarding options like -i -n -p
by Corion (Pope) on May 29, 2013 at 20:28 UTC

    I would keep a list of files to delete, and delete them in an END block:

    push @CLEANUP, $ARGV if $oldargv ne $ARGV; END { unlink $_ for @CLEANUP };

    ... of course, that somewhat circumvents the idea of backup files, as that END block will still wipe all files ...

Re: Question regarding options like -i -n -p
by Laurent_R (Parson) on May 29, 2013 at 20:34 UTC

    I am not sure of what you want exactly, but if you want to get rid of bakcup files, you can do something like this:

    $ perl -i.bak -pe 's/ /  /g; END {unlink "*.bak"}' file.txt

    This is *nix syntax, but I guess you could have this untested equivalent under Windows:

    perl -i.bak -pe "s/ /  /g; END {unlink qq/*.bak/}" file.txt

      I don't remember about (and cannot test (and don't want to bother looking up)) unlink under *nix, but under Windoze it doesn't do wildcards. But this sure works (for Windose):

      >perl -wMstrict -le "unlink glob '*.bak'; "

        Yes, you are right, my mistake. The unlink function does not take wildwards, also not under *nix. So the use of the additional glob function would be necessary to get it to work.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-12-19 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (85 votes), past polls