Reading and Writing to File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PerlNewbie308
    New Member
    • Oct 2006
    • 1

    Reading and Writing to File

    Hi,

    I'm currently writing a perl script and it's my first time doing this. So I'm not sure why I can't seem to write to the same file that I am reading. I read somewhere that I cannot read and write at the same time. Is this true? If not, can you help me in solving this writing problem I am having? Below is my code:

    my $midtierGS=`ls mtb_*_*_*_hnw_g s.xml`;

    my $disclaimer="DY NAMIC_DISCLAIME R_IMPLEMENTED";
    my $provider="MKD_ CURRENT_DATA_PR OVIDER";

    open(FILE,"+<$m idtierGS") or die "Couldn't open file!";
    #read line from file
    while ($record = <FILE>) {
    #if line contains value
    if ($record =~ m/$disclaimer/) {
    #go to next line
    $nextLine = <FILE>;
    #substitute old value for new value
    $nextLine =~ s/false/true/;
    #save changes to file
    print FILE "$nextLine" ;
    }
    elsif ($record =~ m/$provider/) {
    $nextLine = <FILE>;
    $nextLine =~ s/RSF/Comstock/;
    print FILE "$nextLine" ;
    }
    }
    close FILE;
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    From Perl Cookbook Ver 1 Chap 7.10 - Modifying a File in place without a temporary file

    The operating system treats files as unstructured streams of bytes. This makes it impossible to insert, modify, or change bits of the file in place. (Except for the special case of fixed-record-length files). You can use a temporary file o hold the changed output, or you can read the entire file into memory, change it, and write it back out again.

    To that end I give you two files. One a script to make this translation and other a file with fake data. I left things as simple as possible for you instead of dealing with file locking, or even using perl shortcut variables.

    fileop.pl
    Code:
    #!/usr/bin/perl
    
    my $file = shift;
    my $tmp = $file . ".tmp";
    
    my $disclaimer = "DYNAMIC_DISCLAIMER_IMPLEMENTED";
    my $provider = "MKD_CURRENT_DATA_PROVIDER";
    
    open(OLD, "< $file") or die "open $file: $!";
    open(NEW, "> $tmp") or die "open $tmp: $!";
    
    my %toggleBool = qw(true false false true);
    my %toggleComp = qw(RSF Comstock Comstock RSF);
    
    while (my $line = <OLD>) {
    	print NEW $line or die "print $tmp: $!";
    	
    	if ($line =~ m/$disclaimer/) {
    		$line = <OLD>;
    		$line =~ s/(true|false)/$toggleBool{$1}/;
    		print NEW $line;
    	} elsif ($line =~ m/$provider/) {
    		$line = <OLD>;
    		$line =~ s/(RSF|Comstock)/$toggleComp{$1}/;
    		print NEW $line;
    	}
    }
    close(OLD) or die "close $file: $!";
    close(NEW) or die "close $tmp: $!";
    
    unlink($file) or die "unlink $file: $!";
    rename($tmp, $file) or die "can't rename $tmp to $file: $!";
    
    1;
    
    __END__
    fileop.txt
    Code:
    foo bar like text
    DYNAMIC_DISCLAIMER_IMPLEMENTED
    false
    barbaz text
    MKD_CURRENT_DATA_PROVIDER
    RSF plus some other data
    bar foo text
    qwerty
    asdf jkl

    Comment

    Working...