in reply to Re: Why does "flush filehandle" work? (indirect object)
in thread Why does "flush filehandle" work?

print $filehandle  "Hello world"; is an everyday use of indirect object notation (same with say)

No it's not. print is not a method. This is not a method call, direct or indirect. print is an operator. Same goes for say, grep, map and sort. The lack of comma after their first operand does not make these method calls.

There is no flush operator, though. flush $filehandle is truly a method call (even though the handle might not be an object). To use flush, IO::Handle or IO::File (depending on the version of Perl) first needs to be loaded to provide the method (although newer version of Perl automatically load IO::File when needed).

Replies are listed 'Best First'.
Re^3: Why does "flush filehandle" work? (indirect object)
by LanX (Saint) on Jan 29, 2025 at 22:40 UTC
    Well obviously built-ins for print and say are needed for the non-method use.

    I remembered patching IO::Handle::say , but this will only affect say STDOUT "text" if feature "say" is not activated. Otherwise it's not working.¹

    This means the print and say built-ins are only mimicking the indirect object notation and completely ignoring the methods.

    Kind of disappointing². :/

    But seems like technically you are right.

    (While I still remember being able to make it work, I can't find the thread anymore)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

    ¹) NB STDOUT->say() will always call the patched version

    ²) because say $FH and $FH->say will diverge after patching say

      But seems like technically you are right
      This may be the least graceful way of acknowledging being under-correct that I have ever seen. Well done*.

      But seems like technically you are right.

      And you were under-correct. (Thanks etj!)

      If print SCALAR LIST, print BAREWORD LIST and print BLOCK LIST were method calls,

      • The first operand would have to refer to an object or class.
      • A method would be called.
      • Inheritance would come into play.
      • CORE overrides wouldn't come into play.
      • no feature qw( indirect ); would come into play.
      • They would be parsed differently.
      • etc.

      print, say, map, grep and sort are not indirect method calls despite having name BAREWORD and name BLOCK calling conventions like indirect method calls. They are not technically different; they are very different.

        IMHO this is all a result of piggybacking OO onto Perl 4 syntax° which already had filehandles, and I'm disappointed about the bad implementation.

        The fact that print $fh and $fh->print can divert is a serious design flaw in my books.

        Even if print is implemented as a special cased monster for backwards compatibility, print $fh should do the same like $fh->print

        Let me reiterate "you are technically correct" or in my words Perl is buggy here.

        > And you were under-correct. (Thanks etj!)

        Regarding etj, he hasn't contributed anything technical yet and has obviously beef with me.

        I have no problems admitting when I'm wrong, this is just a very ugly case to describe how wrong everything is.

        But if we want to get personal, you are the one notorious to fight till Re:50+ before admitting to be wrong or compromising that views may differ.

        > CORE overrides wouldn't come into play.

        ORLY? Please humor me and demonstrate a core override of print and say.

        They are not possible, exactly because of their weird implementation.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery

        °) For the record, I tried finding a Perl 4 language reference but failed, any hints/links?

        You're welcome!