Home > Archive > PERL Beginners > February 2007 > Module - Can't locate object method
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Module - Can't locate object method
|
|
| 39898@supinfo.com 2007-02-17, 3:58 am |
| I am currently writing a rss parser as a learning exercise and I am
getting stuck calling methods from my script.
Executing my script reports me the same error again and again no
matter how I edit it according to the docs I read... >>> "Can't locate
object method "getFeedType" via package "self" (perhaps you forgot to
load "self"?) at testrss.pl line 7."
Can someone tell me why?
Thanks in advance.
========
Here is my calling script:
-------------------
# /usr/bin/perl -w
use rssParser;
$feed = new rssParser;
$feed->getFeedType("$ARGV[0]") or die "Cannot load the file: $!\n";
========
And here is my module:
--------------------
package rssParser;
sub new {
my ($class_name) = @_;
my ($self) = {};
bless ($self, $class_name);
return self;
}
sub getFeedType {
my ($self, $file) = @_;
my $feedtype;
open (RSS_FEED, $file) or return 0;
for $line (<RSS_FEED> ) {
if( $line =~ /^<rss.*version/i ) {
$self->{"$feedtype"}= "rss";
return 1;
}
if( $line =~ /^<rdf[ ]{0,}:[ ]{1,}rdf/i ) {
$self->{"$feedtype"}= "rdf";
return 1;
}
}
close RSS_FEED;
return 0;
}
1;
| |
| nobull67@gmail.com 2007-02-17, 3:58 am |
| On 17 Feb, 07:02, 3...@supinfo.com wrote:
> I am currently writing a rss parser as a learning exercise and I am
> getting stuck calling methods from my script.
> And here is my module:
> --------------------
> package rssParser;
Missing
use strict;
use warnings;
>
> sub new {
>
> my ($class_name) = @_;
>
> my ($self) = {};
>
> bless ($self, $class_name);
>
> return self;
There is a typo in the above line. If you'd enabled warnings and
strictures Perl would have spotted this for you.
Please, ask Perl for help before you ask sentient entities to invest
our time.
| |
| Paul Lalli 2007-02-17, 6:58 pm |
| On Feb 17, 2:02 am, 3...@supinfo.com wrote:
> I am currently writing a rss parser as a learning exercise and I am
> getting stuck calling methods from my script.
> Executing my script reports me the same error again and again no
> matter how I edit it according to the docs I read... >>> "Can't locate
> object method "getFeedType" via package "self" (perhaps you forgot to
> load "self"?) at testrss.pl line 7."
> Can someone tell me why?
> Thanks in advance.
> ========
> Here is my calling script:
> -------------------
> # /usr/bin/perl -w
This is not a shebang. It's just a comment. You forgot the ! after
the #. That means that the -w has no effect, so you haven't actually
enabled warnings. If you had, Perl would have told you what you did
wrong.
>
> use rssParser;
>
> $feed = new rssParser;
>
> $feed->getFeedType("$ARGV[0]") or die "Cannot load the file: $!\n";
>
> ========
> And here is my module:
> --------------------
> package rssParser;
Of course, you shouldn't be counting on the fact that the user of your
module will globally enable warnings, so you should put them in your
module explicitly:
use strict;
use warnings;
If you had done that, Perl would have told you what you did wrong too.
Please ask Perl for help before asking the world. . . .
Paul Lalli
|
|
|
|
|