in reply to Redirecting STDOUT, lexically scoped

print "Hello world\n"; { local(*STDOUT); open(STDOUT, ">some_other_world.txt") or die $!; print "Hi other world\n"; close STDOUT; } print "Goodbye world\n";
- Miller

Replies are listed 'Best First'.
Re^2: Redirecting STDOUT, lexically scoped
by moritz (Cardinal) on Jul 31, 2007 at 10:12 UTC

      Do you understand why? The leading * is there because you're localizing the typeglob. As a result, you'll also effectively localize any $STDOUT, @STDOUT, or %STDOUT you may have defined.

      Not likely to be a problem, but something that bit me at least once as a novice... ;-)

      <radiant.matrix>
      Ramblings and references
      The Code that can be seen is not the true Code
      I haven't found a problem yet that can't be solved by a well-placed trebuchet
        You don't have to clobber the whole typeglob. Read about the "*foo{THING} notation, and consider this:
        use strict; use warnings; open my $a, ">", "$ENV{HOME}/blahblahblah"; my @STDOUT = (0, 1, 2, 3); { local *STDOUT; print "It doesn't matter, because you'll get a warning\n"; } { local *STDOUT = $a; @STDOUT = ('a', 'b', 'c', 'd'); print "Inside Block @STDOUT\n"; } print "Outside Block\n";