develooper Front page | perl.beginners | Postings from October 2022

How to implement a static method in perl class?

Thread Next
From:
Henrik P
Date:
October 13, 2022 08:04
Subject:
How to implement a static method in perl class?
Message ID:
447fc2d2-2816-d890-478d-d0d1612f0426@simplemail.co.in
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/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About