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

Hello monks, I have created some simple modules but it fails because it says I may need to install them first(maybe...I don't know?).

my question to you is,do I have to install them every time before even testing them?

it seems like alot of work before I even get to see them work correctly and,if not, debug and re-install can be a hassle

is there a way to circumvent that during testing ?

these are the code for your review, I will appreciate any criticism to improve format and readability. Looking for your advice in improving my scripts as always ;-)

package Dreamcar; sub modello{ my ($macchina, $modello) = @_; $modello && ($macchina->{'Modello'}=$modello); return $macchina->{'Modello'}; } 1;
package Scuderia; use Dreamcar; @ISA = qw[Dreamcar]; sub tipo{ my ($macchina, $tipo)=@_; if ($tipo){$macchina->{'Tipo'}=$tipo}; return $macchina_>{'Tipo'}; } 1;
package Da_corsa; use Scuderia;; @ISA= "Scuderia"; # we say that ISA e' macchina da corsa nella scuderi +a famosa #declaring a class of racing car sub class{ my($class)=@; my $macchina ={}; #empty anonomous hash bless $macchina, $class; return $macchina; } sub tempo_sul_giro { my ($macchina, $tempo)=@_; $tempo && ($macchina->{'Tempo_sul_Giro'}=$tempo); return $macchina->{'Tempo_sul_Giro'}; }
#testing the modules use strict; use warnings; use Data::Dump qw(dump); use Da_corsa; #inheritance adesso my $da_corsa = Da_corsa->class(); $da_corsa-> modello('Ferrari'); $da_corsa-> tipo('LaFerrari'); $da_corsa -> tempo_sul_giro('2.8 secondi'); print "the Racing car is from ", $da_corsa->modello()," and the type i +s ",$da_corsa->tipo(), " and can do 0-60MPH in : ", $da_corsa->tempo_dal_giro(),"\n";

Replies are listed 'Best First'.
Re: PM module question/improvements
by 1nickt (Canon) on Nov 07, 2015 at 01:11 UTC

    The error means it can't find the module files, that's all. If they exist they are installed; just tell perl where they live by adding to @INC or with lib and/or FindBin.

    From the command line:

    perl -I/path/to/module/directory -MMyModule -E 'my $foo = MyModule->ne +w; say $foo->bar;'

    In a script, if the script is in a directory and you know the module files are in a directory called `my_lib` at the same level:

    use FindBin qw($RealBin); use lib "$RealBin/../my_lib"; use MyModule; my $foo = MyModule->new; say $foo->bar; ...

    The way forward always starts with a minimal test.

      quick follow up question, one error message went away but didn't quite worked by adding the path to the files using use lib "path"... as follows

      use lib "C:\Users/Lucca/Documents/NetBeansProjects/perlpm";

      this is the error message, why is it asking me to pointing to a different directory I implicitly set?

      Can't locate object method "class" via package "Da_corsa" (perhaps you + forgot to load "Da_corsa"?) at C:\Users\Lucca\Documents\NetBeansProj +ects\PERL scripts\perl\MioDreamcar.pl line 17.
      use warnings; use Data::Dump qw(dump); use lib "C:/Users/Lucca/Documents/NetBeansProjects/perlpm"; #initial e +rror gone but other pop up. #use lib "C:/Users/Lucca/Documents/NetBeansProjects"; #tried to follow + example syntax for this method but didn't work #use perlpm::Da_corsa; #adding the file this way...why is this method +wrong.

      created a different dir to do all my testing of future PM's...so the permission are set correctly

      $ ls -lrt perlpm/ total 3 -rwxrwxrwx+ 1 Lucca Lucca 288 Nov 6 17:58 Scuderia.pm -rwxrwxrwx+ 1 Lucca Lucca 343 Nov 6 17:58 Dreamcar.pm -rwxrwxrwx+ 1 Lucca Lucca 569 Nov 6 17:58 Da_corsa.pm

        at C:\Users\Lucca\Documents\NetBeansProjects\PERL scripts\perl\MioDreamcar.pl line 17. is telling you the line and file where the error occured; in this case the error occurred when you tried to LOAD Da_corsa.

        The error message Can't locate object method "class" via package "Da_corsa" (perhaps you forgot to load "Da_corsa"?) is telling you that Perl still can't find a valid package of that name. The reason is probably that you are forgetting to return a true value at the end of your package, i.e. your module Da_corsa.pm should be like:

        package Da_corsa; use strict; use warnings; sub foo { return 'bar'; } 1; # return true
        In your original post the other two packages return true but Da_corsa doesn't.

        Given the various comments and such in your latest example, once your module returns true, you might have success with:

        use lib "C:/Users/Lucca/Documents/NetBeansProjects/perlpm"; use Da_corsa;

        Hope this helps!

        The way forward always starts with a minimal test.
Re: PM module question/improvements
by stevieb (Canon) on Nov 07, 2015 at 03:11 UTC

    I made a valiant attempt to properly modularize this for you and explain how to continue, but my time was cut short. If nobody else does so by the time I'm awake tomorrow, I'll write you a full-blown example, using your code (with necessary modifications).

    Essentially, the easiest way is to make it a real module:

    > cpan install Module::Starter > module-starter --author="Your Name" --email=your_email@example.com - +-license=perl --eumm --module=Dreamcar

    ...then:

    > cd Dreamcar > mkdir lib/Dreamcar

    ...then put your Dreamcar package info into lib/Dreamcar.pm, and underneath lib/Dreamcar/, put the Scuderia package in Scuderia.pm and the Da_corsa package in Da_corsa.pm. You'll need to reference them as Dreamcar::Scuderia and Dreamcar::Da_corsa.

    After that, review t/00-load.t, and copy it to t/01_cars.t, and put your test cases in there.

    Once done and working, you can simply run perl Makefile.PL (once), and thereafter a make test will test everything without reinstall or referencing any other locations. This makes it portable, and these are called unit tests.

    Again, I'm sorry I can't elaborate right now, but it gives you something to think about until I can write up a full-blown example tomorrow (hopefully another Monk will have the time to put this together by then).

      quick question, once I installed the module starter, it went ahead and created a Dreamcar.pm under the dir /cygdrive/c/Users/Lucca/Dreamcar/lib/. It is not empty, it has some text. Do I delete that replace it with my own pm module?

      not sure if the file it automatically created has some format in it that is required

        quick question, once I installed the module starter, it went ahead and created a Dreamcar.pm under the dir /cygdrive/c/Users/Lucca/Dreamcar/lib/

        why are you in cygwin now instead of  C:\Users\Lucca\Documents\NetBeans...?

        Do I delete that replace it with my own pm module? not sure if the file it automatically created has some format in it that is required

        Um,

        perldoc module-starter NAME module-starter - creates a skeleton module distribution ...

        This means it does a copy/paste of an example module .... you then edit/delete/cut/paste/copy/.... whatever you want or need, its regular perl modules