Jamie Risk wrote:
> I'm a casual PERL programmer at best, and I have a working facsimile
> of the non-working code below. When I run it, I get an error
> "print() on closed filehandle $fh at ./test.pl line [n]".
>
> This is just my first step to being able to pass file handles to my
> sub-routines. What have I missed?
>
> open my $fh, "test" || die $!;
You have a precedence problem. Perl sees this as:
open(my $fh, ("test" || die $!));
Since "test" is boolean true at compile-time, the RHS is optimized away to:
open(my $fh, "test);
You can verify this by running your program as:
perl -MO=Deparse,-p myscript.pl
You need to use either:
open(my $fh, "test) || die $!;
or
open my $fh, "test" or die $!;
> print $fh "Hello!\n";
> close $fh;
>
> Secondly, how do I pass STDOUT has a file handle to a sub-routine?
foo(\*STDOUT);
sub foo {
my $fh = shift;
print $fh "Hello, World\n";
}
See:
perldoc -q 'pass/return'
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]