Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2018-04-22 22:50 GMT
    Find Nodes?
      Voting Booth?