How to implement a static method in perl class?

Henrik P <[email protected]>
Newsgroups gmane.comp.lang.perl.beginners
Message-ID <[email protected]>
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/
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.