Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: Perl Modules

by stevieb (Canon)
on Jun 20, 2017 at 18:34 UTC ( #1193165=note: print w/replies, xml ) Need Help??

in reply to Re^2: Perl Modules
in thread Perl Modules

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.


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?

Replies are listed 'Best First'.
Re^4: Perl Modules
by jamroll (Beadle) on Jun 20, 2017 at 20:18 UTC
    makes 100 % perfect sense. my code is not OO, though - except for when i use the core modules perl comes with (like CGI and others)...but, does your example work in my case? should i be making my modules generate objects, instead of how i have it setup (i posted code in another reply to my main post)? sounds like a major overhaul if so. this project needs completion. I'm a one man band, and it's been in dev. for 3yrs now (two drafts, and one "final" version)... clearly i'm new at this site, so if i'm doing things that don't adhere to what you would rather see, allow me to apologize. i'll figure it out as i go. like i said, as a one man band, i feel like i'm the only employee for a large hotel - i gotta be everything. so many hats, it's been a very tough learning curve.

      You do not need to change to OO. It was simply an example, which did include using a non-OO based module (WiringPi::API). My above example shows the same type of usage, but without any OO at all.

      Using OO is a personal preference, or when you need to keep the state of things.

        ah, alright. well, i'm not at all familiar with that Wiring module...i am reading everyone's posts though, and heeding all the advice very carefully. so many thanks. every tidbit of info i get will help. i had thought about converting to OO - at least for the module...but, i'm not saavy with OO in any language. it, too, is confusing.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2021-06-15 11:07 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (70 votes). Check out past polls.