in reply to Using "USE" instead of "DO" for subroutines call in external file

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

  • Comment on Re: Using "USE" instead of "DO" for subroutines call in external file
  • Download Code

Replies are listed 'Best First'.
Re^2: Using "USE" instead of "DO" for subroutines call in external file
by Anonymous Monk on May 27, 2005 at 07:09 UTC
    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

        thanks a million. It works fine now. However, timing the two instances "do" versus "use", seems that 'do' is faster: with "use":
        1.01user 0.01system 0:01.09elapsed 94%CPU
        with "do":
        0.94user 0.01system 0:01.03elapsed 93%CPU
        Would you suggest I stick to "do"?