On Sunday 09 May 2010 08:07:20 John W. Krahn wrote:
> Xiao Lan (??) wrote:
> > # cat t1.pl
> > use strict;
> > use warnings;
> > 
> > my $cfg = "1.txt";
> > my @x = qw/a b c 1 2 3/;
> > my $fd;
> > 
> > open $fd,">",$cfg or die $!;
> > print $fd for @x;
> 
> Perl doesn't know that $fd is a filehandle and so it thinks you mean:
> 
> print STDOUT $fd for @x;
> 
> Just remove the foreach statement modifier:
> 
> print $fd @x;

Indeed. Note that Perl Best Practices recommends (and I tend to agree) that 
you always specify the {...} in the filehandle expression to "perldoc -f 
print" and friends:

print {$fd} @x;

You need it for more complex expressions:

print {$self->_for_err($stuff) ? $self->_error() : $self->_out()} @x;

Finally, it is a good idea to avoid using $_ as much as possible in serious 
code, because it is easily modified and destroyed. Always do 
<< for my $iter_var >> with an explicit variable. It is still OK to use $_ in 
map, grep and friends, but even then it's often a good idea to do << my $item 
= $_; ... >>.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to