I like using IO::File to open and read files rather than the built in way.
open my $fh, "<", $flle or die;
use IO::File;
my $fh = IO::File->new( $file, "r" );
However, what if I am treating the output of a command as my file?
The built in open function allows me to do this:
open my $cmd_fh, "-|", "zcat $file.gz" or die;
while ( my $line < $cmd_fh > ) {
chomp $line;
}
What would be the equivalent of IO::File or IO::Handle?
By the way, I know can do this:
open my $cmd_fh, "-|", "zcat $file.gz" or die;
my $cmd_obj = IO::File-> new_from_fd( fileno( $cmd_fh ), 'r' );
But then why bother with IO::File if there's already a file handle?
You can open them just like in open, because that's exactly what IO::File does - it initializes IO::Handle object and links it to file opened with Perl's native open.
use IO::File;
if (my $fh = new IO::File('dmesg|')) {
print <$fh>;
$fh->close;
}
IO::File is really just a pretty wrapper. If what it does not complex enough for you, you can just go and init IO::Handle from any FD you like yourself. You need rest of IO::* OO functionality, I suppose, so who cares how does initializer looks?