In this article, let us discuss how to manipulate the file handlers in Perl.
1. Typical Way of Opening a Perl File Handlers
The perl example below opens a file with a bareword. This is a typical perl file open scenario.
#!/usr/bin/perl open FH,"</tmp/msg";
Read Operation with Bareword file handle:
#!/usr/bin/perl open FH,"</tmp/msg"; $line = <FH>; print $line;
Write Operation with the Bareword file handle:
#!/usr/bin/perl open FH,">/tmp/msg"; print FH "Perl - Practical Extraction Report Language\n";
If you want to pass this handler to a perl function, you would use typeglob as shown below.
#!/usr/bin/perl open FH,"</tmp/msg"; read_text(*FH); sub read_text { local *FH = shift; my @lines; @lines = <FH>; print @lines; }
2. Opening a Perl File Handle reference in Normal Scalar Variable
You can use a scalar variables to store the file handle reference as shown below.
#!/usr/bin/perl # $log_fh declared to store the file handle. my $log_fh; open $log_fh,"</tmp/msg"; read_text($log_fh); sub read_text { local $log_fh = shift; my @lines; @lines = <$log_fh>; print @lines; }
3. Use Perl IO::File to Open a File Handle
IO::File is a perl standard CPAN module which is used for opening a file handle in other colourful conventions. Use cpan command to install perl modules.
#!/usr/bin/perl use IO::File; $read_fh = IO::File->new("/tmp/msg",'r'); read_text($read_fh); sub read_text { local $read_fh = shift; my @lines; @lines = <$read_fh>; print @lines; }
Following perl code snippet explains perl write operation with IO::File module.
$write_fh = IO::File->new("/tmp/msg",'w');
To open the file handler in append mode, do the following.
$fh = IO::File->new("/tmp/msg",O_WRONLY|O_APPEND);
4. Open Perl File Handler in Both Read and Write mode
When you want to open both in read and write mode, Perl allows you to do it. The below perl mode symbols are used to open the file handle in respective modes.
MODE | DESCRIPTION |
---|---|
+< | READ,WRITE |
+> | READ,WRITE,TRUNCATE,CREATE |
+>> | READ,WRITE,CREATE,APPEND |
Let us write an example perl program to open a sample text file in both read and write mode.
$ cat /tmp/text one two three four five
The below code reads first line from the /tmp/text file and immediately does the write operation.
#!/usr/bin/perl open(FH,"+</tmp/text"); read_line(*FH); write_line(*FH,"222\n"); sub read_line { local *FH = shift; my $lines; $line = <FH>; print $line; } sub write_line { local *FH = shift; print FH @_; } close(FH);
The output of the above code is shown below.
$ perl ./read_and_write.pl one $ cat /tmp/text one 222 three four five
Note: Use perl debugger to debug your perl scripts.
5. Open the Standard Input and Standard Output
Perl allows you to open the standard input and standard output with other file handle names.
Perl standard output example:
#!/usr/bin/perl open(OUT,">-"); print OUT "STDOUT opened with the name as OUT";
Perl standard input example:
#!/usr/bin/perl open(IN,"-"); print "STDIN opened with the name as IN"; $input = <IN>;
6. Use sysopen() to Open the File
sysopen() function requires three arguments such as file handle, filename and mode.
Read Operation Example:
#!/usr/bin/perl sysopen(FH,"/tmp/text",O_RDONLY); $line = <FH>; print $line;
Write Operation Example :
#!/usr/bin/perl sysopen(FH,"/tmp/text",O_WRONLY); print FH "write operation";
Different types of modes are shown in the table below.
MODE | DESCRIPTION |
---|---|
O_RDONLY | READ |
O_WRONLY | WRITE |
O_RDWR | READ and WRITE |
O_CREAT | CREATE |
O_APPEND | APPEND |
O_TRUNC | TRUNCATE |
O_NONBLOCK | NON BLOCK MODE |
Note : You would need to have the habit of validating opened file handlers. The most common way of handling the file handler open failure with the die function is shown below.
open(FH,">/tmp/text") or die "Could not open /tmp/text file : $!\n";
If the above code is unable to open the file “/tmp/text”, it returns failure, and die gets executed. And the “$!” Buildin variable contains the reason for open function failure.
Comments on this entry are closed.
I have a csv file like this:
TranID,Date,AcNo,Type,Amount,ChequeNo,DDNo,Bank,Branch
132520,01-01-2011,51321342,Dr,5000,,,,
132524,01-01-2011,51321342,Dr,1000,,4126123,SB,Ashoknagar
132538,08-01-2011,51321342,Cr,1620,192101,,,
132548,17-01-2011,51321342,Cr,3500,192102,,,
132519,01-01-2011,55212341,Dr,2000,142514,,SBM,Hampankatte
132523,01-01-2011,55212341,Cr,500,192121,,,
132529,02-01-2011,55212341,Dr,5000,131211,,SB,Ashoknagar
132539,09-01-2011,55212341,Cr,500,192122,,,
132541,10-01-2011,55212341,Cr,2000,192123,,,
132525,02-01-2011,55212342,Dr,5000,,,,
132533,04-01-2011,55212342,Cr,2100,192201,,,
132526,02-01-2011,55212343,Dr,3000,126200,,ICICI,Hampankatte
132531,03-01-2011,55212343,Cr,500,192221,,,
132537,07-01-2011,55212343,Dr,5456,135123,,CB,Kankanady
132544,15-01-2011,55212343,Cr,3500,192222,,,
132546,15-01-2011,55212343,Cr,1342,192223,,,
132527,02-01-2011,55212344,Dr,5000,127821,,SB,MGRoad
132536,06-01-2011,55212344,Cr,1500,192251,,,
132528,02-01-2011,55212345,Dr,2000,162423,,CB,MGRoad
132530,03-01-2011,55212345,Cr,1000,192271,,,
132542,11-01-2011,55212345,Dr,3500,,4251234,ICICI,Hampankatte
132543,14-01-2011,55212345,Cr,1500,192272,,,
132521,01-01-2011,55212346,Dr,6000,125324,,CB,Kankanady
132532,03-01-2011,55212346,Cr,1000,192341,,,
132547,16-01-2011,55212346,Dr,2300,,,,
132522,01-01-2011,55212415,Dr,1200,162341,,SBI,Hampankatte
132534,05-01-2011,55212415,Dr,5000,162450,,SB,Kuloor
132535,06-01-2011,55212415,Cr,2000,192361,,,
132540,09-01-2011,55212415,Dr,2000,,4521349,CB,MGRoad
132545,15-01-2011,55212415,Cr,1245,192362,,,
How to Calculate and display total balance in each account using hash in perl. Without using parse function
Good one…
Excellent Documentation with examples on File handling in perl…
Really useful…
here is my function
open (MYFILE1, ‘>>/home/blddevtiers/scripts/$APP/$Build/result.txt’);
problem is variables are not taking inside the open function for that path where I want to write the file.
Please help me how to read the variables declared outside OPEN
(Sai Madhuri: Use double quotes instead of single quotes)
I had a very trivial problem where the solution took me a long time to figure out:
open(my $fileh,”>”,”example.txt”) or die;
print $fileh, “Example\n”
This printed out the file handle reference to stdout, not what I had in mind.
After reading lots of different web pages and trying various weird variants, I finally figured out that the problem was the comma. Remove the comma and it works.
Simple. Afterwards.
nice explanation