Forum Moderators: coopster & phranque

Message Too Old, No Replies

require command

         

branmh

12:41 pm on Sep 22, 2003 (gmt 0)

10+ Year Member



require "$script";

I have this in a small cgi file: Is there a way see if the $script varible is found, if not show a error instead of a 500 error.

moltar

1:40 pm on Sep 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You mean if the file exists?

if ( !(-e $script) ) {
print "Error";
}

branmh

2:02 pm on Sep 22, 2003 (gmt 0)

10+ Year Member



Okay when I add
if (!(-e $script) ) {
print "Error";
}

to my file I now get an error 500.

moltar

2:07 pm on Sep 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, this was just an example piece of code. You need to work around it.


if ( !(-e $script) ) {
print "Content-type: text/html\n\n";
print "Error";
exit(0);
} else {
require "$script";
}

branmh

2:14 pm on Sep 22, 2003 (gmt 0)

10+ Year Member



thanks

Storyteller

5:14 am on Sep 23, 2003 (gmt 0)

10+ Year Member



The proper Perl idiom for it is this:

eval "require '$script'";
print "Couldn't include $script: $!" if $!;

This handles include paths as well as various errors that may arise on "require".

dkubb

7:15 am on Sep 23, 2003 (gmt 0)

10+ Year Member



Storyteller, you probably meant to use $@ rather than $! here:

eval "require '$script'";

print "Couldn't include $script: $@" if $@;