Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: newbie's question on inheritance

by tobyink (Canon)
on Oct 28, 2012 at 06:43 UTC ( [id://1001251]=note: print w/replies, xml ) Need Help??


in reply to newbie's question on inheritance

For this to work, you need new to return an object. Right now it just returns undef.

Firstly, you need to read perlsub to learn how subs work, and the important difference between $_ and @_.

Then turn to perlootut to learn the basics of object-oriented programming in Perl.

Here's how you could write your code...

use strict; use warnings; { package testor; sub new { my $class = shift; return bless { component_name => $_[0], }, $class; } sub component_name { my $self = shift; return $self->{component_name}; } sub isExist { my $self = shift; return 0; } } my $myComponent = '7-Zip 9.20 (x64 edition)'; my $checker = testor->new($myComponent); if( $checker->isExist ) { print $checker->component_name, " already installed\n"; } else { print $checker->component_name, " not installed\n"; }

But personally I'd write it as...

use strict; use warnings; { package testor; use Moo; has component_name => ( is => 'ro' ); sub isExist { my $self = shift; return 0; } } my $myComponent = '7-Zip 9.20 (x64 edition)'; my $checker = testor->new(component_name => $myComponent); if( $checker->isExist ) { print $checker->component_name, " already installed\n"; } else { print $checker->component_name, " not installed\n"; }

Either way, read those bits of Perl documentation I mentioned, and learn why your original attempt is wrong.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: newbie's question on inheritance
by anaconda_wly (Scribe) on Oct 28, 2012 at 06:52 UTC

    It's very kind of you. Thanks a lot!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found