For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > Redirecting file handler to STDOUT









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Redirecting file handler to STDOUT
Ben Edwards

2007-06-22, 7:59 am

I am opening a log file:

open( LOGFILE, ">>cronlog.txt" );

This is being written to in lots of places.

I have been asked to change the program so if -m (manual) flag is
passed the stuff that goes to the log file is send to standard out
instead. Is it possible to change the above command to redirect
LOGFILE to STDOUT (i.e. make the two the same thing.

i.e.

if ( defined( $opt_m ) ) {
open( LOGFILE, STDOUT );
} else {
open( LOGFILE, ">>cronlog.txt" );
}

I even tried:

LOGFILE = STDOUT;

But I get:

Bareword "STDOUT" not allowed while "strict subs" in use

Whjenever I try to use STDOUT;(

Any ideas?
Ben
--
Ben Edwards - Bristol, UK
If you have a problem emailing me use
http://www.gurtlush.org.uk/profiles.php?uid=4
(email address this email is sent from may be defunct)
Xavier Noria

2007-06-22, 7:59 am

On Jun 22, 2007, at 11:57 AM, Ben Edwards wrote:

> I am opening a log file:
>
> open( LOGFILE, ">>cronlog.txt" );
>
> This is being written to in lots of places.
>
> I have been asked to change the program so if -m (manual) flag is
> passed the stuff that goes to the log file is send to standard out
> instead. Is it possible to change the above command to redirect
> LOGFILE to STDOUT (i.e. make the two the same thing.


Given those requirements, a simple solution would be:

unless (m_flag_given) {
open my $fh, '>>cronlog.txt' or die $!;
select $fh;
}

# the rest of the program uses regular prints

-- fxn

Paul Lalli

2007-06-22, 7:59 am

On Jun 22, 5:57 am, funkyt...@gmail.com (Ben Edwards) wrote:
> I am opening a log file:
>
> open( LOGFILE, ">>cronlog.txt" );
>
> This is being written to in lots of places.
>
> I have been asked to change the program so if -m (manual) flag is
> passed the stuff that goes to the log file is send to standard out
> instead. Is it possible to change the above command to redirect
> LOGFILE to STDOUT (i.e. make the two the same thing.
>
> i.e.
>
> if ( defined( $opt_m ) ) {
> open( LOGFILE, STDOUT );} else {
>
> open( LOGFILE, ">>cronlog.txt" );
>
> }
>
> I even tried:
>
> LOGFILE = STDOUT;
>
> But I get:
>
> Bareword "STDOUT" not allowed while "strict subs" in use
>
> Whjenever I try to use STDOUT;(


Because this is the equivalent of saying
"LOGFILE" = "STDOUT";
which obviously makes no sense, so by using strict such things are
prevented.

You can however assign the typeglob *LOGFILE to *STDOUT:

if (defined( $opt_m) ) {
*LOGFILE = *STDOUT;
}

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com