CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [ChopperCharles] private and protected methods: Edit Log



budman
User

Mar 28, 2012, 5:09 PM


Views: 9734
Re: [ChopperCharles] private and protected methods

 
I changed the way __foo and run handles its arg checking.


Code
 
package foobar;
use Carp;

sub new {
my $class = shift;
my $self = {};
bless ($self, $class);
return $self;
}

sub __foo { #private
my $name = shift;
if ( ref($name) ) {
my $self = $name;
# make it private
if ( ref($self) ne __PACKAGE__ ) {
confess("Access to private method denied.");
}
$name = shift;
}
print "Hello $name.\n";
return 0;
}

sub run {#public, but can also be called from this package.
my $name = shift;
if ( ref($name) ) {
my $self = $name;
$name = shift;
return $self->__foo($name);
}
else {
return __foo($name);
}
}

sub iAmFoobar { #public
return run("foobar");
}
1;


To inherit, you need to use the parent.
You can do:
use foobar;
use base foobar;
use parent foobar; # newer

Or look into Moose.pm:
extends 'foobar';

Edit: when you "use parent" it does a require on the module and pushes the module onto @ISA. No need to explicitly push @ISA.


Code
package TacoBell; 

use parent foobar;

sub new {
my $class = shift;
my $self = $class->SUPER::new();
#my $self = {};
#bless ($self, $class);
return $self;
}

sub burrito {
my $self = shift;
return $self->__foo("Charles");
# calling the inherited __foo from parent will fail
}

sub burrito2 {
my $self = shift;
# use this form if you need the super class's method
# to run before your overrides take place
my $x = $self->SUPER::__foo("Charles");
# override some stuff
return $x;
}

1;



Tests

Code
perl -MTacoBell -le '$t=TacoBell->new();$t->iAmFoobar' 
Hello foobar.

perl -MTacoBell -le '$t=TacoBell->new();$t->__foo("Chuck")'
Access to private method denied. at foobar.pm line 17.
foobar::__foo('TacoBell=HASH(0x605998)', 'Chuck') called at -e line 1

perl -Mfoobar -le '$t=foobar->new();$t->iAmFoobar'
Hello foobar.

perl -Mfoobar -le '$t=foobar->new();$t->__foo("Chuck")'
Hello Chuck.


perl -MTacoBell -le '$t=TacoBell->new();$t->burrito()'
Access to private method denied. at foobar.pm line 15.
foobar::__foo('TacoBell=HASH(0x605998)', 'Charles') called at TacoBell.pm line 17
TacoBell::burrito('TacoBell=HASH(0x605998)') called at -e line 1

perl -MTacoBell -e '$t=TacoBell->new();$t->burrito2()'
Access to private method denied. at foobar.pm line 15.
foobar::__foo('TacoBell=HASH(0x605a70)', 'Charles') called at TacoBell.pm line 26
TacoBell::burrito2('TacoBell=HASH(0x605a70)') called at -e line 1



Hope that helps


(This post was edited by budman on Mar 29, 2012, 10:00 AM)


Edit Log:
Post edited by budman (User) on Mar 28, 2012, 5:16 PM
Post edited by budman (User) on Mar 28, 2012, 5:25 PM
Post edited by budman (User) on Mar 28, 2012, 5:26 PM
Post edited by budman (User) on Mar 28, 2012, 5:29 PM
Post edited by budman (User) on Mar 28, 2012, 5:39 PM
Post edited by budman (User) on Mar 28, 2012, 5:39 PM
Post edited by budman (User) on Mar 28, 2012, 5:40 PM
Post edited by budman (User) on Mar 29, 2012, 10:00 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives