develooper Front page | perl.perl5.porters | Postings from June 2003

Re: [ FW: perlio: OPEN() in PerlIO:: via:: modules]

Nick Ing-Simmons
June 30, 2003 02:19
Re: [ FW: perlio: OPEN() in PerlIO:: via:: modules]
Message ID:

>Hi Jarkko,

you would be better off sending PerlIO questions to me, 
or to perl5-porters.

>I am looking for an example of a :via() IO layer which implements
>the OPEN() function. Unfortunately none of the modules in the PerlIO::via
>namespace available on CPAN does this.

It is going to be a rare thing to do.

>I thought of a layer which always opens the same file no matter what the 
>user asked for. Thus
>    open my $fh, "<:via(ats)", "file" or die $!;
>    print <$fh>;
>    close $fh;
>should always print the same (ats = always the same). I tried variations of
>    package PerlIO::via::ats;
>    sub PUSHED { bless {}, shift }
>    sub OPEN
>    {   my $this = shift;
>        # --- do a real open ---
>        open my $handle, "theFileIlike" or die $!;

die is IMHO not the right way to exit an OPEN handler ...
It should return false (e.g. undef) 

>        # --- remember and return handle ---
>        $this->{fh} = $handle;
>        return $handle;
>    }
>    sub READ
>    {   my ($this, $buffer, $len, $fh) = @_;
>        read $fh, $buffer, $len;
>    }

What you want is 

    sub READ { 
     return read $_[0]->{fh}, $_[1], $_[2] 

That is read() from your saved FH, passing real buffer and requested length.

>    sub POPPED { close $_[0]->{fh} }
>However I cannot manage to get any data read since READ() is never invoked
>with a handle. 

$_[3] is a file handle for layer _below_ yours (if any).
There isn't one in your case.

There might be for something like PerlIO::via::tar (say)
where lower layer was file handle of a 'tar' file and the layer
implemented opening a member of the archive.

>Reading from $this->{fh} instead of $fh in READ() doesn't 
>change anything.

Yes it does - but you need to combine that with the $_[1] real-buffer
fix you noted.

>Next I tried to have two :via layers where the lower one implements PUSHED()
>and OPEN() and the upper one implements PUSHED() and READ() (or FILL()).
>READ/FILL in the upper layer is then invoked with a handle 

i.e. the lower layer.

>but still no data
>is actually transfered.
>Can you provide me with an example how to do an OPEN() in a PerlIO::via::
>module or point me to a resource where I can find an instructive one?

Attached is your code converted to something that works by 
tweaking READ() as above.

>Thanks in advance for any hints,
>    Peter

Nick Ing-Simmons Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About