Created
December 20, 2011 00:02
-
-
Save jnbek/1499512 to your computer and use it in GitHub Desktop.
This captures print output from a sub into a file handle so you can use the data, in a variable (I need an array in this case) cuz sometimes cpanel writes silly code...
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 strict; | |
use warnings; | |
use Data::Dumper; | |
sub do_print { | |
for (keys %ENV) { | |
print "$_ => $ENV{$_}\n"; | |
} | |
} | |
sub get_print { | |
my $filename = &gen_fn('/tmp'); | |
my $OLDFH = select(STDOUT); | |
open(my $FH, "+>", $filename) || die $!; | |
select($FH); | |
&do_print(); | |
seek($FH, 0, 2); | |
my $contents = do { local (@ARGV, $/) = $filename; <> }; | |
select($OLDFH); | |
close($FH); | |
my $death = unlink($filename); | |
my @data = split /\n/,$contents; | |
print Dumper(@data); | |
} | |
sub gen_fn { | |
my $tmpdir = shift; | |
my @chars = ("a" .. "z", "A" .. "Z", 0 .. 9); | |
my $frand = join "", (map { $chars[rand @chars] } @chars)[0 .. 16]; #filename | |
my $erand = join "", (map { $chars[rand @chars] } @chars)[0 .. 3]; #extension | |
if (-e "$tmpdir/$frand.$erand") { | |
&gen_fn($tmpdir); | |
} | |
return "$tmpdir/$frand.$erand"; | |
} | |
get_print(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment