Real world example?

Here's the source code for a module that pretty much only usees external modules, and each subroutine generates an object of the external module classes, then returns that object.

Take a look at the dac() sub for instance... it creates a new object of class RPi::DAC::MCP4922, which is in an external, separate module. That module is used on this line.

There are no exports in this case, because the majority of the code is Object Oriented, meaning that the functions don't need to be imported; the subs are methods of the object that was returned.

The RPi::DAC::MCP4922 module itself relies on modules of its own, namely it uses a core external API library. Notice that this use statement does import explicitly, in this case *all* of the functions that are exported by that API module. The API module then requires/uses other external modules, but only Exporter to be able to export the functions , and XSLoader to load the C/XS code that it requires.

Example:

my $pi = RPi::WiringPi->new; my $dac = $pi->dac( model => 'MCP4922', channel => 0, cs => 18, ); my ($dacA, $dacB) = (0, 1); $dac->set($dacA, 4095); $dac->set($dacB, 0);

So you instantiate a new top-level RPi::WiringPi object, then call its dac() method. It then makes a call to RPi::DAC::MCP4922's new() method which returns an object of its own class. Then once the object is returned to the top-level object, it in turn returns it to you.

In this case its a convenience thing. We include the necessary external classes and hide away the need for you, the caller script from having to use the module directly, and manually writing the DACs instantiation code within the script.

Make sense?


In reply to Re^3: Perl Modules by stevieb
in thread Perl Modules by jamroll

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.