Author |
Message |
Lyvois #1 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Can someone please tell me what's wrong below. I am trying to trap a fatal error if a module is not installed when calling that module via a use statement but doing the eval statement first is not catching the error is the File::Find module is not present. # get our total disk space # we are using the File::Find module but just in case # it is not available on a server, we want to trap # a fatal error first. $result = eval("use File::Find");
my $disk_space = "Unable To Determine"; } else { # we can use it so let's get our disk space find(\&stat, "$cgi_path"); find(\&stat, "$public_path"); my $megabytes = $filesize / 1000000; $disk_space = sprintf("%.02f",$megabytes); $disk_space .= " MB"; } sub stat { $filesize += (stat("$_"))[7]; Quote: }
Thank you.
|
Sun, 25 Sep 2005 17:25:50 GMT |
|
 |
Tassilo v. Parseva #2 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Also sprach Lyvoise: Quote: > Can someone please tell me what's wrong below. I am trying to trap a fatal > error if a module is not installed when calling that module via a use > statement but doing the eval statement first is not catching the error is > the File::Find module is not present.
Actually I doubt that. Your code below should catch these cases. However, there are some things you should change in the code: Quote: > # get our total disk space > # we are using the File::Find module but just in case > # it is not available on a server, we want to trap > # a fatal error first. > $result = eval("use File::Find");
First of all, you are using string-eval here. It works but it is slower and not needed in this case. Block eval (along with require() instead of use()) would be the way to go. In the case of File::Find you should do that in a BEGIN block because some of the functions have a prototype that would otherwise be lost. So I'd suggest doing it that way: my $HAVE_FILE_FIND; BEGIN { eval { require File::Find };
File::Find->import; $HAVE_FILE_FIND = 1; } } Calling File::Find->import is necessary if you want this module to export its default symbols such as find() and finddepth().
if (! $HAVE_FILE_FIND) { Quote: > my $disk_space = "Unable To Determine";
Declaring and using the lexical in this if-branch is probably not what you want. $disk_space will disappear once you leave this block. You must probably declare $disk_space before entering the if-else blocks. Quote: > } else { # we can use it so let's get our disk space > find(\&stat, "$cgi_path"); > find(\&stat, "$public_path"); > my $megabytes = $filesize / 1000000; > $disk_space = sprintf("%.02f",$megabytes); > $disk_space .= " MB"; > } > sub stat > { > $filesize += (stat("$_"))[7]; > }
With the suggested changes, the rest of your script should work. Tassilo --
{ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
|
Sun, 25 Sep 2005 18:09:24 GMT |
|
 |
Gunnar Hjalmarsso #3 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Quote:
> Can someone please tell me what's wrong below. I am trying to trap > a fatal error if a module is not installed when calling that module > via a use statement but doing the eval statement first is not > catching the error is the File::Find module is not present. > <snip> > $result = eval("use File::Find");
The use pragma looks for a module at compile-time. Try this instead: eval { require File::Find }; / Gunnar -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
Sun, 25 Sep 2005 18:07:28 GMT |
|
 |
Tassilo v. Parseva #4 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Also sprach Gunnar Hjalmarsson: Quote:
>> Can someone please tell me what's wrong below. I am trying to trap >> a fatal error if a module is not installed when calling that module >> via a use statement but doing the eval statement first is not >> catching the error is the File::Find module is not present. >> <snip> >> $result = eval("use File::Find"); > The use pragma looks for a module at compile-time. Try this instead: > eval { require File::Find };
Not when wrapped in string-eval. It might well be the only situation where use() does not happen at compile time. Tassilo --
{ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
|
Sun, 25 Sep 2005 19:37:57 GMT |
|
 |
Gunnar Hjalmarsso #5 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Quote:
> Also sprach Gunnar Hjalmarsson:
>>> $result = eval("use File::Find"); >> The use pragma looks for a module at compile-time. Try this >> instead: >> eval { require File::Find }; > Not when wrapped in string-eval.
Didn't know that; thanks for correcting me! Quote: > It might well be the only situation where use() does not happen at > compile time.
My usual luck. :( / Gunnar -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
|
Sun, 25 Sep 2005 21:24:48 GMT |
|
 |
Benjamin Goldber #6 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
[snip] Quote: > First of all, you are using string-eval here. It works but it is slower > and not needed in this case. Block eval (along with require() instead > of use()) would be the way to go. > In the case of File::Find you should do that in a BEGIN block because > some of the functions have a prototype that would otherwise be lost. So > I'd suggest doing it that way: > my $HAVE_FILE_FIND; > BEGIN { > eval { require File::Find };
> File::Find->import; > $HAVE_FILE_FIND = 1; > } > }
I would write that as: use constant HAVE_FILE_FIND => eval { require File::Find; File::Find->import; 1; } || 0; That way, a block of code like: if( HAVE_FILE_FIND ) { .... } Gets optomized away, if File::Find wasn't found. --
|
Sun, 25 Sep 2005 22:11:40 GMT |
|
 |
Tassilo v. Parseva #7 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Also sprach Benjamin Goldberg: Quote:
>> my $HAVE_FILE_FIND; >> BEGIN { >> eval { require File::Find };
>> File::Find->import; >> $HAVE_FILE_FIND = 1; >> } >> } > I would write that as: > use constant HAVE_FILE_FIND => eval { > require File::Find; > File::Find->import; > 1; > } || 0; > That way, a block of code like: > if( HAVE_FILE_FIND ) { > .... > } > Gets optomized away, if File::Find wasn't found.
And it looks even less cluttered. Nice! Tassilo --
{ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
|
Mon, 26 Sep 2005 06:13:59 GMT |
|
 |
Anno Sieg #8 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Quote:
> > Also sprach Gunnar Hjalmarsson:
> >>> $result = eval("use File::Find"); > >> The use pragma looks for a module at compile-time. Try this > >> instead: > >> eval { require File::Find }; > > Not when wrapped in string-eval. > Didn't know that; thanks for correcting me! > > It might well be the only situation where use() does not happen at > > compile time. > My usual luck. :(
Strictly speaking, it *does* happen at compile time -- the time the string inside eval() is compiled. Since that compilation happens at run-time of the file, the effect is the same. Anno
|
Mon, 26 Sep 2005 10:08:56 GMT |
|
 |
Lyvois #9 / 9
|
 Using Eval Statement To Trap Module Not Present Fatal Error
Thanks everyone for the help. It correctly trapping all errors now. Very much appreciated. Lyvoise
Quote: > Can someone please tell me what's wrong below. I am trying to trap a fatal > error if a module is not installed when calling that module via a use > statement but doing the eval statement first is not catching the error is > the File::Find module is not present. > # get our total disk space > # we are using the File::Find module but just in case > # it is not available on a server, we want to trap > # a fatal error first. > $result = eval("use File::Find");
> my $disk_space = "Unable To Determine"; > } else { # we can use it so let's get our disk space > find(\&stat, "$cgi_path"); > find(\&stat, "$public_path"); > my $megabytes = $filesize / 1000000; > $disk_space = sprintf("%.02f",$megabytes); > $disk_space .= " MB"; > } > sub stat > { > $filesize += (stat("$_"))[7]; > } > Thank you.
|
Mon, 26 Sep 2005 21:41:23 GMT |
|
|