Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: copying a file with .bak extension

by kennethk (Monsignor)
on Nov 07, 2012 at 18:27 UTC ( #1002708=note: print w/ replies, xml ) Need Help??


in reply to [SOLVED] copying a file with .bak extension

I assume this is the entire body of the script, and not just a snippet. I also assume you are running perl with the -i switch. If you simply modify that to be perl -i.bak ..., your issue will be solved. See perlrun.

If you have your -i switch in your hashbang, just modify it there instead.

Update: If all you are doing is appending "\n##Copyright (C) 2012 by firstName lastName\n" to the tail of the file, why are you using switches when you could just open the file for appending, and append? e.g.

use File::Copy; for my $file (@ARGV) { -e $file or warn("File not found: $file\n"), next; copy($file, "$file.bak") or die "Copy failed ($file): $!"; open my $fh, '>>', $file or die "Open failed ($file): $!"; print $fh "\n##Copyright (C) 2012 by firstName lastName\n"; }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: copying a file with .bak extension
Select or Download Code
Re^2: copying a file with .bak extension
by jaffinito34 (Acolyte) on Nov 07, 2012 at 19:32 UTC

    I'm not that advanced, I just started playing with perl a couple weeks ago so I'm still learning how it works.

      I would argue that's exactly why you should adopt this approach. As a new user, you should avoid using classic Perl "magic", and make sure your code is straight-forward and readable. The script, as I've presented it, loops over the passed file names (@ARGV), tests to make sure you've passed file names that can be found (-e), copies the files using a CORE module (File::Copy) and then modifies the file with an append (perlopentut). It tests if operations succeed and is warnings and strict compliant. I never use command line switches in any of my code unless I'm writing one-liners, and half that time it would have been faster just to write a proper script.

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-09-19 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls