Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Something like c++'s static?

by Zaxo (Archbishop)
on Aug 02, 2002 at 18:07 UTC ( [id://187169]=note: print w/replies, xml ) Need Help??


in reply to Something like c++'s static?

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found