Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: Perl Modules

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

in reply to Perl Modules

It is terribly difficult to read your post as is, so I didn't. However, I've put together an example of what I think you're after. The first file is the script itself, which uses the A module ( in the local directory). The A module exports a single function, and this module includes the B module which also exports a single function. It in turn uses the C module, which exports a single function, which prints an incoming string.

a_func() takes a single parameter, a string. It passes it to b_func(), which then passes it to c_func() which then prints it to the screen.

The flow is like this:

script -> A::a_func("str") -> B::b_func("str") -> C::c_func("str") # c_func is what prints the data

The script:

use warnings; use strict; use lib '.'; use A qw(a_func); a_func("a string");

The A module:

package A; use lib '.'; use B qw(b_func); # load the 'B' package use Exporter qw(import); our @EXPORT_OK = qw(a_func); sub a_func { my $str = shift; b_func($str); } 1;

The B module:

package B; use lib '.'; use C qw(c_func); use Exporter qw(import); our @EXPORT_OK = qw (b_func); sub b_func { my $str = shift; c_func($str); } 1;

The C module:

package C; use Exporter qw(import); our @EXPORT_OK = qw(c_func); sub c_func { my $string_to_say = shift; print "$string_to_say\n"; } 1;

Replies are listed 'Best First'.
Re^2: Perl Modules
by jamroll (Beadle) on Jun 20, 2017 at 17:51 UTC
    my apologies re the ugly post, initially. i hadn't noticed i needed to add the p tag...weird to have to add it. anyway. thank you for your reply. i honestly will review it in depth. i have a goal to meet, and the more info i can suck up the better. so your reply is very useful, and spot on track with what i need - despite the initial post being a nightmare to read lol. very cool

      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?

        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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2021-06-13 21:32 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (57 votes). Check out past polls.