Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: require in script breaks module

by chrstphrchvz (Scribe)
on Mar 04, 2019 at 08:13 UTC ( #1230821=note: print w/replies, xml ) Need Help??


in reply to Re: require in script breaks module
in thread require in script breaks module

Thanks for the reply and suggesting possible workarounds.

Peeking inside the .ph file on my machines, it's mostly just more requires. So unfortunately neither do 'sys/ioctl.ph' nor delete $INC{'sys/ioctl.ph'}; require 'sys/ioctl.ph' alone yield any improvement.

I ended up moving the require to where it would be run once during the module's import, only nested in an eval so that any error is saved to a package variable. Then, when the feature requiring ioctl is used, any error will be croaked; and tests can check the package variable to know whether to skip. Here's the implementation: https://github.com/chrstphrchvz/perl-tcl-ptk/commit/75abce83f7

Maybe there is something more elaborate that resists the effects of require appearing in "user" code, but because this particular feature in the module is due for replacement (likely by something written in C/XS), I've opted for something low-effort and less invasive as it were.

Replies are listed 'Best First'.
Re^3: require in script breaks module
by haukex (Archbishop) on Mar 04, 2019 at 21:38 UTC
    Peeking inside the .ph file on my machines, it's mostly just more requires.

    Yes, the "load a file only once" logic with %INC would apply to every require call. I don't have enough time to test right now, but assuming that those files don't load any other modules, an ugly hack might be { package Foo; local %INC; require "..."; }

    By the way, I took a look at your commit, and I noticed this logic:

    eval { require 'sys/ioctl.ph' }; $Tcl::pTk::_FE_unavailable = $@; ... if ($Tcl::pTk::_FE_unavailable) { ...

    Be aware of Bug in eval in pre-5.14 and that $@ may not be a true value after an eval failure. The better pattern would be for example:

    eval { require 'sys/ioctl.ph'; 1 } or do { $Tcl::pTk::_FE_unavailable = $@||'unknown error'; };

    because in this case $Tcl::pTk::_FE_unavailable will always be a true value if an error occurs, even if $@ is not.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2022-09-27 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (118 votes). Check out past polls.

    Notices?