in reply to Very very small style question
Consider this scenario. Say we have some parent object that is going to pass of some work to some number of child objects. How many child objects? Don't know; more could be added later depending on the task. Say some method in most of the children require $foo, $bar, and $baz. You could write them like:
Say one of these child objects doesn't require $foo for it's, task. You could special case the call to my_sub in that child, but that breaks the idea of the parent being generic and the children only being specialized. So you just live with shifting off the useless $foo in that child...which isn't so bad.sub my_sub { my $self = shift; my $foo = shift; my $bar = shift; }
But, say later down the line, we want to write this new child that also needs $bork to be passed into that method. So, depending on how much stuff you have to pass around, this could quickly become a mess.
Thus I use a hashref to bypass a lot of this.
So, there's no remembering the order of the params, no shifting variables you don't need, and passing a new one won't break existing children, since they only pull out the ones they need, and never see the rest. The only thing you have to remember is which key has the value you need.sub my_sub { my $self = shift; my $params = shift; my $foo = $params->{'foo'}; my $bar = $params->{'bar'}; } # and in another child we could have sub my_sub { my $self = shift; my $params = shift; my $foo = $params->{'foo'}; my $bork = $params->{'bork'}; } # and the parent calls them like $child->my_sub({'foo' => $foo, 'bar' => $bar, 'baz' => $baz, 'bork' => $bork});
/\/\averick
|
---|