Filehandles 
Author Message
 Filehandles

Here's a question about passing filehandles to functions in different
packages...

I have the following code:

---

(file db.pl)

package DB;

sub SpewNames {

    print $outfile "$school:\n";
...

Quote:
}

---

(file main.pl)

package main;

    if (! open(NamFile, ">${namFilename}")) {
        die "\n$0: Unable to open name output file: $namFilename\n";
    }
    &spewHeader('NamFile', $namFilename);
    &DB::SpewNames('NamFile', $school);

---

spewHeader (in main) works fine...
SpewNames (in DB) does not print to the file $outfile.
spewNames will work if I move it to main.pl.

What am I doing wrong here?

        -Fred
--
-----------------|----------------------------------------------------------
Wilfredo Sanchez | Massachussetts Institute of Technology

-----------------|----------------------------------------------------------



Thu, 01 May 1997 11:23:51 GMT  
 Filehandles



Quote:

>Here's a question about passing filehandles to functions in different
>packages...

[...]
>spewHeader (in main) works fine...
>SpewNames (in DB) does not print to the file $outfile.
>spewNames will work if I move it to main.pl.

>What am I doing wrong here?

The basic objects in perl are
PACKAGE SCALAR ARRAY HASH CODE FILEHANDLE DIRHANDLE FORMAT.

They all work very similar: they live in a package. Your filehandle
with the name `NamFile' lives in package main. As long as you are
playing with only one package, you don't need to worry about
qualifying your filehandles. But from one package to another you have
to.

In your example you passed the NAME of a filehandle to a subroutine
without qualifying it. The -w switch would have helped you to
recognize this:

~> perl -lwe 'package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
open FH, ">testfile";
print FH "from main";
FOO::testFH(FH,"going through a package");
'
Identifier "main::FH" used only once: possible typo at -e line 7.
Filehandle FH never opened at -e line 4.
~>

The message would certainly be more helpful as

    Filehandle FOO::FH never opened at -e line 4.

LARRY?

To answer your question. There are several ways to handle it:

1. You fully qualify your passed filehandle:
1a Direct use of filehandle

perl -le 'package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
open FH, ">testfile";
print FH "from main";
FOO::testFH(main::FH,"going through a package");
'

1b Indirect use of filehandle

perl -le 'package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
$softref=main::FH;
open $softref, ">testfile";
print $softref "from main";
FOO::testFH($softref,"going through a package");
'

2. You pass a GLOB

perl -le 'package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
open FH, ">testfile";
print FH "from main";
FOO::testFH(*FH,"going through a package");
'

3. You pass a reference

perl -le 'package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
$ref=\*FH;
open $ref, ">testfile";
print $ref "from main";
FOO::testFH($ref,"going through a package");
'

I think I like (1b) and (4) most. Unfortunately (4) comes with a
warning if use with the -w switch.

But if you have a POSIX system, then we have a very cute and clean
method in there:

5. POSIX!!

perl -wle '
use POSIX;
package FOO;
sub testFH {

   print $fh "$text";

Quote:
}

package main;
$ref= new FileHandle "testfile", "w";
print $ref "from main";
FOO::testFH($ref,"going through POSIX");
'

No open!! Thanks, Larry!

Hope that helps.
--andreas



Thu, 01 May 1997 17:34:43 GMT  
 Filehandles

Quote:

>The basic objects in perl are
>PACKAGE SCALAR ARRAY HASH CODE FILEHANDLE DIRHANDLE FORMAT.
>--andreas

Hmm. I'd like to know exactly what objects are what and how
to determine that in the program. Are these really objects
in the OO sense? Or did you mean types. I know this sounds like
nitpicking, but I'll give you an idea of what I mean:

#!perl
########################################
package FileHandle;
sub dib {'FileHandle'}
package main;
open(TT,"tt");
$a = TT;
print ref($a),"\n";           #GIVES ''
print $a->dib(),"\n";              #GIVES FileHandle
close TT;
########################################
package DirHandle;
sub dib {'DirHandle'}
package main;
opendir(UU,"..");
$a = UU;
print ref($a),"\n";           #GIVES ''
print $a->dib(),"\n";              #GIVES FileHandle
closedir UU;
########################################
package HASH;
sub dib {'HASH'}
package main;
$a = {};
print ref($a),"\n";           #GIVES 'HASH'
#print $a->dib(),"\n";             #would give error
########################################
package ARRAY;
sub dib {'ARRAY'}
package main;
$a = [];
print ref($a),"\n";           #GIVES 'ARRAY'
#print $a->dib(),"\n";             #would give error
########################################
package SCALAR;
sub dib {'SCALAR'}
package main;
$a = \"hi";
print ref($a),"\n";           #GIVES 'SCALAR'
#print $a->dib(),"\n";             #would give error
########################################
package CODE;
sub dib {'CODE'}
package main;
$a = sub {'tt'};
print ref($a),"\n";           #GIVES 'CODE'
#print $a->dib(),"\n";             #would give error
########################################
package DOB;
sub dib {'DOB'}
package Package;
sub dib {'Package'}
package main;
$a = DOB;
print ref($a),"\n";           #GIVES ''
print $a->dib(),"\n";              #GIVES DOB

__END__
--

If you only have a hammer, you tend to see every problem as a nail.
                -- Maslow



Sat, 03 May 1997 21:40:54 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ref doesn't return FileHandle for FileHandles?

2. print to filehandle when filehandle is a $var

3. ANNOUNCE: FileHandle::Rollback - FileHandle with rollback and commit

4. Redirecting filehandle to multiple filehandles

5. Benchmark: local filehandle vs. Filehandle.pm

6. global filehandle, dereferencing filehandle

7. perl5a10 bug: can't read from <$filehandle> after my($filehandle)

8. passing filehandles to a pre-forked child

9. passing the null filehandle to a subroutine

10. How to restore a filehandle tie correctly?

11. Filehandles writing to strings?

12. How to determine file type of a filehandle?

 

 
Powered by phpBB® Forum Software