Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Do I have to export all variables and functions from my PM file?

by eyepopslikeamosquito (Archbishop)
on Mar 06, 2022 at 02:27 UTC ( [id://11141870]=note: print w/replies, xml ) Need Help??


in reply to Re: Do I have to export all variables and functions from my PM file?
in thread Do I have to export all variables and functions from my PM file?

For some background on the principles behind the LanX's excellent points, see Information hiding -- the general idea is to hide your module's implementation (which is likely to change over time) from the (possibly many) programs that call it. So don't just blindly export all your module's variables! Instead, think harder about designing a good module interface, one that will stand the test of time, and so allow you to make many future module improvements without needing to update in step all your programs that use it.

This topic is discussed in much more detail at: On Interfaces and APIs

  • Comment on Re^2: Do I have to export all variables and functions from my PM file?

Replies are listed 'Best First'.
Re^3: Do I have to export all variables and functions from my PM file?
by cavac (Parson) on Mar 09, 2022 at 14:30 UTC

    As long as you don't make it too hard to modify the behaviour of your code, that is. Also see the discussion on How to redefine a modules private function?.

    perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      Also related is TDD because writing a test first forces you to focus on module interface early and from the point of view of the module user.

      In your example discussion, module tests that employed a mock DNS server running on a different port would have caught this specific module interface issue before the module was released. Not only that, having your own mock DNS server controlled by your tests makes it easier to test module robustness and recovery (for example, how your module handles timeouts, broken connections, data corruption, ...).

        And a test would be pretty much impossible to run with the current version, unless the test was run as root AND unless there wasn't already a process running at the default port.

        perl -e 'use Crypt::Digest::SHA256 qw[sha256_hex]; print substr(sha256_hex("the Answer To Life, The Universe And Everything"), 6, 2), "\n";'

      Yes indeed! An example of having to resort to mind-boggling trickery and deviousness simply to modify the default value of a module parameter! :) While entertaining, and a challenging problem, definitely not an example of good module design.

      From On Interfaces and APIs, the most relevant point is probably this simple tip for Perl module designers from TheDamian:

      • Make common usage the default; allow uncommon usage via optional attributes.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-20 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found