Discussion:
[Mojolicious] Using 'state' in a helper
Viktor Nacht
2018-11-20 01:17:57 UTC
Permalink
Does using state inside a helper last for the duration of the connection,
or something longer lasting like a process, worker, etc?

I want to create (and return) a current DateTime object and make it
accessible to multiple helpers and templates during a connection.

I saw this Gist, but I'm not sure it pertains to per-connection:

https://gist.github.com/s1037989/179d53b86e46ae788f62

V
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Dan Book
2018-11-20 02:01:09 UTC
Permalink
A 'state' variable lasts for the rest of that process, but is only
available to the scope it's declared in. If you want per-connection (or
more precisely per request), I recommend storing it in the stash, like:

helper foo => sub {
my $c = shift;
return $c->stash->{'myapp.foo'} //= ...;
};

-Dan
Post by Viktor Nacht
Does using state inside a helper last for the duration of the connection,
or something longer lasting like a process, worker, etc?
I want to create (and return) a current DateTime object and make it
accessible to multiple helpers and templates during a connection.
https://gist.github.com/s1037989/179d53b86e46ae788f62
V
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Daniel Mantovani
2018-11-20 21:12:46 UTC
Permalink
Hi, not sure if this is what you need, but I wrote an example on how to
trigger some state machine with a post request, and later be able to check
some status with other requests:

https://github.com/dmanto/mojo-stateful-server-experiment/blob/master/stateful.pl

Hope it can help
BR,
Daniel
Post by Dan Book
A 'state' variable lasts for the rest of that process, but is only
available to the scope it's declared in. If you want per-connection (or
helper foo => sub {
my $c = shift;
return $c->stash->{'myapp.foo'} //= ...;
};
-Dan
Post by Viktor Nacht
Does using state inside a helper last for the duration of the connection,
or something longer lasting like a process, worker, etc?
I want to create (and return) a current DateTime object and make it
accessible to multiple helpers and templates during a connection.
https://gist.github.com/s1037989/179d53b86e46ae788f62
V
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Viktor Nacht
2018-11-20 22:11:34 UTC
Permalink
*facepalm* The stash, of course. Thank you. :)
Post by Dan Book
A 'state' variable lasts for the rest of that process, but is only
available to the scope it's declared in. If you want per-connection (or
helper foo => sub {
my $c = shift;
return $c->stash->{'myapp.foo'} //= ...;
};
-Dan
--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.
Loading...