Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

How do I save changes made to a file

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


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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2018-06-23 10:13 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (125 votes). Check out past polls.