Using Eval Statement To Trap Module Not Present Fatal Error 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 9 post ] 

 Relevant Pages 

1. How to trap fatal errors w/ perlembed?

2. Trapping sendmail errors (with eval?)

3. Error Trapping with "die" and eval

4. eval() doesn't trap error - why?

5. make file fatal errors during module compilation -

6. nmake fatal error installing modules

7. DBI::DBD::Sybase not trapping Sybase error codes and messages

8. Symbol not found error when using Curses module

9. Symbol not found error when using Curses module

10. Trapping for system level error messages using system

11. trapping errors using $!

12. An Error with sockets using SNMP traps

 

 
Powered by phpBB® Forum Software