文件描述符 文件操作 <> open 文件句柄

#! /usr/bin/perl
use strict;
use warnings;

=head1
print " ---------------------------------test_--------------------------- ";
if(!open LOG, ">> Z/logfile"){
    die "can't create logfile: $!";
}
print " ---------------------------------test_--------------------------- ";
=cut

=head1
print " ---------------------------------1st_use_FD_read_/etc/passwd_--------------------------- ";
unless(open PASSWD, "/etc/passwd"){
  die "How did you get logged in? ($!)";
}
while(<PASSWD>) {
    chomp;
    print $_;
    print " ";
}
print "-----over---- ---------------------------------1st_use_FD_read_/etc/passwd_--------------------------- ";
close PASSWD;
=cut1

=head1
print " ---------------------------------write_2_file_--------------------------- ";
unless(open MYLOG, ">> logfile"){
    die "err open logfile: $!";
}
my $done = 1;
my $total = 3;
print MYLOG "Captain's log, stardate 3.14159 ";
printf STDERR "%d percent compete. ", $done/$total *100;
printf (STDERR "%d percent compete. ", $done/$total *100);
printf STDERR ("%d percent compete. ", $done/$total *100);
select MYLOG;
printf ("%d percent compete. ", $done/$total *100);
select STDOUT;
print " ---------------------------------write_2_file_--------------------------- ";
close MYLOG;
=cut

=head1
print " ---------------------------------flush_buffer_--------------------------- ";
unless(open MYLOG, ">> logfile"){
    die "err open logfile: $!";
}
$| = 1;
print MYLOG "flush buffer immediately after write. ";
close MYLOG;
print " ---------------------------------flush_buffer_--------------------------- ";
=cut

=head1
print " ---------------------------------redirect_STDERR_--------------------------- ";
unless(open STDERR, ">> mySTDERR"){
    die "err open STDERR (>> mySTDERR): $!";
}
printf (STDERR "%d percent compete. ", 1/3 *100);
print " ---------------------------------redirect_STDERR_--------------------------- ";
=cut

print " ---------------------------------_--------------------------- ";

#you should always check the return value of open, since the rest of the
#program is relying upon its success.

原文地址:https://www.cnblogs.com/books2read/p/11004061.html