Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > behavior of my print function call

Reply
Thread Tools

behavior of my print function call

 
 
Dave Slayton
Guest
Posts: n/a
 
      02-19-2007
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!


 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-19-2007
"Dave Slayton" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Dave Slayton
Guest
Posts: n/a
 
      02-19-2007
<(E-Mail Removed)> wrote in message
news:20070218224100.126$(E-Mail Removed)...
> "Dave Slayton" <(E-Mail Removed)> 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?


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



 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      02-19-2007
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
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      02-19-2007
"Dave Slayton" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
> news:20070218224100.126$(E-Mail Removed)...
> > "Dave Slayton" <(E-Mail Removed)> 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?

>
> 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
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-19-2007
Joe Smith wrote:
> 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;


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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      02-21-2007
John W. Krahn wrote:
> Joe Smith wrote:


>> 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));


That wasn't what I expected, but you're right.
-Joe
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-21-2007
Joe Smith wrote:
> John W. Krahn wrote:
>> Joe Smith wrote:

>
>>> 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));

>
> 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
 
Reply With Quote
 
Dave Slayton
Guest
Posts: n/a
 
      02-23-2007
<(E-Mail Removed)> wrote in message
news:20070219121906.713$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print a vs print '%s' % a vs print '%f' a David Cournapeau Python 0 12-30-2008 03:19 AM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
Problem - I want to print Current Output of Pdf file and should print once.I get print dialog box but it is not working keto Java 0 05-30-2007 11:27 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM



Advertisments