Re: How to implement a static method in perl class?

Andrew Solomon <[email protected]>
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <CAN=twKu8TfKuOf9dxcsrEi7sMx_SZ5uHgxA6NXXS2rpXbd=jrw@mail.gmail.com>
hi Henrik,

From Perl v5.10 there has been a "state" variable declaration

https://perldoc.perl.org/functions/state

This differs from "our", in that it declares a variable local to the scope
of the subroutine where you're accessing it, while "our" refers to a global
variable accessible throughout the package.
Here's your code modified to use state:

https://publ.ink/x/7f47c58239493f5200cffdca9ebb1a8f

Now, the fact that this has been called in a package with a 'new' method
makes me wonder whether you'd actually like "count" to be a method of an
object. As you can see, using the state variable within the package it is
still shared with all objects of that class.

https://publ.ink/x/e69403568d37b18476872443f9f7afa9

if you'd rather have it as an attribute of an object, with the objects
changing the count separately, the approach to take is more like:

https://publ.ink/x/d4a771e75c89a7f1b4987054352d63a6

In this case though, the Perl community has mostly moved on from using
"bless" directly, to more advanced (and easy to use) packages for object
oriented programming.

At present, the easiest package for OO development is
https://metacpan.org/pod/Moo
The most advanced is https://metacpan.org/pod/Moose

Before too long, there will be advanced OO features in the core of Perl
(the project is called Corinna) https://www.youtube.com/watch?v=5lSdBSCkFGs
but in the meantime, you can use https://metacpan.org/pod/Object::Pad as a
non-core simulation of the Corinna functionality.

Does this answer your question? :-)

Andrew

On Thu, Oct 13, 2022 at 9:04 AM Henrik P <[email protected]> wrote:

> Do you think if my following code is correct for implementing a static
> method (maybe singleton, or class method) in perl class?
>
> use strict;
> use warnings;
>
> package A;
>
> sub new {
>    my $class = shift;
>    bless {},$class;
> }
>
> sub count {
>    our $int ||= 0;
>    $int ++;
>    return $int;
> }
>
> 1;
>
> package B;
>
> print A->count,"\n";
> print A->count,"\n";
> print A->count,"\n";
>
>
> The run results:
> $ perl t4.pl
> 1
> 2
> 3
>
>
> I want a method like this one in scala code.
>
> object Foo {
>    var int = 0
>    def increase = { int += 1; int }
> }
>
> println(Foo.increase)
> println(Foo.increase)
> println(Foo.increase)
>
>
> Thank you.
>
>
> --
> Simple Mail
> https://simplemail.co.in/
>
> --
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> http://learn.perl.org/
>
>
>

-- 
Andrew Solomon
Director, Geekuni <https://geekuni.com/>
P: +44 7931 946 062
E: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.