Filehandles are one of the many ways of getting data into and out of a perl program. In Perl, FileHandles can be assigned to special variables with name that you decide, although, as stated above, there are some reasonable standards that people follow.
- <> : This is a special 'variable' which perl makes available to all programs. It contains the data being piped into the program via STDIN, or from the file or files being fed to the perl application. Its default is to provide blocking, line based access to the data as an array. This can be changed by changing perls record separator variable $/.
        shell> perl -e 'while(<>) { print "LINE $_"; }' somefile
        shell> perl -e 'my @lines = <>; foreach (@lines) { print "LINE $_"; }' somefile
        shell> perl -e 'undef $/; my $line = <>; print "LINE $line";' somefile
        shell> ls | perl -e 'while (<>) { print "FILE $_"; }'
- STDIN, STDOUT, STDERR: These are special 'variables' which, usually, provide access to data within the standard unix input and outputs. These can be overridden with globs (more on this later).
while (my $line = <STDIN>) {
  # does the same thing as <>
}
print STDOUT "SOMETHING\n"; # prints SOMETHING to STDOUT, same as 'print "SOMETHING\n";'
print STDERR "SOMETHING\n"; # prints SOMETHING to STDERR. Things printed to STDERR do not get captured by shell pipes, and are not redirected to files where STDOUT is redirected, unless specified.
warn("SOMETHING STRANGE HAPPENED\n"); # prints SOMETHING STRANGE HAPPENED, plus some debugging information to STDERR
die("SOMETHING DEADLY HAPPENED\n"); # prints SOMETHING DEADLY HAPPENED, plus some debugging information to STDERR, and then dies with a non-zero exit status).
- open: the open command in perl allows you to open 'handles' (files, network connections, pipes, etc) for reading and writing. It is used to assign the handle to a special variable (does not contain a $,%,@, and cant be used like normal variables) of your naming. All open handles should be properly 'closed' when you are done with them (although they will be closed at desctruction, but it is better to make sure). It is recommended that handle variables be all uppercase, to prevent their being confused with other perl reserved words, eg LOG instead of log (which is log base e in perl). Also, it is important to note that these special variables are always global (eg, if you create them in the topmost scope, they will be available to all lower scopes, which may be a problem if you think you are opening a new handle to something with a previously used name within a block. There are ways to use globs to save a handle as is as a reference in a scalar, reuse the handle name for some other source, and then reassign the original handle back to the handle name later (useful for temporarily overriding where STDERR is pointed). There are other ways to contain handles, using objects, but we will talk about these later. Also, note that all of the examples below create blocking IO handles. You can do non-Blocking IO in perl, and byte-based reads and writes, but you will have to figure that out on your own (see the cookbook).
open(DATA, "</path/to/somefile");
while (my $line = <DATA>) { &do_something_with($line); }
close DATA;
open (OUTPUT, ">/path/to/somefile");
print OUTPUT "SOMETHING\n";
close OUTPUT;
sub file_handler_botching_subroutine {
  open (OUT, ">file2");
  print OUT "GOODBYE\n"; # prints GOODBYE to file2
}
sub file_handler_closing_at_the_wrong_time_subroutine {
  open (OUT, ">file3");
  print OUT "GOODBYE file3\n";
  close OUT;
}
open (OUT, ">file1");
print OUT "HELLO file1\n"; # prints to file1
&file_handler_botching_subroutine();
print OUT "HELLO AGAIN"; # prints to file2, opened in the above sub. file1 is now closed and must be reopened to access
&file_handler_closing_at_the_wrong_time_subroutine();
print OUT "HELLO AGAIN AGAIN\n"; # throws a runtime exception for attempting to print to a closed filehandle
close OUT;
        shell> perl -le 'open (PIPEOUT, "| grep -v NOT"); print PIPEOUT "HELLO"; print PIPEOUT "NOTHELLO"; close PIPEOUT;'
        shell> perl -le 'open (PIPEIN, "grep -v NOT somefile |"); ; while (<PIPEIN>) { print "GOT $_"; } close PIPEIN;'
Comments (0)
You don't have permission to comment on this page.