Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Something like c++'s static?

by RollyGuy (Chaplain)
on Aug 02, 2002 at 16:46 UTC ( #187147=perlquestion: print w/replies, xml ) Need Help??

RollyGuy has asked for the wisdom of the Perl Monks concerning the following question:

In C++, one can have a member of a class be static. This actually turns the member into a single global variable that is shared by all objects of this class. Is there anything like this in Perl?

Replies are listed 'Best First'.
(jeffa) Re: Something like c++'s static?
by jeffa (Bishop) on Aug 02, 2002 at 17:15 UTC
    TheDamian discusses this in his book, Object Oriented Perl. In a nutshell, Perl has no equivalent to C++'s static (or Java's for that matter), but you can achieve roughly the same effect with properly scoped lexical variables:
    use strict; my @foo; push @foo, Foo->new() for (0..4); package Foo; { my $count = 0; sub get_count { return $count } sub inc_count { return ++$count } } sub new { my $class = shift; $class->inc_count(); print STDERR 'there are ', $class->get_count(), " instances\n"; my $self = { bar => 42 }; return bless $self, $class; }
    UPDATE:
    Very good point perrin. Try adding the following code snippet in perrin's example and my example after the for loop line to see the difference in action:
    print $Foo::count, "\n";
    The methods get_count() and inc_count() are still accessible to the client in my code, by the way. That is not necessarily a good thing ...

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Damian's example is fine if you need to hide all variables behind methods, but the use of closures often confuses newbies. The simple answer to this question is to just use a (package-scoped) global:
      use strict; my @foo; push @foo, Foo->new() for (0..4); package Foo; use vars qw($count); sub new { my $class = shift; $count++; print STDERR 'there are ', $count, " instances\n"; my $self = { bar => 42 }; return bless $self, $class; }
Re: Something like c++'s static?
by DamnDirtyApe (Curate) on Aug 02, 2002 at 17:07 UTC
Re: Something like c++'s static?
by Zaxo (Archbishop) on Aug 02, 2002 at 18:07 UTC

    Another approach in newer perls gives something that looks very C-like in use. Make foo an lvalue accessor which takes no arguments:

    BEGIN { my $foo = "default"; sub foo () : lvalue { $foo } } print foo, $/; foo = "special"; print foo,$/;

    The BEGIN block is only needed to get the "default" value in early (completely unneeded in this simple case). In a module, you can leave 'foo' off the @EXPORT_OK list so it's perlishly private to the module package.

    For thread safety, you'd want the 'locked' attribute, besides 'lvalue'.

    Update: Added clarification on the BEGIN block, and an unwise adverb.

    After Compline,
    Zaxo

Re: Something like c++'s static?
by BUU (Prior) on Aug 02, 2002 at 17:00 UTC
    you probably want something like this:
    package baz; { my $static=1; sub new{return bless my $x;} }
    But note that '$static' is completely hidden unless your class has an accessor method. Or you could just use a class global, in the form of '$baz::foo=12;'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://187147]
Approved by osfameron
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-08-13 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?