Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    IO::InSitu 0.0.2

    Download button

    No screenshots available
    Downloads: 301  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Damian Conway | More programs
    Perl Artistic License / FREE
    January 16th, 2007, 08:05 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    IO::InSitu description

    IO::InSitu is a Perl module to avoid clobbering files opened for both input and output.

    IO::InSitu is a Perl module to avoid clobbering files opened for both input and output.

    SYNOPSIS

    use IO::InSitu;

    my ($in, $out) = open_rw($infile_name, $outfile_name);

    for my $line () {
    $line =~ s/foo/bar/g;
    print {$out} $line;
    }

    When users want to do in-situ processing on a file, they often specify it as both the input and output file:

    > myapp -i sample_data -o sample_data -op=normalize

    But, if the -i and -o flags are processed independently, the program will usually open the file for input, open it again for output (at which point the file will be truncated to zero length), and then attempt to read in the first line of the now-empty file:

    # Open both filehandles...
    use Fatal qw( open );
    open my $src, '', $destination_file;

    # Read, process, and output data, line-by-line...
    while (my $line = < $src >) {
    print {$dest} transform($line);
    }

    Not only does this not perform the requested transformation on the file, it also destroys the original data. Fortunately, this problem is extremely easy to avoid: just make sure that you unlink the output file before you open it:

    # Open both filehandles...
    use Fatal qw( open );
    open my $src, '', $destination_file;

    # Read, process, and output data, line-by-line...
    while (my $line = ) {
    print {$dest} transform($line);
    }

    If the input and output files are different, unlinking the output file merely removes a file that was about to be rewritten anyway. Then the second open simply recreates the output file, ready for writing.

    If the two filenames actually refer to a single in-situ file, unlinking the output filename removes that filename from its directory, but doesn't remove the file itself from the filesystem. The file is already open through the filehandle in $input, so the filesystem will preserve the unlinked file until that input filehandle is closed. The second open then creates a new version of the in-situ file, ready for writing.

    The only limitation of this technique is that it changes the inode of any in-situ file . That can be a problem if the file has any hard-linked aliases, or if other applications are identifying the file by its inode number. If either of those situations is possible, you can preserve the in-situ file's inode by using the open_rw() subroutine that is exported from this module:

    # Open both filehandles...
    use IO::InSitu;
    my ($src, $dest) = open_rw($source_file, $destination_file);

    # Read, process, and output data, line-by-line...
    while (my $line = ) {
    print {$dest} transform($line);
    }

    Product's homepage

    Requirements:

    · Perl

      


    TAGS:

    avoid clobbering | input and output | Perl module | IO::InSitu | avoid | clobbering

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM