Home > Archive > PERL Miscellaneous > February 2007 > behavior of my print function call
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
behavior of my print function call
|
|
| Dave Slayton 2007-02-18, 10:00 pm |
| 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!
| |
| xhoster@gmail.com 2007-02-18, 10:00 pm |
| "Dave Slayton" <evad.notyals@liamg.moc> wrote:
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| Dave Slayton 2007-02-19, 4:01 am |
| <xhoster@gmail.com> wrote in message
news:20070218224100.126$AG@newsreader.com...
> "Dave Slayton" <evad.notyals@liamg.moc> wrote:
>
> 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).
>
>
> Xho
>
> --
> -------------------- http://NewsReader.Com/ --------------------
> Usenet Newsgroup Service $9.95/Month 30GB
| |
| Sherm Pendley 2007-02-19, 4:01 am |
| "Dave Slayton" <evad.notyals@liamg.moc> writes:
> 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?
Why? Is there something wrong with the explanation in "perldoc -f print"?
Note that if you're storing FILEHANDLES in an array or other
expression, you will have to use a block returning its value
instead:
print { $files[$i] } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";
sherm--
--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net
| |
| Joe Smith 2007-02-19, 4:01 am |
| Dave Slayton wrote:
> 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
| |
| xhoster@gmail.com 2007-02-19, 7:02 pm |
| "Dave Slayton" <evad.notyals@liamg.moc> wrote:
> <xhoster@gmail.com> wrote in message
> news:20070218224100.126$AG@newsreader.com...
>
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
| |
| John W. Krahn 2007-02-19, 7:02 pm |
| Joe Smith wrote:
> Dave Slayton wrote:
>
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Joe Smith 2007-02-21, 7:05 pm |
| John W. Krahn wrote:
> 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
| |
| John W. Krahn 2007-02-21, 7:06 pm |
| Joe Smith wrote:
> John W. Krahn wrote:
>
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Dave Slayton 2007-02-22, 10:03 pm |
| <xhoster@gmail.com> wrote in message
news:20070219121906.713$wl@newsreader.com...
> 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.
|
|
|
|
|