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

When run from the command line or the web, the following code works just fine but when run from cron I get this error... Can't call method "get_tag" without a package or object reference at line 122. Any suggestions to correct this are most appreciated!
$p = HTML::TokeParser->new(shift||"$file"); while (my $token = $p->get_tag("a")) { my $url = $token->[1]{href} || "-"; my $text = $p->get_trimmed_text("/a"); push @array, $url; }

Replies are listed 'Best First'.
Re: Why does cron kill this script??
by chipmunk (Parson) on Jan 07, 2002 at 02:56 UTC
    You haven't verified that HTML::TokeParser->new() succeeds, so you go on to call $p->get_tag() even though $p is empty.

    The call to HTML::TokeParser->new() is probably failing because the script cannot find the specified file, or the script doesn't have the appropriate permissions to open it.

Re: Why does cron kill this script??
by kschwab (Vicar) on Jan 07, 2002 at 05:25 UTC
    This sort of thing usually happens when the script writer makes too many assumptions about cron.

    The general environment that a script gets from cron isn't the same as the one you get when you log in.

    Things to check:

    • Does the script depend on being in a certain directory to run ? ( chdir() there explicitly in the script)
    • Does the script depend on certain environment variables ? cron doesn't read your ".profile" file. The PATH environment variable is a common problem.
Re: Why does cron kill this script??
by rbc (Curate) on Jan 07, 2002 at 05:42 UTC
    I think the error you are getting
    is the result of ...

    use strict;

    somewhere in your script or libs included by your
    script. So you need to 'maybe' say ...

    my $p = HTML::...

    Why this error manifest itself when executed
    from cron is probably do to the environment
    cron runs in vs. your command line env.
    ... as others have pointed out

    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.
      Actually, it is the result of a non-existing file:
      use strict; use HTML::TokeParser; my $p = HTML::TokeParser->new(shift); my $token = $p->get_tag("a"); [jeffa@trinity perl]$ ./bar.pl asdfsdaf Can't call method "get_tag" on an undefined value at ./bar.pl line 7.
      Not declaring $p with my yields:
      Global symbol "$p" requires explicit package name ...
      So i'd put my money on the bet that strict is not being used or $p was declared earlier. More likely getting home paths mixed up.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)