tfoertsch has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I know about eval "string" and do "filename.pl"

Now I have a stream that produces data blocks, something like a function that returns one data block per call. I don't want to store all the data in memory to do a string eval. So I thought of something like

do sub {...} # the function returns one data chunk per call
(I know this does something else and is deprecated)

So, how about this?

do FILEHANDLE # FILEHANDLE is opened with a special PerlIO # layer that calls sub {...} on read.
Is that possible?

Thanks,
Torsten

Replies are listed 'Best First'.
Re: do FILEHANDLE ?
by tilly (Archbishop) on Mar 10, 2009 at 15:29 UTC
    Sanity check. If the data is too big to hold in memory before you eval it, how are you ever going to hold the compiled version after you eval it?

    Perhaps you should think your design through more carefully.

Re: do FILEHANDLE ?
by repellent (Priest) on Mar 10, 2009 at 16:38 UTC
    There was a thread on this. And here's the trick hack:
    sub do_fh (*) { # make use of @INC my $FH = shift() or return undef; unshift(@INC, sub { shift(@INC) and return $FH }); $@ = ""; my @ret = do $FH; # remove %INC side-effect no warnings; delete $INC{$FH}; return @ret; }
Re: do FILEHANDLE ?
by rovf (Priest) on Mar 10, 2009 at 11:29 UTC
    do FILEHANDLE # FILEHANDLE is opened with a special PerlIO # layer that calls sub {...} on read.
    I can't imagine that this will work. The expression after do is expected to be a file name, so in the unlikely event that Perl would do something useful in this case, it would be an undocumented feature.

    Maybe it would make more sense to drop do alltogether and instead write your own IO layer? See for instance PerlIO.

    -- 
    Ronald Fischer <ynnor@mm.st>