in reply to codeopen(INPUT, "<INPUT") gives INPUT opened only for output

If you don't already have it, download File::Temp and use it. Really.

With that what you want to do can be done as:

use File::Temp qw(tempfile); my $temp_fh = tempfile(); my $saved_fh = select($temp_fh); eval "print 'Waka waka!'"; seek ($temp_fh, 0, 0); select($saved_fh); $text = join '', <$temp_fh>; print "The text was '$text'\n";
In fact looking at this, you can add error trapping etc while turning this into a function:
use File::Temp; use Carp; # Takes code to be evaled, supresses printing and returns # the text that would have been printed. sub eval_print_trap { my $code = shift; my $temp_fh = tempfile(); my $saved_fh = select($temp_fh); eval($code); select($saved_fh); if ($@) { confess("Evaling \n'$code'\ngave error $@"); } seek($temp_fh, 0, 0); wantarray ? <$temp_fh>: join '', $temp_fh; }
The point being, of course, that the error handling is a Good Thing to add, you now have a piece of reusable functionality which does not assume that STDOUT had been the previous selected default filehandle, and in your code wouldn't you rather see:
my $output = eval_print_trap("print 'Waka waka!'");
than all of the garbage it required?

Caveat programmer: Perl's select will only redirect prints to the default output from Perl code. You will not trap error output, and you will not trap any printing that comes within C code or other programs. Good luck if you plan to call system.

Replies are listed 'Best First'.
Re: Re (tilly) 1: codeopen(INPUT, "&lt;INPUT") gives INPUT opened only for output
by danger (Priest) on Nov 16, 2001 at 23:23 UTC

    An alternative is to avoid the tmpfile altogether and tie an IO::Scalar for capturing the output. We can also localize STDOUT and STDERR (rather than select) and provide a simple __WARN__ sig handler to make up for some of the mentioned caveats:

    #!/usr/bin/perl -w use strict; use IO::Scalar; use Carp; my $out = eval_print_trap(<<'EOC'); print "Hello World\n"; print STDOUT "Hello again\n"; print STDERR "stderr is here too\n"; print "this uninit warning is captured:$a\n"; warn "and explicit warnings as well"; system(q|echo "Can't have everything!"|); EOC print "***\n$out***\n"; print "done\n"; sub eval_print_trap { my $code = shift; my $string; my $SH = IO::Scalar->new_tie(\$string); { local *STDOUT = $SH; local *STDERR = $SH; local $SIG{__WARN__} = sub{print @_}; eval($code); } if($@){ confess "Evaling\n'$code'\ngave error $@"; } return $string; } __END__ ### could also have this at the end: seek($SH,0,0); wantarray ? <$SH> : $string;
      Yeah I should have mentioned I didn't want to use IO::Scalar; at least File::Temp is standard in 5.6.1 and there is a fall back which is always available.

      -- perl -p -e "s/(?:\w);(<A HREF="/index.pl?node=st&lastnode_id=2437">st< +/A>)/'\$1/mg"
Re: Re (tilly) 1: open(INPUT, "&lt;INPUT") gives INPUT opened only for output
by belg4mit (Prior) on Nov 16, 2001 at 22:26 UTC
    tilly I am using File::Temp, that's where tmpnam comes from (with a fallback to POSIX).

    I should point out that the code in the eval will be unknown, not wirtten by me no idea what's in it. Also, I have already dupped STDERR to STDOUT.

    I would prefer to use tmpfile; even if I can't fallback then; but since I'm trying to *replace* STDOUT (for those who explicitly print to it, which as you pointed out select will do nothing about). But I as unable to dup the filehandle to STDOUT. I tried doing a

    my $TRAP = tmpfile(); $no = fileno($TRAP);
    on it and opening as open(STDOUT, ">&$fileno"); as well as open(STDOUT, ">&=$fileno"); Neither of which worked so I figured I had to roll my own.

    -- perl -p -e "s/(?:\w);(<A HREF="/index.pl?node=st&lastnode_id=2437">st< +/A>)/'\$1/mg"