When you run perl -e "Bla->new"
, you get this well-known error:
Can't locate object method "new" via package "Bla"
(perhaps you forgot to load "Bla"?)
Happened in a Perl server process the other day due to an oversight of mine. There are multiple scripts, and most of them have the proper use
statements in place. But there was one script that was doing Bla->new
in sub blub
at line 123 but missing a use Bla
at the top, and when it was hit by a click without any of the other scripts using Bla
having been loaded by the server process before, then bang!
Testing the script in isolation would be the obvious way to safeguard against this particular mistake, but alas the code is dependent upon a humungous environment. Do you know of another way to safeguard against this oversight?
Here's one example how PPI
(despite its merits) is limited in its view on Perl:
use strict;
use HTTP::Request::Common;
my $req = GET 'http://www.example.com';
$req->headers->push_header( Bla => time );
my $au=Auweia->new;
__END__
PPI::Token::Symbol '$req'
PPI::Token::Operator '->'
PPI::Token::Word 'headers'
PPI::Token::Operator '->'
PPI::Token::Word 'push_header'
PPI::Token::Symbol '$au'
PPI::Token::Operator '='
PPI::Token::Word 'Auweia'
PPI::Token::Operator '->'
PPI::Token::Word 'new'
Setting the header and assigning the Auweia->new
parse the same. So I'm not sure how you can build upon such a shaky foundation. I think the problem is that Auweia
could also be a subroutine; perl.exe
cannot tell until runtime.
Further Update
Okay, from @Schwern's instructive comments below I learnt that PPI
is just a tokenizer, and you can build upon it if you accept its limitations.