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

guru,
I have a file which I call "mysub.pm". It stores *only* subroutines which are use for "mymain.pl" code.
Currently the way I call it in "mymain.pl" is like this:
#!/usr/bin/perl -w use strict; do 'mysub.pm'; # The rest of code
And it works. How can I change it so that I can call it with "use" like this?
#!/usr/bin/perl -w use strict; use mysub.pm; # The rest of code
I tried that code it throws the warning "Command exited with non-zero status 2".

"mysub.pm" simply look like this:
sub mysub1{ # somecodes } sub mysub2{ # somecodes } # some more subroutines
My second question is. Which is better using "use" or "do" to call it? Which is faster? Thanks ...

Replies are listed 'Best First'.
Re: Using "USE" instead of "DO" for subroutines call in external file
by Zaxo (Archbishop) on May 27, 2005 at 06:59 UTC

    Files which are loaded by use or require must return a true value. That is usually done by making the statement, 1; the last perl statement in the file. If you exit the file early with return, a true value represents success, false an error condition.

    The major difference between use and do is that use happens at compile time as if it were in a BEGIN block, but do, like require, happens at runtime. There are also differences in the scope of variables and the matter of the return value you found. See perlfunc for details.

    After Compline,
    Zaxo

      thanks. but I tried your solution by putting "1;" at the end of mysub.pm. But it still fails, same error message.
      sub mysub1{ # somecodes } sub mysub2{ # somecodes } 1;
      "happens at runtime" is slower than "compile time" ?

        I just noticed your use syntax, say

        use mysub; # not # use mysub.pm;
        to load the module. I see a syntax error for that, rather than not returning true, though.

        Your example works for me.

        After Compline,
        Zaxo

Re: Using "USE" instead of "DO" for subroutines call in external file
by holli (Abbot) on May 27, 2005 at 06:58 UTC
      thanks guru, but I don't intend to create CPAN module. I just want to use it personally. Does it has to be that complicated as described in that website? Sorry I'm a newbie.
        read the part about "Exports".


        holli, /regexed monk/
      Doe the program work currently with 'use'?
      If not, maybe you are missing use lib
      Try this:
      #!/usr/local/bin/perl use lib 'path/to/mysub.pm'; use mysub;
Re: Using "USE" instead of "DO" for subroutines call in external file
by displeaser (Hermit) on May 27, 2005 at 13:35 UTC
    Hi,

    an easy guide to very basic modules can be found at this node: Simple Module Tutorial.

    I found this to be very easy to understand and even if you dont understand all the exports etc(which becomes very clear very quickly once you use them a few times), its still very easy to get working.

    Hope this helps some.
    Displeaser
Re: Using "USE" instead of "DO" for subroutines call in external file
by tucano (Scribe) on May 27, 2005 at 13:04 UTC
    Have you tried this?:
    Package mysub; sub 1 { } sub 2 { } 1;
    In the Caller you can say at the start of the script: require mysub; And use the module only when you really need it with: use mysub;
      Mr Tucano,
      use the module only when you really need it
      Thanks for the reply.
      But how do I know I really need it? i.e. with *use*
      Also having read,pointer by Mr,Displeaser below. Do I need to put all that bell and whistles? (e.g. ISA,etc)
        From O'reilly Perl Cookbook:

        Problem
        You have a module that you don't need to load each time the program runs, or whose inclusion you wish to delay until after the program starts up.

        A related situation arises in programs that don't always use the same set of modules every time they're run. For example, the factors program from Chapter 2, Numbers, needs the infinite precision arithmetic library only when the -b command-line flag is supplied. A use statement would be pointless within a conditional because it's evaluated at compile time, long before the if can be checked. So we'll use a require instead:
        if ($opt_b) { require Math::BigInt; }
Re: Using "USE" instead of "DO" for subroutines call in external file
by brian_d_foy (Abbot) on May 27, 2005 at 18:46 UTC

    That doesn't look like a Perl error message. Something else may be going on.

    The trick with these sorts of things is to reproduce the behaviour with the smallest possible chunk of code. Copy everything you need to a safe place (for backup), then start excising bits of code. When you stop seeing the error message, you know the part you just took out may be the culprit. Put that code back in, and excise the code around that, making sure you keep seeing the error message. Pretty soon you should have a small example that shows the problem.

    Once you have the smallest example that demonstrates the problem, so it to us. :)

    Good luck ;)

    --
    brian d foy <brian@stonehenge.com>