Closed p5pRT closed 11 years ago
I don't understand the rational for disallowing list initialization of state vars:
perl -wE 'use strict; sub cntdn(){state ($x\,$y)=(3\,3); [$x--\, $y--]} use P; P "%s"\, cntdn for (1 .. 3)' Initialization of state variables in list context currently forbidden at -e line 2\, near ");" Initialization of state variables in list context currently forbidden at -e line 2\, near ");" BEGIN not safe after errors--compilation aborted at -e line 3.
Isn't it the case that you only want them initialized\, 'once' during "START'/BEGIN time?
If so\, thats fairly trivial to do in a ugly way:
perl -wE 'use strict;BEGIN{$::INC{"I.pm"}=1} sub cntdn() { state ($x\,$y);use I(($x\,$y)=(3\,3)); [$x--\, $y--]} use P; P "%s"\, cntdn for (1 .. 3)' [3\,3] [2\,2] [1\,1] ---- Assumming I understood the problem. Does the above\, semantically\, fit the bill for initializing state vars only once\, at 'load time'? Wouldn't it be possible to come up with something syntactically more attractive if you worked inside the parser?
(If only Perl had a '#define/macro' ability...)
On Sat Feb 16 15:24:29 2013\, LAWalsh wrote:
This is a bug report for perl from perl-diddler@tlinx.org\, generated with the help of perlbug 1.39 running under perl 5.16.1.
----------------------------------------------------------------- [Please describe your issue here] I don't understand the rational for disallowing list initialization of state vars:
perl -wE 'use strict; sub cntdn(){state ($x\,$y)=(3\,3); [$x--\, $y--]} use P; P "%s"\, cntdn for (1 .. 3)' Initialization of state variables in list context currently forbidden at -e line 2\, near ");" Initialization of state variables in list context currently forbidden at -e line 2\, near ");" BEGIN not safe after errors--compilation aborted at -e line 3.
Here's what pod/perldiag.pod (blead) says about this:
########## Initialization of state variables in list context currently forbidden (F) Currently the implementation of "state" only permits the initialization of scalar variables in scalar context. Re-write "state ($a) = 42" as "state $a = 42" to change from list to scalar context. Constructions such as "state (@a) = foo()" will be supported in a future perl release. ##########
So\, while there may be a more specific rationale\, the immediate reason is: It hasn't been done yet. Would you care to supply a patch?
Isn't it the case that you only want them initialized\, 'once' during "START'/BEGIN time?
Please see the documentation in pod/perlsub.pod under the heading\, "Persistent variables via state()". You will note that there's no discussion of "START" time (whatever that might be) or BEGIN time.
If so\, thats fairly trivial to do in a ugly way:
perl -wE 'use strict;BEGIN{$::INC{"I.pm"}=1} sub cntdn() { state ($x\,$y);use I(($x\,$y)=(3\,3)); [$x--\, $y--]} use P; P "%s"\, cntdn for (1 .. 3)' [3\,3] [2\,2] [1\,1] ---- Assumming I understood the problem. Does the above\, semantically\, fit the bill for initializing state vars only once\, at 'load time'? Wouldn't it be possible to come up with something syntactically more attractive if you worked inside the parser?
(If only Perl had a '#define/macro' ability...)
Since the use of state variables is\, as indicated above\, not tied to any particular point in Perl's execution\, these questions are not relevant.
Thank you very much. Jim Keenan
The RT System itself - Status changed from 'new' to 'open'
On Sat Feb 16 16:09:50 2013\, jkeenan wrote:
Please see the documentation in pod/perlsub.pod under the heading\, "Persistent variables via state()". You will note that there's no discussion of "START" time (whatever that might be) or BEGIN time.
See 'perlvar' $(^GLOBAL_PHASE} -- The current phase of the perl interpreter. CONSTRUCT... START This is the global compile-time. That includes\, basically\, every "BEGIN" block executed directly or indirectly from during the compile-time of the top- level program.
This phase is not called "BEGIN" to avoid confusion with "BEGIN"-blocks\, as those are executed during compile-time of any compilation unit\, not just the top- level program. A new\, localised compile-time entered at run-time\, for example by constructs as "eval "use SomeModule"" are not global interpreter phases\, and therefore aren't reflected by "${^GLOBAL_PHASE}".
The perlsub.pod you refer to says: When combined with variable declaration\, simple scalar assignment to "state" variables (as in "state $x = 42") is executed only the first time. When such statements are evaluated subsequent times\, the assignment is ignored. The behavior of this sort of assignment to non- scalar variables is undefined.
Which isn't really correct:
#!/usr/bin/perl -w use strict; sub sfx($) { use integer; use P; my $n=shift;my $x=$n%100; my $_; my %dt= qw(1 st 2 nd 3 rd) ; $n.(($_=$dt{$x%10}) && $x/10-1 ? $_:"th")} for my $iterator (1 .. 5) { eval { use feature "state"; local * gimme_another = sub {state $x; return ++$x }; printf "%s time: gimme_another=%d\n"\, sfx($iterator)\, &gimme_another }; die "$@" if $@; }
/tmp/testprog1.pl
1st time: gimme_another=1 2nd time: gimme_another=1 3rd time: gimme_another=1 4th time: gimme_another=1 5th time: gimme_another=1
As you can see\, it isn't just initialized the 1st time\, but start off at zero the 2nd\, 3rd\, 4th... etc time.
Would you like me to file a bug report on the doc page for you\, or do you want to file it?
Apparently from the perlvar section above\, the correct initialization is 'in a BEGIN' block whenever a section of code containing that block is 'compiled'.
I mentioned the first example\, above\, as I seem to remember the person who implemented state (who would be far more knowledgeable and more likely appropriate to make such a change\, if they wanted\, than I) saying that they had a hard time telling how to do list context initializations only during the correct "compiler phase". I thought it was START time\, as I vaguely remember (2nd hand) Larry saying that it was to be like a BSS section in the binary of a C program -- an area that was loaded initially with static values at load time. Which\, in my 1st example\, could be checked for by also checking GLOBAL_PHASE before you did the assignment in the BEGIN section of the C\
So I'm not clear what was intended and could think of uses for both behaviors so that's something that would need some discussion which ... well... is difficult for me to participate in for multiple reasons (Pavlovian responses\, accessibility...etc)..
My example was a hope to stimulate the "aha" processes of the person who was having problems implementing it -- I didn't know if such an example might stimulate a new way of thinking about the problem.
But now ... at the very least the perlsub page you point out is flawed. Did it mean 'BEGIN' blocks\, or START-time?
On Sat\, Feb 16\, 2013 at 10:48 PM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
As you can see\, it isn't just initialized the 1st time\, but start off at zero the 2nd\, 3rd\, 4th... etc time.
You show that each $x is initialised the first time C\
Contrary to your claims\, you didn't show any $x being initialised the on
subsequent executions of C\
Would you like me to file a bug report on the doc page for you\, or do you want to file it?
You haven't demonstrated any bug.
- Eric
On Sat Feb 16 22:44:33 2013\, ikegami@adaelis.com wrote:
On Sat\, Feb 16\, 2013 at 10:48 PM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
As you can see\, it isn't just initialized the 1st time\, but start off at zero the 2nd\, 3rd\, 4th... etc time.
You show that each $x is initialised the first time C\
is executed\, just like the docs say. I *eval*ate the same code 5 times and the variable is is re-initialized each time -- it doesn't hold it's value.
Then it says "when such statements are **eval**uated subsequent times\, the assignment is ignored.
The document is equating execution and evaluation.
Contrary to your claims\, you didn't show any $x being initialised the on subsequent executions of C\
since you never called any of the subs more than once.
"gimme_another"\, also the name of the subroutine that is called multiple times in the perlsub.pod is called 5 times here. It is the subroutine that has the state var in it. The manpage clearly says on subsequent evaluations it shouldn't be reinitialized.
On Sun\, Feb 17\, 2013 at 3:41 AM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
On Sat Feb 16 22:44:33 2013\, ikegami@adaelis.com wrote:
On Sat\, Feb 16\, 2013 at 10:48 PM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
As you can see\, it isn't just initialized the 1st time\, but start off at zero the 2nd\, 3rd\, 4th... etc time.
You show that each $x is initialised the first time C\
is executed\, just like the docs say. I *eval*ate the same code 5 times and the variable is is re-initialized each time -- it doesn't hold it's value.
Remove C\
I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once.
On Sun\, Feb 17\, 2013 at 4:22 AM\, Eric Brine \ikegami@​adaelis\.com wrote:
On Sun\, Feb 17\, 2013 at 3:41 AM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
On Sat Feb 16 22:44:33 2013\, ikegami@adaelis.com wrote:
On Sat\, Feb 16\, 2013 at 10:48 PM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
As you can see\, it isn't just initialized the 1st time\, but start off at zero the 2nd\, 3rd\, 4th... etc time.
You show that each $x is initialised the first time C\
is executed\, just like the docs say. I *eval*ate the same code 5 times and the variable is is re-initialized each time -- it doesn't hold it's value.
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You created 5 subs with a lexical var named $x\, and each got initialised once.
Perhaps you should stop creating demonstrations so complicated you have problems understanding them.
# Two state vars\, initialised once each.
perl -E"for (1..2) { local *f = sub { state $x; ++$x }; say f(); }" 1 1
# One state var\, initialised once.
perl -E"for (1..2) { state $x; local *f = sub { ++$x }; say f(); }" 1 2
On Sun Feb 17 01:23:36 2013\, ikegami@adaelis.com wrote:
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once.
Ok\, fine.. you're right.
The documentation is just perfect clear the way it is.
That doesn't negate the subject of this RFE\, which is clearly not supported at this time. If only you could so eagerly prove to me that worked\, then you'd be awesome.
On Sun Feb 17 04:45:33 2013\, LAWalsh wrote:
On Sun Feb 17 01:23:36 2013\, ikegami@adaelis.com wrote:
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once. ---- Ok\, fine.. you're right.
The documentation is just perfect clear the way it is.
That doesn't negate the subject of this RFE\, which is clearly not supported at this time. If only you could so eagerly prove to me that worked\, then you'd be awesome.
@jkeenan - Status changed from 'open' to 'rejected'
On 17 February 2013 13:45\, Linda Walsh via RT \perlbug\-followup@​perl\.org wrote:
On Sun Feb 17 01:23:36 2013\, ikegami@adaelis.com wrote:
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once. ---- Ok\, fine.. you're right.
The documentation is just perfect clear the way it is.
That doesn't negate the subject of this RFE\, which is clearly not supported at this time. If only you could so eagerly prove to me that worked\, then you'd be awesome.
You really should read more carefully\, as if you had you would know he said that the reason it does not work is purely because no-one has had time or interest to implement the feature\, and that patches to implement it are welcome.
Yves
-- perl -Mre=debug -e "/just|another|perl|hacker/"
On Sun\, Feb 17\, 2013 at 7:45 AM\, Linda Walsh via RT \< perlbug-followup@perl.org> wrote:
On Sun Feb 17 01:23:36 2013\, ikegami@adaelis.com wrote:
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once. ---- Ok\, fine.. you're right.
The documentation is just perfect clear the way it is.
I think you might have meant to be sarcastic\, but actually\, it is. It's not appropriate to go into detail about a corner case that affects high-level coders that can understand what's happening and how to work around it.
On 02/17/2013 02:25 PM\, Eric Brine wrote:
On Sun\, Feb 17\, 2013 at 7:45 AM\, Linda Walsh via RT \<perlbug-followup@perl.org \mailto​:perlbug\-followup@​perl\.org> wrote:
On Sun Feb 17 01​:23​:36 2013\, ikegami@​adaelis\.com \<mailto​:ikegami@​adaelis\.com> wrote​: > Remove C\<eval> and you'll get the same result\. C\<eval> has nothing to do > with why you get the same output five times\. > > I shall repeat​: You creates 5 subs with a lexical var named $x\, and each > got initialised once\. \-\-\-\- Ok\, fine\.\. you're right\. The documentation is just perfect clear the way it is\.
I think you might have meant to be sarcastic\, but actually\, it is. It's not appropriate to go into detail about a corner case that affects high-level coders that can understand what's happening and how to work around it.
Just a note\, sarcasm does not translate well\, and should be avoided if at all possible for even very intelligent people whose native language is different then the joke is being expressed in.
On Sun Feb 17 13:26:12 2013\, ikegami@adaelis.com wrote:
On Sun\, Feb 17\, 2013 at 7:45 AM\, Linda Walsh via RT \<
Ok\, fine.. you're right.
The documentation is just perfect clear the way it is
I think you might have meant to be sarcastic\, but actually\, it is. It's not appropriate to go into detail about a corner case that affects high-level coders that can understand what's happening and how to work around it.
Glad to hear you are the official litmus test of understanding for all perl users! If you think it's fine\, it must be.
I'm sure no one might get the idea that C\
I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once.
you were right... fixed that. I still wonder if state was meant to be a private var executed only once / program run vs./eval (difference demoed below. It's good that the wording on the perlsub page makes the difference clear to perl experts...I hope no one new to perl\, coming from 'C' will read that page\, and find out their expectations were incorrect. NOT that they can't work around it. That's trivial.
Understanding what the design intent was and being able to use the docs to get it right the first time\, vs. discovering it's not what they thought and "being able to easily work around it"\, to me\, feel like 2 different things.....but maybe to others\, that's not the case.
cat stateVstatic.pl #!/usr/bin/perl -w use 5.16.0; use P; sub sfx($) { use integer; my $n=shift;my $x=$n%100; my $_; my %dt= qw(1 st 2 nd 3 rd) ; $n.(($_=$dt{$x%10}) && $x/10-1 ? $_:"th")} for (my $i=0; $i\<5;++$i) { sub gimme_another() { {eval q[our $global_state++; state $eval_state++; { state_per_prog => $global_state\, state_per_eval => $eval_state}] }; } P "%s time: gimme_another=%s\n"\, sfx($i)\, gimme_another(); }
Ishtar:law/bin> stateVstatic.pl 0th time: gimme_another={"state_per_eval"=>"1"\, "state_per_prog"=>"1"} 1st time: gimme_another={"state_per_eval"=>"1"\, "state_per_prog"=>"2"} 2nd time: gimme_another={"state_per_eval"=>"1"\, "state_per_prog"=>"3"} 3rd time: gimme_another={"state_per_eval"=>"1"\, "state_per_prog"=>"4"} 4th time: gimme_another={"state_per_eval"=>"1"\, "state_per_prog"=>"5"}
On Sun\, Feb 17\, 2013 at 12:24 AM\, Linda Walsh \perlbug\-followup@​perl\.org wrote:
I don't understand the rational for disallowing list initialization of state vars:
perl -wE 'use strict; sub cntdn(){state ($x\,$y)=(3\,3); [$x--\, $y--]} use P; P "%s"\, cntdn for (1 .. 3)' Initialization of state variables in list context currently forbidden at -e line 2\, near ");" Initialization of state variables in list context currently forbidden at -e line 2\, near ");" BEGIN not safe after errors--compilation aborted at -e line 3.
Hi Linda\,
Could you please not open a bug ticket every time you have a perl question? Specially when a simple 'use diagnostics' would have provided you the answer to your question. There are other more suitable platforms for that such as perlmonks and stackoverflow.
Leon
On Sun Feb 17 05:26:38 2013\, demerphq wrote:
You really should read more carefully\, as if you had you would know he said that the reason it does not work is purely because no-one has had time or interest to implement the feature\, and that patches to implement it are welcome.
That's not what the person working on the feature said at the time they were working on it -- they said they said that there was something about the parser that they couldn't figure out how to do at the time\, so for now\, this was what we got.
Sometimes an RFE with a suggestion on a feature can stimulate someone to look at a problem in a different light that may let them solve the problem in a different way. At least I have found that to be the case at times\, personally.
=========== Leont politely asked:
Hi Linda\, Could you please not open a bug ticket every time you have a perl question? Specially when a simple 'use diagnostics' would have provided you the answer to your question. There are other more suitable platforms for that such as perlmonks and stackoverflow
It was after asking a question on perlmonks that was answered with a quote from the camel book that had me wondering why for(;;) wasn't supported as it was so easy (according the the camel book).
I understand now why perl developers haven't done that yet\, and have been told that I am welcome to provide a patch. It *IS* a limited use case\, I said that in the bug report\, just that it seems to contradict what was being quoted to me from the Camel book.
Filing a bug is not my first choice given the some of the personalities that have knee jerk reactions to me\, at this point -- primarily\, for my being overly attentive to details that others think are unimportant if they see them at all. A bit overfocused at times\, I am. Sorry\, I will admit to it -- it isn't out of any malice that it happens -- but most often because I *like* something.
;=(
On Sun Feb 17 04:45:33 2013\, LAWalsh wrote:
On Sun Feb 17 01:23:36 2013\, ikegami@adaelis.com wrote:
Remove C\
and you'll get the same result. C\ has nothing to do with why you get the same output five times. I shall repeat: You creates 5 subs with a lexical var named $x\, and each got initialised once. ---- Ok\, fine.. you're right.
The documentation is just perfect clear the way it is.
That doesn't negate the subject of this RFE\, which is clearly not supported at this time. If only you could so eagerly prove to me that worked\, then you'd be awesome.
Describing this as a "Request for Enhancement" *now* is somewhat contradicting the ticket's submitted subject of "useless forbidding of list init of state vars?"
The restriction is not useless. As Porting/todo.pod notes\, there are two forms of syntax that need to differ in behaviour\, and it's not obvious to anyone where to patch the code to tell them apart. Until that's possible\, we prefer to leave things as they were before\, instead of giving people the ability to write code which they may not realise is buggy\, and will break later.
Nicholas Clark
Migrated from rt.perl.org#116789 (status was 'rejected')
Searchable as RT116789$