Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

[SOLVED] copying a file with .bak extension

by jaffinito34 (Acolyte)
on Nov 07, 2012 at 17:56 UTC ( [id://1002702]=perlquestion: print w/replies, xml ) Need Help??

jaffinito34 has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a script that opens all files passed in on the command line. Then it'll save a copy with a ".bak" extension and modify the file to add a copyright at the end. Here's what I have so far:

chomp(my @lines = <>); foreach $line(@lines){ print $line."\n"; } print "\n"; print "##Copyright (C) 2012 by firstName lastName\n";

The code so far will open the files passed thru on the command line (I'm just using a txt file for now), prints the file, and then adds the copyright at the end. I can't find anything on how to save a copy of this as a ".bak". Any help or reference to helpful material is appreciated.

Replies are listed 'Best First'.
Re: copying a file with .bak extension
by kennethk (Abbot) on Nov 07, 2012 at 18:27 UTC

    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.

      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.

Re: copying a file with .bak extension
by sanbeg (Novice) on Nov 07, 2012 at 18:22 UTC
    If you run that script with  perl -i.bak YOURSCRIPT or just put

    #! /usr/bin/perl -i.bak

    As the first line of the script it should automatically save the backup copy and write output into the original file.

      Nevermind, it was in the wrong format. Should be this:

      #! /usr/bin/perl -i[.bak]

      but now it still doesn't make a copy, or at least I don't see it in my directory.

      I added that to the top and now at the command line, I run the file...

      ~$ fileName textfile.txt

      it returns this:

      Unrecognized switch: -.bak (-h will show valid options).

      I checked out the valid options and -i should work but for some reasons it isnt.

      I tried this but it doesn't work. It clears the original file and prints the copywrite line to the command line. Seems that it has to do with the way <> works with the -i option. See my node below.

Re: copying a file with .bak extension
by Lotus1 (Vicar) on Nov 07, 2012 at 19:06 UTC

    From How can I use Perl's i option from within a program? the -i option changes $^I which in turn changes how <> works. That means the magic only works inside a while loop that uses <>. Your code with -i added on for me made a .bak copy but cleared the original and printed to the command line. The following code does what you requested.

    #!/perl -i.bak while(<>) { print; if(eof) { print "\n"; print "##Copyright (C) 2012 by firstName lastName\n" ; } }

      Script works great thanks a lot! Is it possible to have the backup get modified instead of the original? Only the original gets the modification. Thanks again.

        You could use kennethk's updated solution but open the newly created backup in append mode instead of the original.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1002702]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-23 10:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found