原文链接:https://www.thegeekstuff.com/2010/09/perl-file-handle/
In this article, let us discuss how to manipulate the file handlers in Perl.
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;
}
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;
}
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);
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.
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>;
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.