Discussion:
[Mojolicious] Understanding shift->
john
2018-11-24 20:40:10 UTC
Permalink
This probably a perl question so if I should ask elsewhere let me know...

In looking at the Mojo::Pg blog example and in recent addition of db
method to Mojo::Pg::PubSub I see use of shift->.


From Mojo::Pg::PubSub documentation:

# Reconnect immediately
$pubsub->unsubscribe('disconnect')->on(disconnect => sub { shift->db });

Or from the blog example:

$self->helper(pg => sub { state $pg = Mojo::Pg->new(shift->config('pg')) });

This doesn't work in my code and I replace "shift" with "$self".   That
appears to be working.   Also, I can't reconcile using shift in this way
and what the perl documentation says about it but could get over this if
it worked.

What am I missing here?

Thanks,

John
--
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-24 20:51:35 UTC
Permalink
The shift function removes and returns the first argument from @_ (since
there's no array passed for it to shift from) in a sub. It's used this way
because the first argument to these subs is the invocant/object. You can
replace it with $_[0] to get the first argument without removing it. You
should not use $self from outside unless necessary because that may create
a circular reference and leak memory.

-Dan
Post by john
This probably a perl question so if I should ask elsewhere let me know...
In looking at the Mojo::Pg blog example and in recent addition of db
method to Mojo::Pg::PubSub I see use of shift->.
# Reconnect immediately
$pubsub->unsubscribe('disconnect')->on(disconnect => sub { shift->db });
$self->helper(pg => sub { state $pg = Mojo::Pg->new(shift->config('pg')) });
This doesn't work in my code and I replace "shift" with "$self". That
appears to be working. Also, I can't reconcile using shift in this way
and what the perl documentation says about it but could get over this if
it worked.
What am I missing here?
Thanks,
John
--
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.
john
2018-11-24 21:20:04 UTC
Permalink
(since there's no array passed for it to shift from) in a sub. It's
used this way because the first argument to these subs is the
invocant/object. You can replace it with $_[0] to get the first
argument without removing it. You should not use $self from outside
unless necessary because that may create a circular reference and leak
memory.
Thanks.  I totally missed that it was being used in a sub in those cases.
--
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...