Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: TIMTOWTDI Challenge: Open a file

by fizbin (Chaplain)
on Apr 20, 2006 at 15:52 UTC ( #544632=note: print w/replies, xml ) Need Help??


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@/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://544632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2022-08-10 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?