Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

SelfLoader chokes on closed STDIN

by saintmike (Vicar)
on Nov 07, 2006 at 00:28 UTC ( [id://582528]=perlquestion: print w/replies, xml ) Need Help??

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

There's a handy module called SelfLoader in the perl core, which lets you hide functions in the DATA section and load them on demand:
#!/usr/bin/perl #sloader use SelfLoader; foobar(); __DATA__ sub foobar { print "foobar!\n"; }
Alas, there seems to be a bug with it. If you call the script above from another script via exec and close STDIN, as in
#!/usr/bin/perl #sloader-exec close(STDIN); exec "./sloader";
it fails:
main doesn't contain an __DATA__ token at ./sloader line 5
Anyone seen this before?

Replies are listed 'Best First'.
Re: SelfLoader chokes on closed STDIN
by liverpole (Monsignor) on Nov 07, 2006 at 00:38 UTC
    Hi saintmike,

    It works for me, on Windows XP, using ActiveState Perl 5.8.8.

    What O/S and version of Perl are you using?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      perl-5.8.8 on Linux.

        I'll try this out on a Linux system in a bit, but I've got to ask:  Is there a good reason you need to close <STDIN>?

        Update:  For the record -- this worked fine on Linux (RH9), but the version of Perl there is 5.8.6.


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: SelfLoader chokes on closed STDIN
by Tanktalus (Canon) on Nov 07, 2006 at 20:42 UTC

    Ugh. It's a bug in SelfLoader. Look for the line that says:

    croak("$callpack doesn't contain an __DATA__ token") unless fileno($fh);
    It should say:
    croak("$callpack doesn't contain an __DATA__ token") unless defined fileno($fh);
    Notice the "defined" test? In your system (and mine as well), when you close stdin (fileno 0), the C library reuses that file handle for the next usage - which is reading $0, which goes to *main::DATA. So the file number here is 0, which is false, and it croaks. What the author should have said is "check if there is a file number" - not that it's non-zero. (According to fileno, it returns undef if it's not open.)

    I suggest opening a bug against it and pointing to this thread. And then you can patch your own if you feel lucky ;-)

      Funny, I came to the same conclusion yesterday and got it fixed :).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://582528]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-19 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found