in reply to Procedural vs OOP modules
also note that you can have functions in an OOP module which do not need to know the state, do not need to take the object as input. In which case you just call them as you would without being packaged in an OOP module. That's equivalent of static functions in C++ and Java.
my $xx = XX->new(); $xx->oopfunc("abc"); XX::staticfunc("abc"); {# the OOP module XX package XX; sub new { my $class = shift; my $params = $_[1]; my $parent = ( caller(1) )[3] || "N/A"; my $whoami = ( caller(0) )[3]; my $self = { 'state' => 'xxx' }; bless $self, $class; return $self } # call it as # my $xx = XX->new(); # $xx->oopfunc(...); sub oopfunc { my $self = shift; my $params = shift; print "i am using state: ".$self->{state}."\n"; } # call it as XX::staticfunc(...); sub staticfunc { my $params = shift; print "static func called.\n"; } 1 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Procedural vs OOP modules (mixing interfaces)
by LanX (Saint) on Oct 29, 2021 at 10:10 UTC | |
by bliako (Abbot) on Oct 29, 2021 at 11:29 UTC | |
by LanX (Saint) on Oct 29, 2021 at 14:03 UTC |