Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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!

Replies are listed 'Best First'.
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 (Canon) 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.

Re: Question regarding options like -i -n -p
by SuicideJunkie (Vicar) 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1035906]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-07-20 03:03 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (422 votes). Check out past polls.