Another approch along the lines of Randal's comment is the way I tend to pass params, using a hash ref.

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:

sub my_sub { my $self = shift; my $foo = shift; my $bar = shift; }
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.

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.

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});
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.

/\/\averick


In reply to Re: Very very small style question by maverick
in thread Very very small style question by Dominus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.