Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I save changes made to a file

( #6339=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 28, 2000 at 21:54 UTC
Q&A  > directories


Description:

I have a file and I need to comment out two lines.After commenting them out I need to save the changes . Need Help ASAP Thanks and Have a perl day

Answer: How do I save changes made to a file
contributed by devslashneil

Tie::File could be used to automatically save changes to the file. This is also useful if the file isn't small enough to fit into memory all at once.

#!/usr/bin/perl use Tie::File; my $filename = 'foo.dat'; tie @array, 'Tie::File', $filename or die "Cannot open $filename\n"; $array[2] = '#'.$array[2]; $array[3] = '#'.$array[3];

This will comment out lines 3 and 4 in the file "foo.dat" and automatically save changes.

- Neil
Answer: How do I save changes made to a file
contributed by chromatic

Simply open the file for writing (not appending) and write out the entire file. This is the easiest way to do it:

# file opened, read into $my_data, closed, and $my_data modified appro +priately { local *OUTPUT; open(OUTPUT, "> filename_here") || die "Can't open output file: $! +"; print OUTPUT $my_data; close(OUTPUT) || die "Can't close my file: $!"; }
A few comments. The curly braces give us a block scope for the local filehandle OUTPUT. If OUTPUT is defined with a global scope elsewhere in the program (as it may be in the future), we don't want to clobber it, so local within a block is a good idea.

Next, we open that filehandle to write (that's what the angle brace is for) to the filename. If that doesn't work, we print an error message using the special variable $! (which holds the error message from the failed open call).

After that, we print to the filehandle. No big deal there. Finally, we close the filehandle and again print an error message if it fails. That's the end of the block.

This is by far the easiest way to save changes to a file. Other options include using sysread and syswrite or using append mode (">> filename_here"), but if your file is small enough to fit into memory all at once, just do it this way.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (11)
    As of 2014-04-17 18:37 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (453 votes), past polls