6 Perl File Handle Examples to Open, Read, and Write File

原文链接: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.

MODEDESCRIPTION
+<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.

MODEDESCRIPTION
O_RDONLYREAD
O_WRONLYWRITE
O_RDWRREAD and WRITE
O_CREATCREATE
O_APPENDAPPEND
O_TRUNCTRUNCATE
O_NONBLOCKNON 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值