On Fri, Apr 27, 2001 at 12:32:47AM +1000, Ian Brayshaw wrote:
> Sometimes, however, no recompilation is necessary, and so I'd like
> to return a filehandle that "evaluates to true" (in the 'do <file>' sense of
> evaluates)
>
> Is there a way to do this without creating a dummy file (i.e. can we do this
> in memory)?
In bleadperl (and hence in the forthcoming 5.8) it's really easy to
do this. Just:
sub true_filehandle {
my $true = "1";
open (my $fh, "<", \$true);
return $fh;
}
In earlier perls, you'll need to make a tied filehandle:
package TrueHandle;
sub TIEHANDLE {
bless {};
}
sub READ {
my $self = shift;
return 0 if $self->{done} || $_[1] == 0;
substr($_[0], $_[2], 1) = "1";
$self->{done}=1;
return 1;
}
sub READLINE {
return if $self->{done};
$self->{done}=1;
return "1";
}
package main;
sub true_filehandle {
use Symbol;
my $fh = gensym();
tie *$fh, "TrueHandle";
return $fh;
}
.robin.