develooper Front page | perl.perl5.porters | Postings from October 2012

Re: [perl #114932] state(@a) =

Thread Previous | Thread Next
Aristotle Pagaltzis
October 16, 2012 17:30
Re: [perl #114932] state(@a) =
Message ID:
* Father Chrysostomos via RT <> [2012-10-16 23:50]:
> So that means state(our @foo) = ... should assign to the package
> variable @foo once.
> Right?

    $ perl -E'my (our $foo) = 1; say $::foo ? 1 : 0'
    $ perl -E'our (my $foo) = 1; say $::foo ? 1 : 0'

To my mind that is a “no”. So far `state` is basically just another
declarator, which in Perl can be strewn throughout an expression, and
the closest declarator wins. But it’s already not quite just another
declarator, but one with some special behaviour. That’s fine but I don’t
think I want its semantics complicated further so that it becomes kind
of a declarator with some special behaviour that also modifies some
other declarators within the expression.

(Maybe it is a missed opportunity that isn’t spelled `state my`. I care
not to tug that ship back into the port now by the anchor line though.)

For those times where you really need this functionality you might be
able to just abuse lexical `state`:

    { state $init = do { our $foo = 1 } }

Not as obvious as I would like, but at least not ugly either.

And with the aid of Devel::CallSite you can sugar it up to the point of

    stately { our $foo = 1 };

*AUTOLOAD=*_;sub _{s/::([^:]*)$/print$1,(",$\/"," ")[defined wantarray]/e;chop;$_}
#Aristotle Pagaltzis // <>

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About