This looks like OOP code:
package a;
sub new {
my $class = shift;
my $self = {};
bless $self,$class;
return $self;
}
my $obj = new a ();
However, the last line there is generally considered poor style. Try:
my $obj = "a"->new();
But these lines seem to bear no relation to OOP:
$obj ->{$self}{avalue}=10;
print $obj->{$self}{avalue};
Try something like this:
{
package a;
sub new {
my $class = shift;
my $self = {};
bless $self,$class;
return $self;
}
sub avalue {
my $self = shift;
$self->{avalue} = shift if @_;
return $self->{avalue};
}
}
my $obj = "a"->new();
$obj->avalue(10); # set
print $obj->avalue, "\n"; # get
That should give you an idea how OOP works. That said, I'd recommend using Moose or Moo as a way of reducing repetitive OOP code...
{
package a;
use Moo;
has avalue => (is => 'rw');
}
my $obj = "a"->new();
$obj->avalue(10); # set
print $obj->avalue, "\n"; # get
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|