Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: perl oop concept

by tobyink (Abbot)
on Dec 04, 2012 at 07:07 UTC ( #1007022=note: print w/replies, xml ) Need Help??


in reply to perl oop concept

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'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007022]
help
Chatterbox?
[Cosmic37]: hello perly kings and queens
[jedikaiti]: 'ello Monks
[Cosmic37]: and monkeys too...
[Cosmic37]: is anyone doing machine learning with Perl?
[Cosmic37]: I was using gneural code in C and it was interesting but did not have back propagation for neural networks
[corenth]: Cosmic, poking at it, but not really.
[Cosmic37]: I read that its possible to use the FANN library with Perl so I might try that now...
[corenth]: i have a question. I used map{ blah();blah(); blah();}@stuff; and it used up a ton of memory vs. the for(@stuff){} equivalent. What gives? (if anyone knows)
[Cosmic37]: FANN also has LGPL license which I like and its supposed to be quite a capable library from what I read
[corenth]: Cosmic, that sounds interesting. What is FANN (I could search it if I weren't so lazy about it)?

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2018-02-20 18:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (274 votes). Check out past polls.

    Notices?