Visit Our UNIX and Linux User Community


Perl write and read on same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl write and read on same file
# 1  
Old 08-22-2013
Perl write and read on same file

Hi,

I am trying to do a write operation followed by a read operation on the same file through Perl, expecting the output produced by read to contain the new lines added, as follows:

Code:
#! /usr/bin/perl -w

open FH, "+< testfile" or die "$@";
print FH  "New content added\n";
while (my $line = <FH>) {
    print "$line";
}
close(FH);

but the above code is sometimes corrupting the file's data (like replacing, globbering spaces etc.,). I have tried to flush the buffer but to no avail:
Code:
open FH, "+>> testfile" or die "$@";
$| = 1;
print FH  "New content added\n";
while (my $line = <FH>) {
    print "$line";
}
FH->autoflush(1);
close(FH);

I have even changed the mode to '+>', '+>>', but nothing works (i.e) the output is not printed with the lines recently added unless I close the file handle and reopen the file for reading.

Kindly help me how to modify the above Perl code to see the contents that are added by the previous write operation without closing the handle. Thank you.
# 2  
Old 08-22-2013
The best option for such requirement is Tie::File

In anyway, you need to seek to the beginning of the file before reading it again.

Code:
#! /usr/bin/perl -w

open FH, "+< testfile" or die "$@";
print FH  "New content added\n";
# Move Curser to start
seek(FH,0,0);
while (my $line = <FH>) {
    print "$line";
}
# Move it to END
seek(FH,0,2);
close(FH);

This User Gave Thanks to rajamadhavan For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #22
Difficulty: Easy
RAM is considered volatile memory in a computer system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read/write perl file

Hi I am trying to build a web form where it can take the input from the user and write it to a file. And when I will open that form again that for should read the file that was created at the 1st step and all the fields should auto populate from that file. I have 20 text fields in my form. I... (1 Reply)
Discussion started by: sauravrout
1 Replies

2. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

3. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

4. Shell Programming and Scripting

Read and write in the file

Hello Guys, How all are doing? I have an issue in Unix and want help from all of you I have a file in UNIX which it read by line by line , If at the end of line '0' is written the it should fetch that line into another file and change '0' to '1' and If at the end of line '1' is written then it... (10 Replies)
Discussion started by: adisky123
10 Replies

5. Shell Programming and Scripting

File Read and Write

I have got a file in following format: AAAAAAA BBBBBBBB CCCCCCC DDDDDDD I am trying to read this file and out put it in following format: AAAAAAA,BBBBBBB,CCCCCCC,DDDDDD Preferred method is shell or Perl. Any help appreciated. (11 Replies)
Discussion started by: Araoki
11 Replies

6. Shell Programming and Scripting

perl script to check read/write/execute permission for 'others'

I want to check access rights permissions not for 'user', not for 'group', but for 'others'. I want to do it by system command in which i want to use 'ls -l' and 'awk' command. I have written the following program : #!/usr/bin/local/perl #include <stdlib.h> system ("ls -l | awk... (1 Reply)
Discussion started by: shubhamsachdeva
1 Replies

7. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

8. Shell Programming and Scripting

perl read and write to conf file

Hi Everyone, There is a perl file: a.pl ============ #!/usr/bin/perl my $config_file = $ARGV; open CONFIG, "$config_file" or die "Program stopping, couldn't open the configuration file '$config_file'.\n"; my $config = join "", <CONFIG>; close CONFIG; eval $config; die "Couldn't... (1 Reply)
Discussion started by: jimmy_y
1 Replies

9. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies

10. Shell Programming and Scripting

read and write from a file

I have tried to show the file name whose size is greater than 200 byte in current directory. Please help me. ls -l | tr -s " " " " | cut -f 5,9 -d " " >out.txt #set -a x `cat out.txt` i=0 `cat out.txt` | while do read x echo $x #re=200 j=0 if }" < "200" ] then echo $j j=`expr $j... (2 Replies)
Discussion started by: rinku
2 Replies

Featured Tech Videos