http://qs321.pair.com?node_id=544632


in reply to TIMTOWTDI Challenge: Open a file

What? No one's mentioned the swiss army chainsaw of of IO operations, IO::All ?

From the docs:

use IO::All; my ($io, $contents); $io = io 'file.txt'; $contents < $io;
And if you want to write to the file, you've got that too:
# manipulate $contents to write it back out: $contents > $io; $some_extra_bit >> $io;

Ignoring abominations like that module, there's also this trick:
my $filename = 'thefile.txt'; my $pid = open(FH, "-|"); if (not defined $pid) { die "cannot fork: $!; bailing out"; } if (! $pid) { exec('/bin/cat', $filename); } while (<FH>) { ... }
Which trick is actually useful if you want to do more than open a file; e.g. if you want to run some external program that needs a special environment set up before you invoke it, like tying STDERR to STDOUT, or ensuring that STDIN is closed, or that the program is disassociated from the controlling terminal via a call to POSIX::setsid. (java processes that you want running in the background are sometimes picky about being completely disassociated from the controlling terminal)
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/