By including $data in the callback, you are closing over it, thus
preserving its value. However if $data is a reference, the data it
references could change. If you copy the specific context information you
need into lexically-scoped scalars beforehand, and close over those copies
instead, you can avoid that possibility.

-Dan

On Tue, Feb 28, 2017 at 7:16 PM, Jeremy Begg <[email protected]>
wrote:

> Hi,
>
> I'm working on an application that uses Mojo::UserAgent to send data to
> another system, preferably non-blocking.
> The code looks something like this (greatly simplified):
>
> sub send_data {
>     my $data = shift;
>     my $ua = Mojo::UserAgent->new;
>     my $request = $ua->post($upload_url => $data) => sub {
>                                   my ($ua, $tx) = @_;
>                                   post_process($data);
>                                  });
>     return $request;
> }
>
>
> I am concerned that by the time the callback occurs, $data will contain
> something other than what was sent via the POST that sent it, and so the
> post-processing routine (not shown here) will misbehave.
> The response body from the remote system does not contain any context
> information about the request sent to it; all it returns is a success/fail
> status and (if successful) the number of items uploaded.
>
> How can I save the value of $data for use by the callback?  Is there a
> mechanism to attach local state information to the $ua->post() which is
> handed to the callback when it is invoked?
> (I can't see any reference to such in the documentation for
> Mojo::UserAgent.)
>
> Thanks,
> Jeremy Begg
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to