indirect

There seems to be a general misunderstanding regarding the equivalence of
print FILEHANDLE LIST and
FILEHANDLE->print(LIST).
They are not equivalent. See ikegami's response to "Re: Optional Subroutine Arguments Like in "print FILEHANDLE LIST"". You may find that a review of print and IO::File is useful here.

Regarding what v5.36 does (and does not) allow, see the following code.

#!/usr/bin/env perl use v5.36; use autodie; print "'print'\n"; print STDOUT "'print STDOUT'\n"; print STDERR "'print STDERR'\n"; STDOUT->print("STDOUT->print\n"); STDERR->print("STDERR->print\n"); my $dummy_output = 'test_indirect.out'; { open my $fh, '>', $dummy_output; print $fh "print \$fh\n"; system cat => $dummy_output; } { open my $fh, '>', $dummy_output; $fh->print("\$fh->print\n"); system cat => $dummy_output; } { my %file_handles; open $file_handles{test}, '>', $dummy_output; $file_handles{test}->print("\$file_handles{test}\n"); system cat => $dummy_output; } { my %file_handles; open $file_handles{test}, '>', $dummy_output; print {$file_handles{test}} "print {\$file_handles{test}}\n"; system cat => $dummy_output; } #{ # my %file_handles; # open $file_handles{test}, '>', $dummy_output; # print $file_handles{test} "print \$file_handles{test}\n"; # system cat => $dummy_output; #} use IO::File; { my $fh = IO::File::->new($dummy_output, '>'); print $fh "IO::File: print \$fh\n"; system cat => $dummy_output; } { my $fh = IO::File::->new($dummy_output, '>'); $fh->print("IO::File: \$fh->print\n"); system cat => $dummy_output; } { #use feature 'indirect'; my $fh = new IO::File($dummy_output, '>'); $fh->print("indirect! IO::File: \$fh->print\n"); system cat => $dummy_output; }

Without Perl 5.36 you won't be able to test this, so let me demonstrate.

  1. Running that as is:
    $ ./test_indirect.pl Bareword found where operator expected at ./test_indirect.pl line 63, +near "new IO::File" (Do you need to predeclare new?) syntax error at ./test_indirect.pl line 63, near "new IO::File" Global symbol "$fh" requires explicit package name (did you forget to +declare "my $fh"?) at ./test_indirect.pl line 64. Execution of ./test_indirect.pl aborted due to compilation errors.
  2. Uncommenting the use feature line:
    $ ./test_indirect.pl 'print' 'print STDOUT' 'print STDERR' STDOUT->print STDERR->print print $fh $fh->print $file_handles{test} print {$file_handles{test}} IO::File: print $fh IO::File: $fh->print indirect! IO::File: $fh->print
    • Discarding stderr:
      $ ./test_indirect.pl 2> /dev/null 'print' 'print STDOUT' STDOUT->print print $fh $fh->print $file_handles{test} print {$file_handles{test}} IO::File: print $fh IO::File: $fh->print indirect! IO::File: $fh->print
    • Discarding stdout:
      $ ./test_indirect.pl 1> /dev/null 'print STDERR' STDERR->print
  3. Uncommenting that anonymous block in the middle of the code:
    $ ./test_indirect.pl String found where operator expected at ./test_indirect.pl line 43, ne +ar "} "print \$file_handles{test}\n"" (Missing operator before "print \$file_handles{test}\n"?) syntax error at ./test_indirect.pl line 43, near "} "print \$file_hand +les{test}\n"" BEGIN not safe after errors--compilation aborted at ./test_indirect.pl + line 47.

multidimensional

I had to use that 25-30 years with Perl4. Except for the simplest data structures, I found the syntax to be unwieldy, hard to read, and easy to get wrong. I pretty much jumped with joy when Perl5 came out and I didn't have to use that syntax any more. Admittedly, I had 25-30 years less Perl experience back then — I might find it easier these days (but I'll never know as I have no intention of using it).

— Ken


In reply to Re^3: Multidimensional arrays [plus indirect syntax] by kcott
in thread Multidimensional arrays by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.