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

so i'm trying to pump a bit of laziness into my code by learning how to make modules. my first attempt is failing miserably - and it's mostly copied right out of Camel(3ed)...

package myMod; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(tryIt); sub tryIt { print "We did it!!\n\n"; } 1;
which is called with:
use myMod; BEGIN { push @ISA, "/home/jptxs"; } tryIt();
when i leave strict out of the module, we're cool. When i put it in we get:
/home/jptxs > perl -w getMyMod Use of reserved word "our" is deprecated at myMod.pm line 5. Bareword "our" not allowed while "strict subs" in use at myMod.pm line + 5. Unquoted string "our" may clash with future reserved word at myMod.pm +line 5. Array found where operator expected at myMod.pm line 5, at end of line (Do you need to predeclare our?) Global symbol "@ISA" requires explicit package name at myMod.pm line 5 +. Use of reserved word "our" is deprecated at myMod.pm line 6. Bareword "our" not allowed while "strict subs" in use at myMod.pm line + 6. Unquoted string "our" may clash with future reserved word at myMod.pm +line 6. Array found where operator expected at myMod.pm line 6, at end of line (Do you need to predeclare our?) Global symbol "@EXPORT" requires explicit package name at myMod.pm lin +e 6. syntax error at myMod.pm line 5, near "our @ISA " BEGIN failed--compilation aborted at getMyMod line 1.
if i take out the our's but leave in the strict:
/home/jptxs > perl -w getMyMod Global symbol "@ISA" requires explicit package name at myMod.pm line 5 +. Global symbol "@EXPORT" requires explicit package name at myMod.pm lin +e 6. Compilation failed in require at getMyMod line 1. BEGIN failed--compilation aborted at getMyMod line 1.
I'm very confused...and after feeling so perl-elated reading the camel...

-- I'm a solipsist, and so is everyone else. (think about it)

Replies are listed 'Best First'.
Re (tilly) 1: my first module
by tilly (Archbishop) on Oct 07, 2000 at 00:02 UTC
    Try the following from a command prompt:
    perl -v
    I am guessing the version you will see is something like 5.005_03, which did not have the "our" keyword.

    If so then I suggest:

    use vars qw(@ISA @EXPORT_OK);
    Note that I recommend @EXPORT_OK over @EXPORT because of what is said in perldoc Exporter...
Re: my first module
by KM (Priest) on Oct 07, 2000 at 00:05 UTC
    Are you using Perl 5.6 or 5.005? Sounds like 5.005, which has no 'our'.. so try making the 'our' to 'my' and see what happens. You should also

    use vars qw(@ISA @EXPORT};

    I don't understand why you are pushing onto @ISA.. do you mean @INC?

    Cheers,
    KM

      You can't change our to my for things like @ISA or @EXPORT. They have to be package variables. The closest older thing for our is use vars, as others have pointed out.

      -- Randal L. Schwartz, Perl hacker

        I did point that out. I had meant 'in general' to change 'our' to 'my' if he isn't in 5.6.. bad wording left room for interpretation. But, I am wrong (this Florida sun is scrambling my brain), you would want use vars.

        Cheers,
        KM

      I think he really meant @ISA.

      The purpose is to inherit import from Exporter, so that when someone use's your module they get stuff imported correctly from your namespace.

      For modules with procedural stuff, that is usually the right way to do things.

      UPDATE
      Oops, I just saw the "push" line. You are right, that doesn't make sense to me either. blush

        Can anybody think of advantages to the "push @INC" move here and the more familiar
        use lib 'foo/bar/bletch';

        ?

        Philosophy can be made out of anything -- or less

      I did mean @INC and I since found I don't need to do either as it finds modules in the same directory by default...I was hoping that would slide by since I couldn't edit the top node, but why would i think that with all the Monks on the job? :)

      -- I'm a solipsist, and so is everyone else. (think about it)

        @INC will contain . (the cwd), unless you are using -T, in which case it will remove it and you need to put it back in there.

        Cheers,
        KM

Re: my first module
by lhoward (Vicar) on Oct 07, 2000 at 01:07 UTC
    I always use h2xs to generate my module templates. It comes with perl and is easy to use. The module templates it builds have all the necessary pieces in-place so you don't have to worry about doing it yourself; all you have to do is drop in your code. Also, the templates produced by h2xs are all ready for a CPAN style "perl Makefile.PL/make/make test/make install" install (supports "make tardist" for building distributable bundles too):
    % h2xs -A -X -n Foo::DoIt
Re: my first module
by jptxs (Curate) on Oct 07, 2000 at 00:18 UTC

    the use vars did fix it and I am on 5.005_03 because they don't want me upgrading just yet - I'm just glad they're letting me use Linux for the project : )

    -- I'm a solipsist, and so is everyone else. (think about it)