Reputation: 1471
When I have 2 data structures meaning the same thing, ex:
$c->req->cookies->{app1} = $c->req->cookies->{general};
$c->req->cookies->{app2} = $c->req->cookies->{general};
Can I write:
( $c->req->cookies->{app1}, $c->req->cookies->{app2} ) = $c->req->cookies->{general};
?
Also, Can I write:
$c->req->cookies->{app1} = $c->req->cookies->{app2 } = $c->req->cookies->{general};
?
Upvotes: 2
Views: 54
Reputation: 241858
The second form is possible and some people use it frequently
$x = $y = $z;
The first form does not do what you need. It only assings the value to the first variable.
($x, $y) = $z;
You need two member list on the right hand side as well:
($x, $y) = ($z) x 2;
Update:
In your case, you can use the x
operator only if the methods involved return the same values for both invocations, otherwise, you can use
($x, $y) = map $obj->method, 1, 2;
Upvotes: 7
Reputation: 118128
As usual, there are many ways to do it. For example, you could also use a hash slice:
@{ $c->req->cookies }{qw( app1 app2 )}
But, I would recommend a lack of originality:
my $cookies = $c->req->cookies;
my $general_cookie = $cookies->{general};
$cookies->{$_} = $general_cookie for qw(app1 app2);
which makes the code more readable, doesn't create new data structures, and reduces complex dereferencing as much as possible.
Upvotes: 4