Instead of giving you the theory, which is probably covered much better elwsewhere, let me give you a practical example of something I did rcently which put a smile on my face.

Once of the first modules I wrote did some simple manipulations with dates (this was in early 1999, before I knew what CPAN was all about). Since it does what I want and I know how it works, I pulled it off of a dusty shelf and cleaned up the code so I could re-use it for a project. The work I did included changing one subroutine call from

Adjust ( $Date, $DeltaYears, $DeltaMonths, $DeltaDays );
to use a hash for passing the parameters. Originally, if I wanted to call using only some of the parameters, I'd have to say
Adjust ( $Date, 0, 0, 15 ); # or Adjust ( $Date, 0, -1, 0 ); # or Adjust ( $Date, -3, 0, -1 );
Now I pass in an anonymous hash reference so that those examples become:
Adjust ( { date => $Date, day => 15 } ); # or Adjust ( { date => $Date, month => -1 } ); # or Adjust ( { date => $Date, year => -3, day => -1 } );
The great thing about this technique is that I only pass in the values I need to. If the values aren't there, the subroutine will assume some default value. How?
sub Adjust { my ( $Args ) = @_; my $Year = $Args->{ year } || 0; my $Month = $Args->{ month } || 0; my $Day = $Args->{ day } || 0; # More code .. }
The double bar zero at the end of the three lines allows an undefined value to be interpreted as a zero -- this allows you to specify whatever default value you want, 5, 42, "U.S." or whatever.

Obviously, this becomes much cooler the more parameters you pass in -- and another great thing is that you can expand the subroutine's functionality without worrying about how it affects old code. In an old script I wrote, I had a subroutine that accepted a dozen or so parameters; adding a new one meant searching through the other scripts that called that script and updating all of them.

This technique is much easier to maintain; in my example, I would have had to set up a default value for the new parameter -- and I'd be done!

--t. alex

"Excellent. Release the hounds." -- Monty Burns.


In reply to Re: Trying to learn about References by talexb
in thread Trying to learn about References by dru145

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.