Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

dumb question about perl module creation and use

by desertrat (Acolyte)
on Jan 13, 2012 at 17:34 UTC ( #947777=perlquestion: print w/replies, xml ) Need Help??
desertrat has asked for the wisdom of the Perl Monks concerning the following question:

So, I'm trying to write some modules for use across a website, and I'm testing a rudimentary first attempt. Following what I discovered on the web I first did:

h2xs -X -n MyCal::DoStuff

with the requisite skeleton in hand I edited to:

package MyCal::DoStuff; use 5.012003; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); # Items to export into callers namespace by default. Note: do not expo +rt # names by default without a very good reason. Use EXPORT_OK instead. # Do not simply export all your public functions/methods/constants. # This allows declaration use MyCal::DoStuff ':all'; # If you do not need this, moving things directly into @EXPORT or @EXP +ORT_OK # will save memory. our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw($a do_something anumber ); our $VERSION = '0.01'; our $a =45; use constant anumber => 85; sub do_something { my $input = shift; my $randno = int (rand 20)+1; my $out = $input/5 *$randno; return ($randno, $out); } # Preloaded methods go here. # Autoload methods go after =cut, and are processed by the autosplit p +rogram. 1; __END__ # Below is stub documentation for your module. You'd better edit it! =head1 NAME MyCal::DoStuff - Perl extension for blah blah blah =head1 SYNOPSIS use MyCal::DoStuff; blah blah blah =head1 DESCRIPTION Stub documentation for MyCal::DoStuff, created by h2xs. It looks like +the author of the extension was negligent enough to leave the stub unedited. Blah blah blah. =head2 EXPORT None by default. =head1 SEE ALSO Mention other useful documentation such as the documentation of related modules or operating system documentation (such as man pages in UNIX), or any relevant external documentation such as RFCs or standards. If you have a mailing list set up for your module, mention it here. If you have a web site set up for your module, mention it here. =head1 AUTHOR Bruce Johnson, E<lt>johnson@pharmacy.arizona.eduE<gt> =head1 COPYRIGHT AND LICENSE Copyright (C) 2012 by Bruce Johnson This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.3 or, at your option, any later version of Perl 5 you may have available. =cut

then wrote my first program using it:

#!/usr/bin/perl use strict; use warnings; use lib '/Users/johnson/MyCal-DoStuff/lib'; use MyCal::DoStuff; my ($rno, $res) = &do_something(26); print "a = $a \n"; print "results of sub are random number $rno and result $res \n"; exit;

This throws errors when run:

dbdev2:~ johnson$ perl test Can't locate auto/MyCal/DoStuff/autosplit.ix in @INC (@INC contains: / +Users/johnson/MyCal-DoStuff/lib /opt/local/lib/perl5/site_perl/5.12.3 +/darwin-multi-2level /opt/local/lib/perl5/site_perl/5.12.3 /opt/local +/lib/perl5/vendor_perl/5.12.3/darwin-multi-2level /opt/local/lib/perl +5/vendor_perl/5.12.3 /opt/local/lib/perl5/5.12.3/darwin-multi-2level +/opt/local/lib/perl5/5.12.3 /opt/local/lib/perl5/site_perl /opt/local +/lib/perl5/vendor_perl .) at /opt/local/lib/perl5/5.12.3/AutoLoader.p +m line 173. at /Users/johnson/MyCal-DoStuff/lib/MyCal/ line 8 a = 45 results of sub are random number 14 and result 72.8

In short, "huh??!??" This was all done 'by the book' No other modules in use in this system throw that error, but they're all on the standard @INC, only MyCall::DoStuff is not.

Replies are listed 'Best First'.
Re: dumb question about perl module creation and use
by CountZero (Bishop) on Jan 13, 2012 at 17:51 UTC
    Did you invoke the sacred mantra:
    perl make make test make install

    Without this your work will not be blessed and will linger in the wilderness, prey to bitrot and heisenbugs.

    Update: the problem is probably because you invoke AutoLoader but actually you have nothing to autoload.


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: dumb question about perl module creation and use
by toolic (Bishop) on Jan 13, 2012 at 17:59 UTC
    I duplicated your error, and I made it go away by commenting out AutoLoader:
    #use AutoLoader qw(AUTOLOAD);
    Read about to see if you really need it.
Re: dumb question about perl module creation and use
by ansh batra (Friar) on Jan 13, 2012 at 17:57 UTC
    your module is not installed on you machine.
    the way you have created is meant for the CPAN

    create a file say (note the extention .pm)
    and then in the same directory
    create you program and write use Testingmodule
    you can upload your module to CPAN and then install it on your machine perl -MCPAN -e 'install youmodulename'and then use it in your program
    because by installing it, itll come to @INC.
    but this is not a good idea since your module is not be usefull to anyone
    use CPAN to share usefull module
    install it by source i.e
    make install

    I Hope Everything is clear now

      Ah, I see. All my web searching lead me to unnecessary complexity...All I really need it s the bare the use AutoLoader qw(AUTOLOAD) line.

      Thanks, all!

Re: dumb question about perl module creation and use
by BrowserUk (Pope) on Jan 13, 2012 at 18:01 UTC
    use lib '/Users/johnson/MyCal-DoStuff/lib';

    Shouldn't that be

    use lib '/Users/johnson/MyCal/DoStuff/lib'; ##...........................^

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Depends on where the OP has installed his module to.

      After running h2xs, the source templates reside in .../MyCal-DoStuff/lib  (i.e. .../MyCal-DoStuff/lib/MyCal/

      After building (perl Makefile.PL; make) they temporarily live in .../MyCal-DoStuff/blib/lib.

      And after installation they are wherever they were installed to.

      In case the OP did not actually install the module, the correct include path (for testing purposes) would've been (presuming he created the initial module directory in his home)

      use lib '/Users/johnson/MyCal-DoStuff/blib/lib';

      In that temporary build tree, there should also have been generated the autosplit.ix file, which wasn't found in the OP's case...

      P.S., calling h2xs with the -A option (i.e. h2xs -AX -n MyCal::DoStuff) does not generate the AutoLoader stuff in the first place.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://947777]
Approved by toolic
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-03-25 04:00 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (300 votes). Check out past polls.