This idea of creating a "my" private variable as a sub ref to try to make a totally private function is a non-Perl idea, and not a good one. Perl assumes that you will conform to the interface spec. Or override it when you think it is necessary and proper.

If you make sub privateFunction{} and do not export the name "privateFunction" then anyone following the interface will not be able to call it.

If I know your module name and name of this sub, then even if the sub privateFunction() is not exported, I can call it with the fully qualified name YourModuleName::privateFunction();. Perl module I/F's keep "honest folks honest", but is possible to do "something not intended". Sometimes that's good and sometimes that bad.

In Perl as in C,etc., there are various naming conventions for Private vs Public functions. You can put _, underscore for a private func as a clue that this is not intended for it to be called outside the I/F, but it still can be.

There also appears to be a logical inconsistency in your test strategy. If this function is private, then NO external program should call it, including your test program. There should be a module test routine that tests that module(that test routine is part of the module). Your test program calls this test function, not in the private guts of the module itself. Your test function remains stable even in light of massive implementation changes within the module.

One easy technique for testing is to make a standard sub in each module say "sub test()" and don't export that name. To test, call YourModuleName::test();


In reply to Re: test case for private function in module by Marshall
in thread test case for private function in module by minhtuanht

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.