flock(MBOX,LOCK_UN);
}
- open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
+ open(my $mbox, ">>", "/usr/spool/mail/$ENV{'USER'}")
or die "Can't open mailbox: $!";
lock();
- print MBOX $msg,"\n\n";
+ print $mbox $msg,"\n\n";
unlock();
On systems that support a real flock(), locks are inherited across fork()
#...
sub read_myfile_munged {
my $ALL = shift;
- my $handle = new IO::File;
+ my $handle = IO::File->new;
open($handle, "myfile") or die "myfile: $!";
$first = <$handle>
or return (); # Automatically closed here.
reference to a new anonymous dirhandle.
DIRHANDLEs have their own namespace separate from FILEHANDLEs.
+See example at C<readdir>.
+
=item ord EXPR
X<ord> X<encoding>
better prepend the directory in question. Otherwise, because we didn't
C<chdir> there, it would have been testing the wrong file.
- opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
- @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
- closedir DIR;
+ opendir(my $dh, $some_dir) || die "can't opendir $some_dir: $!";
+ @dots = grep { /^\./ && -f "$some_dir/$_" } readdir($dh);
+ closedir $dh;
=item readline EXPR
of the special variables that you have changed. This is one of the
correct ways to read the whole file at once:
- open my $fh, "foo" or die $!;
+ open my $fh, "<", "foo" or die $!;
local $/; # enable localized slurp mode
my $content = <$fh>;
close $fh;
But the following code is quite bad:
- open my $fh, "foo" or die $!;
+ open my $fh, "<", "foo" or die $!;
undef $/; # enable slurp mode
my $content = <$fh>;
close $fh;
example:
my $content = '';
- open my $fh, "foo" or die $!;
+ open my $fh, "<", "foo" or die $!;
{
local $/;
$content = <$fh>;
The following functions:
-abs, alarm, chomp chop, chr, chroot, cos, defined, eval, exp, glob,
-hex, int, lc, lcfirst, length, log, lstat, mkdir, ord, pos, print,
-quotemeta, readlink, readpipe, ref, require, reverse, rmdir, sin, split,
-sqrt, stat, study, uc, ucfirst, unlink, unpack.
+abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, exp, glob,
+hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord, pos, print,
+quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only),
+rmdir, sin, split (on its second argument), sqrt, stat, study, uc, ucfirst,
+unlink, unpack.
=item *
=item *
-The pattern matching operations C<m//>, C<s///>, and C<tr///> when used
-without an C<=~> operator.
+The pattern matching operations C<m//>, C<s///> and C<tr///> (aka C<y///>)
+when used without an C<=~> operator.
=item *
integer. So this:
local $/ = \32768; # or \"32768", or \$var_containing_32768
- open my $fh, $myfile or die $!;
+ open my $fh, "<", $myfile or die $!;
local $_ = <$fh>;
will read a record of no more than 32768 bytes from FILE. If you're
you are outputting to a pipe or socket, such as when you are running
a Perl program under B<rsh> and want to see the output as it's
happening. This has no effect on input buffering. See L<perlfunc/getc>
-for that. (Mnemonic: when you want your pipes to be piping hot.)
+for that. See L<perldoc/select> on how to select the output channel.
+See also L<IO::Handle>. (Mnemonic: when you want your pipes to be piping hot.)
=item IO::Handle->output_field_separator EXPR
sets this variable. This means that the value of C<$!> is meaningful
only I<immediately> after a B<failure>:
- if (open(FH, $filename)) {
+ if (open my $fh, "<", $filename) {
# Here $! is meaningless.
...
} else {