Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Problem with inheritance

by kevinp (Novice)
on Oct 24, 2007 at 20:36 UTC ( [id://647007]=perlquestion: print w/replies, xml ) Need Help??

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

I am starting with object oriented Perl and have hit a problem:

I have two files (taken and tweaked from a book on LWP) - see below.

What I am trying to do is inherit from LWP::Simple but I get the error:

Can't locate object method "new" via package "Browser" at ./EspaceBrow +ser.pl line 17.

Both files are in the same directory and @INC shows the directory when printed. Why is it failing to inherit new from LWP?

Browser.pm



package Browser; use LWP::Simple; @ISA = "LWP::Simple"; sub notnew { my $class = shift; my $self = { }; return bless $self, $class; } sub name { my $self = shift; $self->{NAME} = shift if @_; return $self->{NAME}; } sub age { my $self = shift; $self->{AGE} = shift if @_; return $self->{AGE}; } 1;

EspaceBrowser.pl



#! /usr/bin/perl -w use strict; use warnings; use FindBin; use lib $FindBin::Bin; use Browser; foreach my $x ( @INC ) { print "$x\n"; } my $dude = Browser->new(); $dude->name("Jason"); $dude->age(23); printf "%s is age %d.\n", $dude->name, $dude->age;

Edit: g0n - code tags

Replies are listed 'Best First'.
Re: Problem with inheritance
by Sidhekin (Priest) on Oct 24, 2007 at 20:52 UTC

    LWP::Simple is not an object oriented module. It's not a class, so conceptually impossible to subclass. :)

    You may want to subclass LWP::UserAgent instead? LWP::Simple is implemented using an object of this class.

    ... and if you want, you may even trick LWP::Simple into using your object instead ... just assign a properly instanciated and initialized object to $LWP::Simple::ua before requiring LWP::Simple! (Disclaimer: Probably evil.)

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

Re: Problem with inheritance
by gamache (Friar) on Oct 24, 2007 at 20:53 UTC
    I did not know that @a = "FOO"; and @a = ("FOO"); were equivalent! What a country.

      Parens are usually used because "=" has higher precedence than ",".

      >perl -MO=Deparse,-p -e "@a = 1, 2" ((@a = 1), '???'); -e syntax OK >perl -MO=Deparse,-p -e "@a = (1, 2)" (@a = (1, 2)); -e syntax OK

      ('???' is the 2 in void context being optimized away.)

      Thanks - Changed to UserAgent now everything works.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://647007]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-18 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found