On 11-08-17 06:53 PM, Bryan R Harris wrote:
How can I do a 3-argument open on STDIN? This doesn't work because the
3-argument open won't open STDIN when you tell it to open "-".
**************************************
@files = ("-");
for (@files) {
print reverse readfile($_);
}
sub readfile {
open(my $fh,"<",$_[0]) or die "$me: Couldn't open $_[0]: $!\n";
my(@fc) =<$fh>;
close($fh) or die "$me: Couldn't close $_[0]: $!\n";
if ($fc[0] =~ /\r/) { @fc = map { s/\r\n?/\n/g; split /(?<=\n)/ } @fc; }
if (wantarray()) { return @fc; }
else { return join('', @fc) }
}
**************************************
How can I use the "safe" 3-argument open and still be able to read off a
pipe?
- Bryan
Well, this doesn't use the 3-argument open but it gets the job done.
#!/usr/bin/env perl
use strict;
use warnings;
my @files = ( $0, '-', $0 );
for my $file ( @files ){
print reverse readfile( $file );
}
sub readfile {
local @ARGV = @_;
my ( @fc ) = <>;
# as above
return wantarray ? @fc : join( '', @fc );
}
__END__
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
"Make something worthwhile." -- Dear Hunter
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/