Question

I have a written a script that uses different modules. The script a loop, returning to the beginning until it reaches a certain id and the dies.

But sometimes, the result of a module query is undefined and the script stops. In my current case:

Can't use an undefined value as a HASH reference at /modulepath/module.pm line xxx

How can I tell Perl, not to stop and skip the current processing? I tried to use the follwing without success:

if (defined $var->movie( id => $imdb )){ 
#do the usual stuff 
}
else {
#skip and do nothing
}
Was it helpful?

Solution

Use eval to keep code from aborting your script.

eval {
  # code that might blow up
};
if ($@) { 
  # it blew up, but we're still running
} 

OTHER TIPS

Check if $var is defined too.

if (defined $var && defined $var->movie( id => $imdb )){ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top