-1

The concept: A library is placed in my public_html folder, it's purpose to decode a string. But if that library is NOT present, to stop with an error. The way that was suggested before was:

eval("require x.pm");

if ($@) {
    print "The library is missing";
}
else{

    BEGIN{
        eval{"use x"};
    }

    1123131323fsdfsfsdfsfgdfgdfgghfghfghfgheterterter ...
    no x;
}

If the library is present, the decode works fine, and everything is "Hunky Dory". IN THEORY ... if the file is NOT present, the top loop should print an error ... except all that happens is I get an Error 500 as the script sees the encrypted string, and decides it's not a valid program.

Wondered if it was the "$@" so I tried it with flags

$set=0;
$set=1 if eval("require x.pm");

if ($set eq 0){
...
..

Same thing. Tried putting the code into a string:

$_="1123131323fsdfsfsdfsfgdfgdfgghfghfghfgheterterter ..."

That didn't help. So can anyone throw any light on what I'm doing wrong?

Borodin
  • 123,915
  • 9
  • 66
  • 138
  • I'm not clear what you expect perl to do with `1123131323fsdfsfsdfsfgdfgdfgghfghfghfgheterterter ...`. Regardless of what modules are loaded, the program still has to compile. Decoding a string involves passing the encoded string to a decoding subroutine. Also, the `BEGIN{ eval{"use x"}; }` will be done at *compile time*, before the `require` and independently of the conditional statement you have put it into – Borodin Apr 08 '16 at 13:08
  • *"If the library is present, the decode works fine"* I can't imagine what may be in your module `x` to get that code to compile. And `no x` won't do anything useful unless your module has an `unimport` method – Borodin Apr 08 '16 at 13:15
  • 1
    That is *not* the way that Sobrique and bolav [suggested](http://stackoverflow.com/a/35651339), so please don't claim that it is. Did you read bolav's comment? The BEGIN block will be executed unconditionally at compile time, before anything else. – ThisSuitIsBlackNot Apr 08 '16 at 13:18
  • 1
    I didn't realise that this was a follow-up question. I note that you say ***"At present - until advised to the contrary - I have made a "mash" of the two. ideas"*** and [**bolav** replies](http://stackoverflow.com/questions/35651264/show-error-if-pm-library-doesnt-exist/35651339#comment58998412_35652980) ***"This is not advisable ..."*** Please take heed of the advice you are given. It's sounding like all of this is about making your Perl code unreadable, which is always a futile venture and can only make your own job harder – Borodin Apr 08 '16 at 13:46

1 Answers1

-1

It doesn't look like you exit in any fashion. Here's a stable way to use eval() to do what you want, with an exit(). $ok won't be set to a true value if the require() fails.

my $ok = eval {require 'nofile.pm'; 1};

if (! $ok){
    print "module not found\n";
    exit;
}
stevieb
  • 8,323
  • 3
  • 24
  • 34
  • Sorry, in my haste at typing, I left the exit out from below the "library is missing" line – Cristofayre Apr 08 '16 at 12:41
  • Nope, sorry ... Still no go. It still comes up as "syntax error at ../program_name.cgi line 23, near "1123131323fsdfs ..." "no" not allowed in expression at line 24 ie it's looking through program, and finds the letters not assigned to a variable, and quits. Yet it the library IS present, it doesn't throw a wobbly and decodes the value!! Weird – Cristofayre Apr 08 '16 at 12:47
  • You're going to have to show us more code. Your first example isn't even valid Perl, and assigning to `$_` in your second example isn't typically standard. Put `use warnings;` and `use strict;` at the top of the script if it isn't already there as well. – stevieb Apr 08 '16 at 13:04