in reply to Why does "flush filehandle" work?

I second the others in avoiding indirect object notation.

But it's worth noting an "inconsistency" when printing to filehandles, since

is an everyday use of indirect object notation (same with say )

So common that no feature "indirect" won't disable it.

This will also answer the question, how to add arguments to such a method call:

Drop the comma between object/class and first argument.

Now I'm curious to know how the very common

is effected by this feature.

Edit

Brian's article says that new is also forbidden!

Which makes sense since it's not a keyword, you can name the constructer(s) freely.

But it also claims that filehandles are generally exempted, which would also cover flush...

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

Replies are listed 'Best First'.
Re^2: Why does "flush filehandle" work? (indirect object)
by ikegami (Patriarch) on Jan 29, 2025 at 19:10 UTC

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

      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.