Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

initialise state variable from list context, NOT!

by Random_Walk (Parson)
on Sep 08, 2009 at 12:32 UTC ( #794116=perlquestion: print w/ replies, xml ) Need Help??
Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Finest monkish minds of the tubes

Getting to grips with Perl 5.10 I am trying to use a state variable in a little SAX fun I am having. To cut to the chase this bit of code

package XML::SAX::jmeter; use strict; use warnings; use 5.010; use Data::Dumper; use base qw(XML::SAX::Base); sub empty_record { return { getting => 'url', url => 'http://', match => '', getting => '', alarm => 'no', }; } state %record = %{empty_record}; #Initialization of state variables in list #context currently forbidden at ...

I also tried giving the compiler a clue ...

state %record = %{empty_record()}; state %record = %{&empty_record}; state %record = %{&empty_record()};

all the same problem.

now I am using $record holding the array ref and my code works but I would like to know why I could not get the other option to work.

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Comment on initialise state variable from list context, NOT!
Select or Download Code
Re: initialise state variable from list context, NOT!
by JavaFan (Canon) on Sep 08, 2009 at 12:40 UTC
    There's no point giving the compiler a clue, as the compiler already knows what you are trying to do. And it's currently forbidden. The reason is that there are too many corner cases to let 'state LIST' be something that is intuitive all the time.

    state $scalar is all that is allowed. Perhaps the best you can do is:

    state $record = empty_record;

      Thanks for the quick answer. The reason I thought I may get it to work with some hints for the compiler is that this is allowed

      state %record; %record = ( getting => 'url', url => 'http://', match => '', getting => '', alarm => 'no', );

      Is an empty hash considered as a safe case? Where is the border of what I can get away with?

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
        One of border cases is:
        sub foo { (my $foo, state $bar) = (f(), g()); ... } foo; foo; # Should g() be called?
        As for state %record;, that is allowed. It's state in a list assignment that is disallowed. But note that
        state %record; %record = ( ... );
        doesn't give you any benefits over
        my %record = ( ... );
        You will have to write it as
        state %record; %record = ( ... ) unless keys %record;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://794116]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-08-30 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls