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

Re: [PATCH] %open::modes to hold ${^OPEN} values for run-time access

Thread Previous | Thread Next
From:
Rafael Garcia-Suarez
Date:
January 29, 2003 04:18
Subject:
Re: [PATCH] %open::modes to hold ${^OPEN} values for run-time access
Message ID:
20030129131826.636fc847.rgarcia@hexaflux.com
I wrote:
> Autrijus Tang <autrijus@autrijus.org> wrote:
> > The attached patch implements the %open::modes hash, which holds
> > the compile time values for IN and OUT layers for run-time access.
> 
> Ah, the old problem, run-time access to compile-time hints.
> 
> I object to your solution, (at least for 5.9.0), because it doesn't work
> in edge cases, and it's needed only because you don't have run-time
> access to the compile-time values of %^H.
> 
> A better solution would be :
> 
> 1- store the info you need in %^H (I notice that $^H{open_IN} and
>    $^H{open_OUT} are already used)

Currently this info is stored in COP.cop_io (a.k.a. ${^OPEN}, a variable
that has the same scoping properties as $^H.) If it's going to be stored
in %^H, we could get rid of this field in the COP struct.

> 2- provide some mechanism to access at run-time the compile-time
>    values of %^H.

A solution suitable for 5.8.x perls is to write a little module that
reads the value of cop_io for the currently executing statement.
Like this :

#!perl -l
use strict;
use warnings;
use Inline C => <<'**C**';
SV *getOPEN() {
    return SvREFCNT_inc( PL_curcop->cop_io );
}
**C**
$_ = getOPEN(); s/\0/-/; print;
use open ':utf8';
$_ = getOPEN(); s/\0/-/; print;
use open IN  => ":crlf";
$_ = getOPEN(); s/\0/-/; print;
use open OUT => ":bytes";
$_ = getOPEN(); s/\0/-/; print;
__END__

The function getOPEN() here returns the value of ${^OPEN}
under which it has been compiled. Is that the info you want, and
is it suitable for you ? (I'm not sure why I've put a SvREFCNT_inc
here, I'll have to look.)

Here's what this script outputs :
$ perl runtimeopen.pl
Use of uninitialized value in substitution (s///) at runtimeopen.pl line 9.
Use of uninitialized value in print at runtimeopen.pl line 9.

:utf8-:utf8
:crlf-:utf8
:crlf-:bytes

If you (and others) find this snippet useful, I can release it
to CPAN. (I've *no idea* about a proper module name for it.)

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About