Wow, that is kind of fun to try and pick apart.  But, try this (untested
of course):

if($LogLogfile eq "YES") {
  print "Logging stuff to log file...\n";
  { # Used for scoping STDOUT and STDERR redirection
    open(STDOUT, ">$PathLog") or die "Canoot redirect STDOUT: $!";
    open(STDERR, ">&STDOUT") or die "Cannot redirect STDERR: $!";
    # Code using the new STDOUT and STDERR
  } # After this STDOUT and STDERR will be back to default
}

This will point STDOUT to the file, STDERR to STDOUT - which in turn
will point it to the file as well.  :o)

I don't think (I'm not sure though) that your "backup" of the STDOUT and
STDERR will work, because you are redirecting STDERRBACKUP to STDERR,
and then redirecting STDERR to the file.  So I would think STDERRBACKUP
would also point to the file, but like I said I'm not sure of this.
Perhaps one of the experts on the list will be able to shed some light
on this topic.

Brian Johnson
Partner/Systems Administrator/Programmer
Source1Hosting.tv, LLC (www.source1hosting.tv)
Source1Results.com, LLC (www.source1results.com)
I may be insane, but remember - The only
difference between an insane man and a
genius is his jacket.

> so how does one redirect both STDERR and STDOUT to $file, do 
> some stuff, and
> then get stderr and stdout back and playing nice?
> 
> I'm trying:
> 
>         # if $LogLogfile="YES"
>         if ( $LogLogfile eq "YES" ) {
>         print "Logging stuff to log file...\n";
>         # redirect stderr --> stdout.
>         open(STDERRBACKUP, ">&STDERR");
>         open(STDERR, ">&STDOUT") or die "Problem redirecting STDERR.";
>         # redirect stdout to file
>         open(STDOUTBACKUP, ">&STDOUT");
>         open(STDOUT, ">$PathLog") or die "Problem redirecting 
> STDOUT.";
>         # execute the boatload of stuff we have to...
>         print "\n";
>         print $CommandLineString ;
>         print "\n";
>         system ( $CommandLineString ) ;
>         print
> "#############################################################
> ##############
> ##\n";
>         SetThingsRight();
>         # get stdout back
>         close (STDOUT);
>         open (STDOUT, ">&STDOUTBACKUP");
>         # get stderr back from stdout.
>         close (STDERR);
>         open(STDERR, ">&STDERRBACKUP");
>         }
> 
> which is probably unnecessarily messy and plumb doesn't work. 
> it's giving:
> 
> Logging stuff to log file...
> Uncaught exception from user code:
>         Problem redirecting STDOUT. at 
> ./hierarchicalcommandlinetester.pl
> line 119.
> 
> ---
> 
> From: Brian [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, July 17, 2001 11:31 AM
> To: [EMAIL PROTECTED]
> Subject: RE: Redirection of STDERR
> 
> 
> If you need it restored within the sub itself, you can also throw
> scoping brackets around it.  i.e. do what Paul suggested, only:
> 
> Sub func {
>   # Some code here
>   {
>     open STDERR, ">$file" or die $!;
>     # Code for the new STDERR here
>   }
>   # STDERR is back to normal here
>   # More code
> }
> 
> Remember, scoping brackets are you friend.  :o)
> 
> Brian Johnson
> Partner/Systems Administrator/Programmer
> Source1Hosting.tv, LLC (www.source1hosting.tv)
> Source1Results.com, LLC (www.source1results.com)
> I may be insane, but remember - The only
> difference between an insane man and a
> genius is his jacket.
> 
> >
> > --- Roland Schoenbaechler <[EMAIL PROTECTED]> wrote:
> > > In a sub of my script I redirected STDERR in a file. How can I
> > > redirect the STDERR back to the default (screen) for the following
> > > subs?
> >
> > Use local(). It's not something to make a big habit of, but 
> this is a
> > good place for it.
> >
> >  sub func {
> >     local *STDERR;
> >     open STDERR, ">$file" or die $!;
> >     # code here
> >  }  # STDERR gets "fixed" when the local() scope ends here
> >
> > c.f. perldoc -f local
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Get personalized email addresses from Yahoo! Mail
> > http://personal.mail.yahoo.com/
> >
> > --
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> 
> 
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to