Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^5: disable functions if module not installed

by almut (Canon)
on Nov 03, 2008 at 23:17 UTC ( #721242=note: print w/ replies, xml ) Need Help??


in reply to Re^4: disable functions if module not installed
in thread disable functions if module not installed

produces out of memory too

This doesn't make much sense, as the eval { ... } is essentially also just loading the module (or trying to), only with fatal errors trapped.

I have : XP, 2Gb ram, perl 5.10, IM 6.4.4-2, PerlMagick

This is your web server machine, is it?


Comment on Re^5: disable functions if module not installed
Download Code
Re^6: disable functions if module not installed
by lepetitalbert (Monsignor) on Nov 03, 2008 at 23:55 UTC

    hi almut,

    It's my notebook.

    Ok after a reboot

    #!/usr/bin/perl -w use strict; use warnings; eval { require Image::Magick }; my $is_Magick = $@ ? 0 : 1; print "Content-type: text/html; charset=ISO-8859-1\n\n"; print "<h1>$is_Magick</h1>";

    works.

    Found what send me out of memory : a use lib statement ?!

    Is that expected behaviour ?

    Time to sleep.

    Thanl you all :)

    Have a nice, nice day !

    "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

      Hi Monks,

      going nuts !

      #!/usr/bin/perl -w use strict; use warnings; use diagnostics; eval { require Image::Magick }; my $is_Magick = $@ ? 0 : 1; print "Content-type: text/html; charset=ISO-8859-1\n\n"; print "<h1>$is_Magick</h1>";

      which finally works on my XP box, now gives me

      Deep recursion on subroutine "Image::Magick::Autoload"

      and an out of memory ! on a debian server

      has anyone a real working example ? an idea ? anything ? I cannot believe this can't be done !

      thanks

      Have a nice day !

      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

        AUTOLOAD is going into an infinite loop trying to reflect a constant, eating up the entire "C" stack. That's the memory you're running out of.

        I'm curious as to what is being autloaded. I'd add a print statement to Image/Magick.pm to print out $constname.

        The immediate cause of the problem is probably $! =~ /Invalid/ returning false when it is intended to return true. I'd add a print statement to Image/Magick.pm to print out (0+$!).":$!".

        Of course, that's assuming your version of Image::Magick is anything like the latest one. You haven't provided any version info.

        By the way,
        eval { require Image::Magick };
        my $is_Magick = $@ ? 0 : 1;
        is simpler and safer when written as
        my $is_Magick = eval { require Image::Magick; 1 };

        Endless recursion would occur, if constant() is not being found - for whatever reason... Normally, the routine is provided by the XS code, but in case the wrong shared library is being loaded, or the symbol isn't exported properly, or something like that, the call to constant() would be routed to AUTOLOAD itself, leading to the next level of recursion...

        Quick proof of concept code:

        #!/usr/bin/perl package Image::Magick; sub AUTOLOAD { # This AUTOLOAD is used to 'autoload' constants from the constant( +) # XS function. If a constant is not found then control is passed # to the AUTOLOAD in AutoLoader. my $constname; ($constname = $AUTOLOAD) =~ s/.*:://; print STDERR "$constname "; # DEBUG my $val = constant($constname, @_ ? $_[0] : 0); # the following code from the original sub is irrelevant (never re +ached) #if ($! != 0) { # if ($! =~ /Invalid/) { # $AutoLoader::AUTOLOAD = $AUTOLOAD; # goto &AutoLoader::AUTOLOAD; # } # else { # my($pack,$file,$line) = caller; # die "Your vendor has not defined PerlMagick macro $pack\: +\:$constname, used at $file line $line.\n"; # } #} #eval "sub $AUTOLOAD { $val }"; #goto &$AUTOLOAD; } package main; Image::Magick::not_there(); # starts endless recursion

        I'm not sure why lepetitalbert's respective print statement doesn't print anything, but maybe it's just that STDOUT isn't connected to the terminal here, or some such...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-12-18 18:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls