in reply to Re: Perl's Bad Ideas
in thread Perl's Bad Ideas

Says YuckFoo:
It never made sense to me why no commas are allowed after the filehandle in a printf with parentheses. I think even if someone could explain why printf(OUT "foo\n"); is the reasonable or logical choice, I would still not be satisfied.

It's not just the reasonable, logical choice, it's pretty much the only choice. If there were a comma after the filehandle, then what would this do?

print $x, $y;
Are you printing the contents of $y to filehandle $x? Or are you printing the contents of $x and $y to stdout?

This is the same old problem that you always have when there's an optional argument at the beginning of an argument list instead of at the end: How do you tell if the optional argument is there or not? Sometimes external factors allow you to tell; when there are no such external factors, you have to invent a syntactic distinction.

--
Mark Dominus
Perl Paraphernalia

Replies are listed 'Best First'.
Re: Re: Perl's Bad Ideas
by YuckFoo (Abbot) on Apr 06, 2002 at 20:52 UTC
    See, it almost makes sense, but I'm not satisfied.

    If there were a comma after the filehandle, then what would this do? print $x, $y; Are you printing the contents of $y to filehandle $x? Or are you printing the contents of $x and $y to stdout?
    If $x is a filehandle, of course I am printing contents of $y to filehandle $x.

    Your other alternative is a non-alternative. If $x is a filehandle, print $x, $y; doesn't print contents of $x and $y to STDOUT, it generates error 'No comma allowed after filehandle...'.

    So if it's not really a choice, why not 'do the right thing'.

    If Perl is smart enough to know it's a filehandle, why isn't it smart enough to use it as such?

    YuckFoo

      Says YuckFoo:
      If $x is a filehandle, of course I am printing contents of $y to filehandle $x.
      You seem to have missed the point here. That's probably my fault.

      You said "If $x is a filehandle...". How can you tell if $x is a filehandle? (Answer: You can't.)

      If Perl is smart enough to know it's a filehandle, why isn't it smart enough to use it as such?
      Perl doesn't have any magic way to know it's a filehandle. That's why you have to leave off the comma. The missing comma says "By the way, this is a filehandle."

      Let's try a slightly more concrete example:

      $x = 'STDERR'; $y = "I like pie.\n" print $x, $y;
      Now what?

      --
      Mark Dominus
      Perl Paraphernalia

        It would have seemed to me the obvious behavior is to
        print 'STDERR', "I like pie.\n";
        to STDOUT. *However* fileno($x) returns 2. How uncool is that? I suppose you could ignore the cases where
        grep {$x eq $_ } 'STDIN', 'STDOUT', 'STDERR'
        And in all other cases you check fileno($x).

        --
        perl -pe "s/\b;([st])/'\1/mg"