[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Bug or Limitation: "Filehandles" in pp_require



Hi,

in the course of events, I found myself needing to dynamically append 
some code to
an existing file, before executing it. The 'usual' solution of an @INC 
hook came to
mind. However, not every hook behaved in the way i expected. Given this 
code:

	$ perl -Mx -e'use ExistsNot'

I use x.pm to set up an @INC handler as follows, to allow ExistsNot to 
be loaded:

     ### file x.pm ###
     BEGIN {
         use IO::String;
         use Tie::Handle;
         use Devel::Peek;
         use PerlIO;
         use File::Temp  qw[tempfile];

         my $Str = "package ExistsNot; 1;\n";

         unshift @INC,
             sub {   warn "Code ref 1 -- IO::String\n";
                     my $io = IO::String->new;
                     $io->print( $Str );
                     #Dump( $io );
                     $io->setpos(0);
                     return $io;
             },
             sub {   warn "Code ref 2 -- Tie::Handle\n";
                     package X;
                     @ISA = qw[Tie::StdHandle];
                     sub READLINE  { $Str };
                     tie *FH, 'X';
                     #Devel::Peek::Dump( \*FH );
                     return \*FH;
             },
             sub {   warn "Code ref 3 -- PerlIO";
                     my $dummy = "";
                     open my $fh, '>', \$dummy   or die $!;
                     print $fh $Str              or die $!;
                     seek $fh, 0, 0;
                     #Dump( $fh );
                     return $fh;
             },
             sub {   warn "Code ref 4 -- File::Temp\n";
                     my($fh,$name) = tempfile();
                     print $fh $Str;
                     close $fh;
                     open my $fh2, $name or warn $!;
                     #Dump( $fh2 );
                     return $fh2;
             },
             sub {   die "Code ref 5 -- failed to load\n" },
     }

     1;


It turns out that only code ref 3 and 4 are able to produce a 
filehandle accepted by
pp_require, whereas 1 and 2 will not. Looking at the pp_require code in 
pp_ctl.c, i see
the following:

     else {
         if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {
             PerlIO_close(IoOFP(io));
         }
         IoIFP(io) = Nullfp;
         IoOFP(io) = Nullfp;
     }

which looks like a direct access to the file, rather than using any 
magic or interface
that's been defined for this value. Not quite what i expected, and not 
documented it seems.

Should this be fixed, or noted as a known shortcoming in some relevant 
pod file?


--
	Jos Boumans

	'Real programmers use "cat > a.out"'

	CPANPLUS	http://cpanplus.sf.net


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]