Passing tied filehandles 
Author Message
 Passing tied filehandles

I spent ages sorting out a bug in my code. I've cut all the code away to get
to the nitty-gritty. (By this I mean that the example is trivial, but the
original wasn't). The code is below and can be run as it stands.

There are 3 'examplettes'. They each do the following
 * Tie a file handle
 * Open
 * Sysread from the handle
 * Sysread from a 'copy' of the file handle

The TIEHANDLE, OPEN and READ functions just do simple prints so we can see
they have been executed. The output is printed below

---snip----
Q:\web\ro\test>perl -w tietest_odd.pl
TIEHANDLE called
OPEN called
READ entered
READ entered

TIEHANDLE called
OPEN called
READ entered

TIEHANDLE called
OPEN called
READ entered
---snip----

As you can see only the first example 'worked'. In examples 2 & 3 the second
sysread failed (trust me, it is the second that fails :). The example that
confuses me is the second. The code is:

tie *FIL2, OddTiedHandle::;
my $ptr2 = \*FIL2;
open (FIL2);
sysread(*FIL2,$buf,$blksize);
sysread(*$ptr2,$buf,$blksize);
print "\n";

The only difference between example 1 and 2 is that example 1 has open
(*FIL2). I do not see why the filehandler reference should work differently
just because the open is star-struck. Is this a bug in Perl or in my head?

Regards - Paul Cotter
www.powermagic.net

----code snip-----

package OddTiedHandle;
use strict;

sub TIEHANDLE {

   print "TIEHANDLE called\n";
   return bless \{},$class;

Quote:
};

sub READ {  print "READ entered\n" ; return 1; };

sub OPEN {  print "OPEN called\n"; }

my ( $len , $buf, $blksize) = (0,"",120,);

tie *FIL1, OddTiedHandle::;
my $ptr = \*FIL1;
open (*FIL1);
sysread(*FIL1,$buf,$blksize);
sysread(*$ptr,$buf,$blksize);
print "\n";

tie *FIL2, OddTiedHandle::;
my $ptr2 = \*FIL2;
open (FIL2);
sysread(*FIL2,$buf,$blksize);
sysread(*$ptr2,$buf,$blksize);
print "\n";

tie *FIL3, OddTiedHandle::;
*DIRECT = *FIL3;
open (*FIL3);
sysread(*FIL3,$buf,$blksize);
sysread(*DIRECT,$buf,$blksize);
---code Snip----



Wed, 08 Oct 2003 17:09:20 GMT  
 Passing tied filehandles

Quote:

> The only difference between example 1 and 2 is that example 1 has open
> (*FIL2). I do not see why the filehandler reference should work differently
> just because the open is star-struck. Is this a bug in Perl or in my head?

Looks like a bug to me.  You don't say what version of Perl you are
using.  Your test code works as expected in 5.6.0.  Does this bug
pre-date or post-date 5.6.0?

Quote:
> tie *FIL3, OddTiedHandle::;
> *DIRECT = *FIL3;
> open (*FIL3);
> sysread(*FIL3,$buf,$blksize);
> sysread(*DIRECT,$buf,$blksize);

In this case I would not be supprised that *DIRECT was not tied
because strictly speaking is it is the GLOB and not the filehandle
that is tied.

After all if I do:

tie $foo, OddScalar;
$bar = $foo; # $bar is not tied.

--
     \\   ( )
  .  _\\__[oo

 .  l___\\
  # ll  l\\
 ###LL  LL\\



Thu, 09 Oct 2003 21:50:25 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CURSOR HANDLE ERROR. HELP!!!

2. How do I call a DOS .bat-file from a pascal program? "Exec()" doesn't seem to support this.

3. Source of FDELAY.TPP fixing Runerror 200 on Pentium II ?

4. If you need help programing pascal...

5. Help needed with justifying txt

6. How to restore a filehandle tie correctly?

7. Tied Filehandles

8. Tie'd filehandle and the system() function

9. How to restore a filehandle tie correctly?

10. Tied filehandles and Storable

11. Tied filehandles

12. Tied filehandle question

 

 
Powered by phpBB® Forum Software