#!/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;
}
####
$ ./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.
####
$ ./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
####
$ ./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
####
$ ./test_indirect.pl 1> /dev/null
'print STDERR'
STDERR->print
####
$ ./test_indirect.pl
String found where operator expected at ./test_indirect.pl line 43, near "} "print \$file_handles{test}\n""
(Missing operator before "print \$file_handles{test}\n"?)
syntax error at ./test_indirect.pl line 43, near "} "print \$file_handles{test}\n""
BEGIN not safe after errors--compilation aborted at ./test_indirect.pl line 47.