Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
All,
For C programmer's, declaring variables in Perl6 with state won't seem very foreign - just think static. In a nutshell, declaring a variable with state only initializes the variable once and any time the sub is re-invoked the variable retains its previous value. Update: This is inaccurate - read the authorative explanation.

Here is an example I, and others, wrote before Pugs had OO.

#!/usr/bin/pugs # Demo of the state() variable declaration. # This is also a neat way of doing OO without actually having OO avail +able. use v6; sub gen_cashier () { # This variable corresponds to a class variable. # It is shared across all "instances" of gen_cashier(). state $cash_in_store = 0; # One could add my() variables here, which correspond to instance +variables. # These would not be shared. # Finally, we return a hashref which maps method names to code. return { add => { $cash_in_store += $^amount }, del => { $cash_in_store -= $^amount }, bal => { $cash_in_store }, }; } my $drawer; $drawer[$_] = gen_cashier() for 1..3; $drawer[1]<add>( 59 ); $drawer[2]<del>( 17 ); say $drawer[3]<bal>(); # This should say "42"

The place I think it would come in most handy is for caching*. In p5, if a cache variable (%seen for instance) is supposed to belong to a sub that needs to persist across invocations, you need to do things like:

  • Pass the cache variable as an argument
  • Scope the variable at the package level
  • Use naked blocks to create a closure

In Perl6, it will be as simple as:
sub foo () { state %seen; # ... }

So how will you use state declared variables in Perl6?

Cheers - L~R

The cache variable doesn't necessarily need to be used to calculate the return value of the sub which would be made easy using Memoize.

In reply to How will you use state declared variables in Perl6? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2021-06-13 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (54 votes). Check out past polls.

    Notices?