#!/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.
-
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.
-
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
-
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).
|