Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Bug (or feature) in 5.0053 print?

by BlueLines (Hermit)
on Jul 27, 2001 at 04:28 UTC ( [id://100188]=perlquestion: print w/replies, xml ) Need Help??

BlueLines has asked for the wisdom of the Perl Monks concerning the following question:

Here's some code:
#!/usr/bin/perl -wT use strict; open (FOO, '>/tmp/foobar') or die "Couldn't open /tmp/foo: $!\n"; my $self = { 'fh' => \*FOO}; print $self->{'fh'} "This is a test\n";
This fails with the following error:
String found where operator expected at ./foo.pl line 5, near "} "Thi +s is a test\n"" (Missing operator before "This is a test\n"?) syntax error at ./foo.pl line 5, near "} "This is a test\n""
Now, if i change the code a little bit, it works fine:
my $fh = $self->{'fh'}; print $fh "This is a test\n";
Why? I thought that $self->{'fh'} wasn't being intepreted in the correct context. So i even tried this (which failed as well):
print scalar($self->{'fh'}) "This is a test\n";
Why is this? I realize that filehandle references are easier to deal with in 5.6, but we use 5.0053 at work, and there's no way we're switching anytime soon. So is this a known bug, or am i totally missing something?

BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Replies are listed 'Best First'.
Re: Bug (or feature) in 5.0053 print?
by bikeNomad (Priest) on Jul 27, 2001 at 04:54 UTC
    It behaves the same in 5.6. This works:

    print { $self->{'fh'} } "This is a test\n";
Re: Bug (or feature) in 5.0053 print?
by petral (Curate) on Jul 27, 2001 at 05:22 UTC
    Somewhere it says "the file handle for print must be a real filehandle not a reference to one".  Apparently, the object itself (that is $self->{'fh'} or more precisely \{$self->{'fh'}}) is being passed to print instead of being evaluated first like the arguments in a list being passed to a function would be.  This has to do with the whole indirect-object-notation semantics (that is a first argument with no comma after it).

      p

Re: Bug (or feature) in 5.0053 print?
by bwana147 (Pilgrim) on Jul 27, 2001 at 12:07 UTC

    Recipe 7.16 of the Cookbook explicitely explains that the file handle must be a real scalar variable, not a array or hash subscript. Which you can work around with scalar, as you discovered, or by surrounding the "complex file handle" with curlies, like bikeNomad puts it.

    --bwana147

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://100188]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-28 21:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found