behavior of my print function call

D

Dave Slayton

I have a hash full of filehandles and a scalar variable containing a line of
text, and try to do this:

print $fh_hash{$key} $line;

which is rejected by the compiler, which says "Scalar found where operator
expected" near "} $line". It wants an operator before $line? I tried
placing parentheses around $line, but that is likewise rejected on the basis
of its not being a code reference, so it presumably thinks I'm using the
parentheses to call a subroutine. I'm clearly not understanding the parsing
that's happening there. Finally, if I surround the file handle with curly
braces like this:

print {$fh_hash{$key}} $line;

then it works as desired.

Would someone please explain what's going on there?

Thanks!
 
X

xhoster

Dave Slayton said:
I have a hash full of filehandles and a scalar variable containing a line
of text, and try to do this:

print $fh_hash{$key} $line;

which is rejected by the compiler, which says "Scalar found where
operator expected" near "} $line". It wants an operator before $line?

Sure. For example, the comma operator would make it happy. perhaps
you wouldn't be happy, but the parser would be.

....
Finally, if I surround
the file handle with curly braces like this:

print {$fh_hash{$key}} $line;

then it works as desired.

Would someone please explain what's going on there?

perldoc -f print

I see no reason to think I could do a better job of explaining that
is already done in the documentation. Have you already read it and
want more explanation? If so, could you describe in more detail
what you want explained?


Xho
 
D

Dave Slayton

Sure. For example, the comma operator would make it happy. perhaps
you wouldn't be happy, but the parser would be.

...

perldoc -f print

I see no reason to think I could do a better job of explaining that
is already done in the documentation. Have you already read it and
want more explanation? If so, could you describe in more detail
what you want explained?

Well, I see that the documentation states the curly braces must be there.
It doesn't begin to explain why. Also, some of the other things it offers
don't seem to work as it suggests, e.g.:
"(NOTE: If FILEHANDLE is a variable and the next token is a term, it may be
misinterpreted as an operator unless you interpose a "+" or put parentheses
around the arguments.)"
As I said, parentheses around $file make it think I'm invoking a subroutine,
and a + right before $file (or $file surrounded by parentheses) gives me a
very large number for output. Putting in a comma as you said does indeed
make the parser happy, but doesn't produce the desired result (which is why
I wouldn't be happy, I guess).
 
J

Joe Smith

Dave said:
Well, I see that the documentation states the curly braces must be there.
It doesn't begin to explain why.

You're right. Somewhere else it's documented as needing a simple scalar.
Also, some of the other things it offers don't seem to work as it suggests, e.g.:
"(NOTE: If FILEHANDLE is a variable and the next token is a term, it may be
misinterpreted as an operator unless you interpose a "+" or put parentheses
around the arguments.)"

As I said, parentheses around $file make it think I'm invoking a subroutine,

The "it may be misinterpreted" refers to the things that are after the
filehandle, not the filehandle itself.


Misinterpreted:

print ($a+$b)/$c; <==> (print($a+$b)) / $c;

print $fh ($a+$b)/$c; <==> (print $fh ($a+$b)) / $c;

Interposing a "+" or putting parentheses around the arguments means:

print +($a+$b)/$c; <==> print (($a+$b)/$c);

print $fh +($a+$b)/$c; <==> print $fh (($a+$b)/$c);

-Joe
 
X

xhoster

Dave Slayton said:
Well, I see that the documentation states the curly braces must be there.
It doesn't begin to explain why.

What kind of answer to "why" are you looking for? The design decision?
The reason behind the design decision? A physchoanalysis of the people
who made that decision? The part of the perl source code that implements
that part of the parsing? An interpretation of that code into pseudocode?
Also, some of the other things it
offers don't seem to work as it suggests, e.g.:
"(NOTE: If FILEHANDLE is a variable and the next token is a term, it may
be misinterpreted as an operator unless you interpose a "+" or put
parentheses around the arguments.)"

As I said, parentheses around $file make it think I'm invoking a
subroutine,

The docs do not suggest that putting paranethesis around $line (not
$file) will let you ignore the rest of the documentation!

print $fh_hash{$key} ($line);

Now, is $fh_hash{$key} a simple scalar variable? No. So then we interpret
the print as taking a list, not a filehandle followed by a list. What
would the degenerate list $fh_hash{$key}($line) be interpreted as? A call
to a subroutine via a code-ref, of course.


Xho
 
J

John W. Krahn

Joe said:
You're right. Somewhere else it's documented as needing a simple scalar.


The "it may be misinterpreted" refers to the things that are after the
filehandle, not the filehandle itself.


Misinterpreted:

print ($a+$b)/$c; <==> (print($a+$b)) / $c;

print $fh ($a+$b)/$c; <==> (print $fh ($a+$b)) / $c;

Incorrect:

$ perl -MO=Deparse,-p -e'print $fh ($a+$b)/$c;'
print($fh (($a + $b) / $c));
-e syntax OK



John
 
J

Joe Smith

John said:
Joe Smith wrote:

Incorrect:

$ perl -MO=Deparse,-p -e'print $fh ($a+$b)/$c;'
print($fh (($a + $b) / $c));

That wasn't what I expected, but you're right.
-Joe
 
J

John W. Krahn

Joe said:
That wasn't what I expected, but you're right.

The rule "It looks like a function, therefore it is a function" only works if
the parenthesis directly follows the function name. In your example there was
a filehandle between the function name and the parenthesis so it looks like an
operator instead of a function.



John
 
D

Dave Slayton

What kind of answer to "why" are you looking for?
........
........
print $fh_hash{$key} ($line);
Now, is $fh_hash{$key} a simple scalar variable? No. So then we
interpret
the print as taking a list, not a filehandle followed by a list. What
would the degenerate list $fh_hash{$key}($line) be interpreted as? A call
to a subroutine via a code-ref, of course.

Ok, there, THAT kind of answer to "why" is the kind I was looking for.
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top