Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to implement a static method in perl class?

2 views
Skip to first unread message

Henrik P

unread,
Oct 13, 2022, 4:15:06 AM10/13/22
to begi...@perl.org
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/
0 new messages