On 4/4/2004 11:34 AM, [EMAIL PROTECTED] wrote:

Hey!

I'm having some trouble passing a file handle from a script into a module as a function parameter. I'm passing it as a variable.

The program says that I'm trying to use an un-opened file handle.

Does anybody have any thoughts on this?

Here is some code I borrowed from one of the core module (don't rememeber which) that will let you work with any kind of file handle (or filename):


sub read {
  my $self = shift;
  my %args = @_;

croak "Error: No 'file' specified to method read()" unless $args{file};

local (*FILE);
my ($type, $needs_close);
if (($type = ref $args{file}) && $type =~ /^(?:GLOB|FileHandle|IO::\w+)$/) {
*FILE = *{$args{file}};
} elsif (($type = ref \$args{file}) && $type eq 'GLOB') {
*FILE = $args{file};
} elsif ($type eq 'SCALAR') {
open FILE, '<'.$args{file} or croak "Error: Can't open '$args{file}': $!";
$needs_close = 1;
} else { }


# Do something...

  close FILE if $needs_close;
}



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to