Hi Monks,

I'm from a C++ background, and I'm struggling with some of Perl's library and OO-like features. I understand that Perl doesn't allow full encapsulation of data with in a package. What I don't quite understand is *how* I should go about passing data that I might want to be "private" to a package.

To be more precise, I have a package that will have three basic subroutines:

sub search_simple($) { # do stuff } sub search_normal($) { # do stuff } sub search_extended($) { # do stuff }

The thing is, each of these subs do very similar things, but with different parameters which are set in the subs. I would like to have something like this:

sub process_search($) { # do stuff } sub search_simple($) { # set parameters return process_search($); } sub search_normal($) { # set parameters return process_search($); } sub search_extended($) { # set parameters return process_search($); }

So, the crux of my question is: how do I set the parameters most effectively? Will our create a package-level variable? Or should I just be passing around hash-references so that they're destroyed after the sub is called (assuming the reference-count is 0)? Or am I maybe thinking about this all wrong?

Any suggestions/advice/criticism encouraged! :)


In reply to Package-Level Variables by Anonymous Monk

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.