behavior of my print function call

Discussion in 'Perl Misc' started by Dave Slayton, Feb 19, 2007.

  1. Dave Slayton

    Dave Slayton Guest

    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!
     
    Dave Slayton, Feb 19, 2007
    #1
    1. Advertising

  2. Dave Slayton

    Guest

    "Dave Slayton" <> 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
     
    , Feb 19, 2007
    #2
    1. Advertising

  3. Dave Slayton

    Dave Slayton Guest

    <> wrote in message
    news:20070218224100.126$...
    > "Dave Slayton" <> 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
     
    Dave Slayton, Feb 19, 2007
    #3
  4. Dave Slayton

    Joe Smith Guest

    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
     
    Joe Smith, Feb 19, 2007
    #4
  5. Dave Slayton

    Guest

    "Dave Slayton" <> wrote:
    > <> wrote in message
    > news:20070218224100.126$...
    > > "Dave Slayton" <> 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
     
    , Feb 19, 2007
    #5
  6. 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
     
    John W. Krahn, Feb 19, 2007
    #6
  7. Dave Slayton

    Joe Smith Guest

    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
     
    Joe Smith, Feb 21, 2007
    #7
  8. 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
     
    John W. Krahn, Feb 21, 2007
    #8
  9. Dave Slayton

    Dave Slayton Guest

    <> wrote in message
    news:20070219121906.713$...
    > 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.
     
    Dave Slayton, Feb 23, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keto
    Replies:
    0
    Views:
    1,049
  2. grbgooglefan
    Replies:
    2
    Views:
    459
    Pascal Bourguignon
    Jan 30, 2008
  3. grbgooglefan
    Replies:
    4
    Views:
    476
    Kenny McCormack
    Jan 30, 2008
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    402
    David Cournapeau
    Dec 30, 2008
  5. Rahul

    print function behavior

    Rahul, Aug 10, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    96
    Rahul
    Aug 11, 2005
Loading...

Share This Page