Perl File Handle Reference

Source: Internet
Author: User



Perl's object-oriented is not yet introduced, so this section has nothing to say but a few things to note.



In the case of a handle that used to use uppercase letters often (that is, the so-called bare-Word file handle, Bareword filehandle), you can now consider moving to the form of a variable file handle, because you can create a file handle reference only by using a variable handle.


open DATA,">>","/tmp/a.log" or die "can't open file: $!";
open my $data_fh ,">>","/tmp/a.log" or die "can't open file: $!";
open my $fh, '<', 'castaways.log' or die "Could not open castaways.log: $!";


The bare-Word file handle and the variable file handle usage are exactly the same, and can be replaced with a variable file handle where the bare-word file handle is:


while( <DATA> ) { ... }
while( <$log_fh> ) { ... }


Regardless of whether you use a bare or variable file handle, the file handle is automatically closed when you exit the scope of the file handle, without manual close.



Just note that using the variable file handle means that when the Say/print output is specified, the file handle needs to be surrounded by curly braces to avoid ambiguity:


print {$data_fh} "your output content";


If you want a function to specify the file handle of the output, it is also simple, just use the file handle as a parameter:


log_message( $log_fh, 'My name is Mr. Ed' );
sub log_message {
    my $fh = shift;
    print $fh @_, "\n";
}
String handle


In addition to associating a handle to a file (open), pipe, Socket, directory (OPENDIR), you can also associate a handle to a string. That is, the associated object that takes a variable as a file handle, read from or write from this variable.



For example:


open my $string_fh, '>>', \my $string;
open my $string_fh, '<', \$multiline_string;


The first sentence above declares a lexical variable$string(initialized to undef) and creates a file handle that outputs an$string_fhobject that the lexical variable$stringpoints to. The second sentence$multiline_stringreads the data from the string.






You can now output some data to this file handle, which is stored$stringin:


#!/usr/bin/perl

Open my $string_fh, ">>",\my $string or die "...$!";

Print {$string_fh} "first line\n";
Print {$string_fh} "second line";

Print $string,"\n"; # output two lines: first line and second line


If you want to output the contents of the standard output stdout default device (Terminal screen) to a string, you need to be careful, because stdout is standard output after all, and many parts of the program may need to use it. Therefore, try to modify the target of the standard output within a small piece of range. For example, surround with curly braces and stdout (the bare-word file handle can only be decorated with local):


print "1. This goes to the real standard output\n";
my $string;
{
    local *STDOUT;
    open STDOUT, '>', \ $string;
    print "2. This goes to the string\n";
    $some_obj?>noisy_method();   # this STDOUT goes to $string too
}
print "3. This goes to the real standard output\n";
File Handle Container


The argument is a bit tall, in fact, is to store the file handle into the data structure (such as hash, array), do a file handle container.



For example, there is a file a.txt, which reads as follows. Now you want to store the second and third columns of each row in a variable named in the first column.


malongshuai big 1250
malongshuai small 910
gaoxiaofang big 1250
gaoxiaofang small 450
tuner middle 1218
wugui middle 199


As follows:


Use v5.10; # for state
While( <> ) {
     State $fhs; # define a hash reference variable
     My( $source, $destination, $bytes ) = split;
     Unless( $fhs?>{$source} ) { # Create a string handle when the hash key (first column) does not exist
         Open my $fh, '>>', $source or die '...';
         $fhs?>{$source} = $fh;
     }
     Say { $fhs?>{$source} } "$destination $bytes";
}


Perl File Handle Reference


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.