Click to See Complete Forum and Search --> : Easy Perl Question - Redirection?


pwharff
05-14-2004, 11:11 PM
I know that when I'm scripting in BASH, I redirect a variable to another file by doing this:


echo $username >> log


I thought I could be smart and try to figure it out on my own with perl, with this:


print $username >> log;


But I was wrong, all it does is prints "0" and never even redirects into the "log" file. Any help or thoughts?

chrism01
05-15-2004, 12:33 AM
basically, you have 2 options:

1: Just use
print $username
and redirect the (STDOUT) output of the script eg;
myscript.pl > file.log

2: Use the open cmd in Perl to open a filehandle for the outputfile eg:
<code>
open(SINK, "> $path")
or die "Couldn't open $path for writing: $!\n";
</code>
You can either print to this directly, or redirect STDOUT (& STDERR) to the file eg.
<code>
open(STDOUT, ">foo.out");
</code>

HTH
PS checkout the Perl cookbook avail online: http://iis1.cps.unizar.es/Oreilly/perl/cookbook/