But there's a more elegant way to do it that doesn't rely on shells, and works more consistently. In fact TIMTOWTDI.
One way is fill the code with:
if ($outputInFile) { print $file "foo";}
else{print "foo"}
/me shivers ...
I found a way to do it quite elegantly redirecting a file handler where I'll be printing to STDOUT, using Typeglobs. I'm not too confident managing typeglobs, but it seems to work :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper::Perltidy; | |
use Getopt::Long; | |
use Perl6::Say; | |
use Pod::Usage; | |
use autodie; | |
use strict; | |
use warnings; | |
sub man {#{{{ | |
pod2usage( | |
-exitval => 1, | |
-verbose => 2 | |
); | |
}#}}} | |
# main | |
GetOptions ( | |
'man' => \&man, | |
); | |
my $output = shift; | |
my $outputfh; | |
if (defined $output) { | |
open $outputfh , '>', $output ; | |
} | |
else { $outputfh = \*STDOUT } | |
print $outputfh "prova\n"; | |
close $outputfh; | |
__END__#{{{ | |
perl fileOrSTDOUT1.pl | |
prints "prova" on STDOUT | |
perl fileOrSTDOUT1.pl /tmp/output | |
writes "prova" to file /tmp/output | |
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ###}}} |
I asked at #barcelona.pm and alexm (O HAI! president) told me I could think it the other way around, and overwrite STDOUT to an opened filehandle in case I needed the redirection. I didn't know I could handle STDOUT like any other fh. It's nice to know it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper::Perltidy; | |
use Getopt::Long; | |
use Perl6::Say; | |
use Pod::Usage; | |
use autodie; | |
use strict; | |
use warnings; | |
sub man {#{{{ | |
pod2usage( | |
-exitval => 1, | |
-verbose => 2 | |
); | |
}#}}} | |
# main | |
GetOptions ( | |
'man' => \&man, | |
); | |
my $output = shift; | |
if (defined $output) { | |
open STDOUT , '>', $output ; | |
} | |
print "prova\n"; | |
__END__#{{{ | |
perl fileOrSTDOUT2.pl | |
prints "prova" on STDOUT | |
perl fileOrSTDOUT2.pl /tmp/output | |
writes "prova" to file /tmp/output | |
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ###}}} |
I think Casiano taught me (back in university times) another way to do it (maybe it was using tee, or some IO::Handle funky stuff).
Is there any cpan module that does that kind of Stdout vs file output? I haven't found it, but it MUST be there. Or maybe the code to do it is so small it doesn't make sense writing a module for that