thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am trying to create my first module for CPAN and I got stack to a very minor problem, that I can not figure out how to proceed.

I have a module with name Net::SNTP::Client, sample provided bellow:

#!/usr/bin/perl use strict; use warnings; use Exporter qw(import); package Net::SNTP::Client; our $VERSION = v5.18.2; # Or higher... our @ISA = qw(Exporter); our @EXPORT = (); our @EXPORT_OK = qw(func1 func2); our %EXPORT_TAGS = ( # Mappings for :shortcuts. DEFAULT => [ qw(&func1) ], BOTH => [ qw(&func1 &func2) ] ); sub func1 { return reverse @_ } sub func2 { return map{ uc }@_ } 1;

I can not figure out how to call the module from a script, sample provided bellow:

#!/usr/bin/perl use strict; use warnings; BEGIN { push @INC, '/home/username/Desktop/' } use Net::SNTP::Client; my @list = qw (J u s t ~ A n o t h e r ~ P e r l ~ H a c k e r !); # case 1 # use Net::SNTP::Client; # print func1(@list),"\n"; # print func2(@list),"\n"; # case 2 # use Net::SNTP::Client qw(&func1); # print func1(@list),"\n"; # print MyModule::func2(@list),"\n"; # case 3 # use Net::SNTP::Client qw(:DEFAULT); # print func1(@list),"\n"; # print func2(@list),"\n"; # case 4 # use Net::SNTP::Client qw(:Both); # print func1(@list),"\n"; # print func2(@list),"\n";

I keep getting the error:

an't locate Net/SNTP/Client.pm in @INC (you may need to install the Ne +t::SNTP::Client module) (@INC contains: /etc/perl /usr/local/lib/perl +/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 +/usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl . /h +ome/username/Desktop/) at client.pl line 8. BEGIN failed--compilation aborted at client.pl line 8.

I am following Simple Module Tutorial and trying to convert my module with defined classes to a reusable module and I can not figure it out where I am going wrong. I have spend 3 hours reading online and trying I still nothing. I know it is something so small but I can not understand why it can not work.

Update: I found how to install the module on my pc based on the instructions from Create a CPAN-style Perl module.

Thank you in advance for your time and effort.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re: How to create a reusable module with Exporter
by toolic (Bishop) on Jun 28, 2015 at 00:25 UTC
    You need to have your Net directory under one of the directories listed in the error message. Temporarily comment out the "use" line in your calling script and add these lines:
    use Data::Dumper; print Dumper(\@INC);

    Post the exact full path to your Client.pm file.

      Hello toolic,

      Thank you for your time and effort, answering my question.

      The output that I got is:

      $VAR1 = [ '/etc/perl', '/usr/local/lib/perl/5.18.2', '/usr/local/share/perl/5.18.2', '/usr/lib/perl5', '/usr/share/perl5', '/usr/lib/perl/5.18', '/usr/share/perl/5.18', '/usr/local/lib/site_perl', '.' ];

      What do you mean by: Post the exact full path to your Client.pm file.?

      I tried to put on the module BEGIN { push @INC, '/home/username/Desktop/' } but the result is the same.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: How to create a reusable module with Exporter [SOLVED]
by afoken (Chancellor) on Jun 28, 2015 at 07:45 UTC
    use Exporter qw(import); package Net::SNTP::Client; our $VERSION = v5.18.2; # Or higher... our @ISA = qw(Exporter);

    Please don't inherit from Exporter. It is not needed and has some unwanted side effects. use Exporter qw( import ); is sufficient. Remove the line our @ISA = qw(Exporter);. See also Re: Advice on style and Re^2: Advice on style.

    Also note that our $VERSION = v5.18.2; looks like a misunderstanding. $VERSION is just an arbitary version number for your module. It has absolutely no relation to the perl version. If your code needs some minimum perl version, use use v5.18.2; or the more portable use 5.018002;.

    And finally, move the package line up, preferably to the first line. Perl has no problem with a package line somewhere deep in the code, but it makes the intention clearer and is easier to read.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: How to create a reusable module with Exporter [SOLVED]
by stevieb (Canon) on Jun 28, 2015 at 00:53 UTC

    What toolic is saying, is that you need to actually install your module. In your case, copying it to one of the directories in @INC would be the way a typical CPAN module would do it. This is far from standard, but it'll get you to understand the directory structure needed for installed Perl modules:

    sudo mkdir -p /usr/local/share/perl/5.18.2/Net/SNTP sudo cp Client.pm /usr/local/share/perl/5.18.2/Net/SNTP

    To create a module for CPAN, there's a bit more to it. It needs several things, including an installer. Here's a bit of a start that should get you on your way: How to make a CPAN Module Distribution. It's an old post, but it's a very good starting point.

    -stevieb

      Hello stevieb,

      That solved my problem, I will follow these instructions to complete my module.

      Seeking for Perl wisdom...on the process of learning...not there...yet!