Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Multidimensional arrays

by Bod (Parson)
on Jun 07, 2023 at 20:37 UTC ( [id://11152682]=note: print w/replies, xml ) Need Help??


in reply to Re: Multidimensional arrays
in thread Multidimensional arrays

the use of "indirect" method calls (like $x = new Class;)

Does this apply to filehandles?
Will we need to write $fh->print("blah"); instead of print $fh "blah";?

disabling features that cause more trouble than they're worth

I've not created a multidimensional hash for a long time and don't intend to unless I come across a situation where one makes sense. Such a situation I cannot imagine! But equally, I fail to understand what "trouble" they cause.
Could you explain please?

You are right I'm not using v5.36.
v5.16.3 on the server where this code is running and
v5.32.1 Strawberry Perl at home/office.

Replies are listed 'Best First'.
Re^3: Multidimensional arrays [plus indirect syntax]
by kcott (Archbishop) on Jun 08, 2023 at 01:22 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11152682]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-28 17:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found