select the filehandle not use the default STDOUT;
- open FILE, ">log.txt" or die "cannot open $!";
-
select FILE;
-
$| = 1;
-
print "this is to log";
-
select STDOUT;
-
print "this is to stdout\n";
after change the filehandle, better to change back.
$| special variable is flush the buffer.
Perl 5.10 borrows the say built-in from the ongoing development of Perl 6 (which may
have borrowed its say from Pascal’s println). It’s the same as print, although it adds
a newline to the end. These forms all output the same thing:
use 5.010;
print "Hello!\n";
print "Hello!", "\n";
say "Hello!";
This is especially handy in the common case of simply wanting to put a newline after whatever you want to output:
To interpolate an array, you still need to quote it though. It’s the quoting that puts the
spaces between the elements:
use 5.010;
my @array = qw( a b c d );
say @array; # "abcd\n"
say "@array"; # "a b c d\n";
Just like with print, you can specify a filehandle with say:
use 5.010;
say BEDROCK "Hello!";
阅读(450) | 评论(0) | 转发(0) |