Skip to content

Instantly share code, notes, and snippets.

@akanehara
Last active August 2, 2018 10:04
Show Gist options
  • Select an option

  • Save akanehara/7aa8be09a38ee7c5c8965d4d89b1df30 to your computer and use it in GitHub Desktop.

Select an option

Save akanehara/7aa8be09a38ee7c5c8965d4d89b1df30 to your computer and use it in GitHub Desktop.
ミニマルなPerlのOOメモ (B extends A)
package A;
use strict;
use warnings;
#use fields();
sub new {
my $class = shift;
my $self = {
@_,
};
bless $self, $class;
$self;
}
sub get_attr {
my $self = shift;
my $k = shift;
$self->{$k};
}
sub set_attr {
my $self = shift;
my $k = shift;
my $v = shift;
$self->{$k} = $v;
}
sub i_am {
my $self = shift;
"I am A";
}
1;
package B;
use strict;
use warnings;
#use fields();
use base qw( A );
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
bless $self, $class;
$self;
}
sub i_am {
my $self = shift;
my $base = $self->SUPER::i_am;
"I am B. base says, \"$base\"";
}
my $b = B->new(foo=>1, bar=>99);
printf("%s\n", $b->get_attr('foo'));
printf("%s\n", $b->get_attr('bar'));
printf("%s\n", $b->i_am());
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment