Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirecting STDOUT

Status
Not open for further replies.

columb

IS-IT--Management
Feb 5, 2004
1,231
0
0
EU
I'm trying to amend someone elses script and having problems with a bit that redirects STDOUT. In particular I want to be able to switch off the redirect.

The script looks like
Code:
    $REPORT = "$SECURE/result.$$";  # global!
    open (REPORT, ">$REPORT") || die "can't create $REPORT: $!\n";

    # assume dups work
    if ( $SHOW_OUT == 0 )
      {
      open (STDOUT, ">&REPORT");
      open (SAVERR, ">&STDERR");
      open (STDERR, ">&STDOUT");
      }
I went on the CPAN doucmentation site and cut and pasted their example from the 'open' section into a test script which looks like
Code:
#!/bin/perl -w
use strict;

open my $oldout, ">&STDOUT"     or die "Can't dup STDOUT: $!";
open STDOUT, ">col.out" or die;
print "To file\n";
open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!";
print "To Screen\n";
but when I run the test script I get
Code:
g03201# ./test.pl
Unknown open() mode '>&' at ./test.pl line 7.
I've read TFM, and even cut and pasted from it. What am I doing wrong?

Thanks

Ceci n'est pas une signature
Columb Healy
 
not sure if this is the case but file operators for read, write etc this link might help


normally I use >+ not >&


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I've done something before along the lines of this...

I was making a command for my AIM bot that would execute a system() command (of course, this could only be used by me), but I wanted to get everything system() printed out, not just the return code.

Code:
open (LOG, ">stdout.txt");

select LOG;
system ("ls -hal");
select STDOUT;

close (LOG);

# then re-open it
open (READ, "stdout.txt");
my @messages = <READ>;
close (READ);

In other words, `select` a different filehandle and print should write to it.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Here is an example of how I handle such things ...

$ROOT and $FILE_CLASSES are defined in my lib.pm file.
All of my recordsets are "|" deliminated

$FILE_NAME = $FILE_CLASSES;
open (DATABASE, "<$root/$FILE_NAME") or
die("MESSAGE 100:<br>Unable to find [$FILE_NAME] Data File.");
flock (DATABASE, 2);
@data = <DATABASE>;
close(DATABASE);

$PrintData = "";
foreach (@data)
{
($var1, $var2, $var3) = split (/\|/);
$PrintData .= "$var1";
}

- or -

$PrintData = "";
foreach $thisrow (@data)
{
($var1, $var2, $var3) = split (/\|/, $thisrow);
if ($var1)
{
$PrintData .= $thisrow;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top