venkatesan_G02 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am using WWW::Mechanize to automate interaction with a web site. The user has to submit an order number and the web site will return a link to the order if the order exists.

My program is working fine under ideal conditions (i.e) if a link is returned by the web site.

But the program crashes if no link is returned by the web site and WWW::Mechanize returns " Can't call method "url" on an undefined value " error.

I read thru the documentation for the module in CPAN and it is mentioned that follow_link will return undef if no link is found.

I tested for undef and even then the program crashes.

Is there a way to overcome this problem and the program should output a user understandable error which i have give in the program.

Thanks in Advance!!!

  • Comment on WWW::Mechanize follow_link() crashes if no link is found

Replies are listed 'Best First'.
Re: WWW::Mechanize follow_link() crashes if no link is found
by arkturuz (Curate) on Sep 25, 2009 at 11:47 UTC
    It's seems to me it's a feature actually and not a bug. Quote from the Changes for version 1.51_03:
    $mech->follow_link() did not complain if a link could not be found, even with autocheck on. Now it does. Thanks, Flavio Poletti.
      Hi,

      First thanks for the reply.

      But, Pardon me for my ignorance. I didn't get your point.

      Is there any way to make my program move on with the next step rather than crashing at follow_link line?

      Thanks
        Is there any way to make my program move on with the next step

        If all else fails, you could trap the exception, i.e. wrap the call within eval { ... } — Something like this

        my $foo; # undefined eval { $foo->url(); # would 'Can't call method "url" on an undefined val +ue ...' }; if ($@) { # ... handle the exception print "error: ...: $@\n"; }
        Well, one way would be to always pass a valid link to be followed. The other would be, as is specified in the WWW::Mechanize docs, to set autocheck to 0. Although, this would silence all requests unless they're errors.
Re: WWW::Mechanize follow_link() crashes if no link is found
by merlyn (Sage) on Sep 25, 2009 at 14:30 UTC
    follow_link is just find_link followed by get. It can (and should) fatal if the link isn't found. If you don't want the fatal, then either separate the steps to verify the link is actually there, or wrap the call inside an eval.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.