vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

What can be the difference between require() and do()? I think both are same any difference ?

Vinoth,G

Replies are listed 'Best First'.
Re: require() vs do()
by ELISHEVA (Prior) on Apr 29, 2009 at 13:41 UTC
    • do myfile.pm unconditionally loads in and evaluates a file. require myfile.pm does it only if it hasn't been done already
    • You must use a file name with do. You can use either a file name or a module name with require.
    • Only require can be used to insist on a specific version of Perl
    • do can be used to group multiple statements into a single expression.require can't. For example,
      • do {...} foreach ...
      • do {....} while {...}
      • eval {...} or do {...}

    For more information, please see require and do.

    Best, beth

Re: require() vs do()
by ikegami (Patriarch) on Apr 29, 2009 at 13:34 UTC

    require checks if the file was already executed. do EXPR doesn't.

    The makes require the better choice when loading a module (a file with a package directive), and do otherwise (i.e. never).

    do also has an unrelated do BLOCK form which is useful for inlining multiple statements into an expression.

Re: require() vs do()
by jettero (Monsignor) on Apr 29, 2009 at 13:32 UTC

    quoth do's pod: ... inclusion of library modules is better done with the use and require operators, which also do automatic error checking and raise an exception if there's a problem.

    The differences aren't huge, but if you read do and require, you will see differences.

    -Paul

Re: require() vs do()
by lakshmananindia (Chaplain) on Apr 30, 2009 at 03:19 UTC

    I think the require will also use "do File" inside it. Am I right?

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


      Well, it might do - I have no idea.

      What affect would it have on you if you knew the answer to that question?

      --
      use JAPH;
      print JAPH::asString();