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

Hi im new to perl, and im currently trying to create, and run my own module.

Here is my module code .pm file

#!/bin/usr/perl use warnings; sub dog { print "roof roof im a dog!\n"; } dog; 1;

and here is my .pl file to run the simple module.

#!/usr/bin/perl use lib '.'; use dog; dog;

Every-time i try to run the code i get this message

$ perl drill.pl Can't locate dog.pm in @INC (you may need to install the dog module) ( +@INC contains: . /usr/lib/perl5/site_perl /usr/share/perl5/site_perl /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 +/core_perl /usr/share/perl5/core_perl) at drill.pl line 4. BEGIN failed--compilation aborted at drill.pl line 4.

PLEASE HELP!!

2017-11-17 Athanasius added code and paragragh tags

Replies are listed 'Best First'.
Re: perl modules
by Discipulus (Canon) on Nov 16, 2017 at 09:04 UTC
    Hello codestroman and welcome to the monastery and to the wonderful world of Perl!

    First of all, please, add <c> code tags</c> around your code and output.

    Then be sure to have read the standard documentation: perlmod and perlnewmod

    Infact a basic perl module define a package and use Exporter to export functions in the using perl program.

    In my homenode i've collected a lot of links on about module creation

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: perl modules (Can't locate in @INC)
by hippo (Archbishop) on Nov 16, 2017 at 09:21 UTC
    PLEASE HELP!!

    This is a monastery - a place of quite contemplation. The louder you shout the less wisdom shall you receive.

    The error message Can't locate dog.pm in @INC is pretty explicit. Either your module file is not called dog.pm in which case, change it or else your file dog.pm is not in any of the directories listed in @INC in which case either move it to one of those directories or else change @INC with use lib.

    I also see, despite the lack of formatting in your post that your module doesn't use any namespace. You should probably address that. Perhaps a solid read through Simple Module Tutorial would be a good idea?

Re: perl modules
by thanos1983 (Parson) on Nov 16, 2017 at 09:17 UTC

    Hello codestroman

    Just to add a minor suggestion here, to the full cover reply of fellow monk Discipulus. It will assist you a lot also to read Simple Module Tutorial

    Update: Direct answer to your question can be found here How to add a relative directory to @INC with multiple possible solutions. I would strongly recommend to go through all the articles that all monks proposed.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: perl modules
by karlgoethebier (Abbot) on Nov 17, 2017 at 10:12 UTC

    Try this:

    Dog.pm

    package Dog { use strict; use warnings; use Exporter qw( import ); our $VERSION = 1.00; our @EXPORT_OK = qw( dog ); sub dog { shift } 1; }

    fido.pl

    #!/usr/bin/env perl use strict; use warnings; use feature qw (say); use Dog qw(dog); say dog ("barf!"); __END__

    OK, the naming is perhaps a bit unlucky - but it should work.

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: perl modules
by Anonymous Monk on Nov 16, 2017 at 09:07 UTC
    use an absolute pathname in use lib
Re: perl modules
by Anonymous Monk on Nov 16, 2017 at 15:16 UTC

    Welcome to the language ... and, to the Monastery. The "simple module tutorial" listed above is a very good place to start. Like all languages of its kind, Perl looks at runtime for external modules in a prescribed list of places, in a specified order. You can affect this in several ways, as the tutorials describe. Please read them carefully.

    In the Perl(-5) language, this list is stored in a pre-defined array variable called @INC and it is populated from a variety of sources: a base-list that is compiled directly into the Perl interpreter, the PERL5LIB environment-variable, use lib statements, and even direct modification of the variable itself. Perl searches this list from beginning to end and processes (only) the first matching file that it finds.

    (Note that, in Perl, the use statement is actually a pragma, or declaration to the compiler, and as such it has many "uses" and a somewhat complicated syntax.)

      (Note that, in Perl, the use statement is actually a pragma, or declaration to the compiler, and as such it has many "uses" and a somewhat complicated syntax.)

      Please no.

      The word "pragma" has a special meaning in Perl, and it is highly confusing to claim that a Perl "keyword" would be a "pragma". use certainly is a keyword and nothing else.

      If you mean to say something different, please describe in more words what you want to say.