in reply to Re: Re: Passing a required script arguments
in thread Passing a required script arguments

As you well know, when you require a file, the code in the file is executed.

Is it?

for (1 .. 10) { require "./foo.pl"; }
executes "foo.pl" at most once, although it is required 10 times.

Abigail

Replies are listed 'Best First'.
Re: Re: Passing a required script arguments
by sauoq (Abbot) on Sep 26, 2003 at 17:07 UTC

    He didn't say he wanted to execute it more than once. If he did, we could add one line to your code:

    for (1 .. 10) { require "./foo.pl"; delete $INC{ './foo.pl' }; }
    which executes "foo.pl" 10 times.

    So, even had he wanted to do it repeatedly, it's still doable and the OP's question still did not indicate a broken understanding of perl. That's just your assumption.

    Update: Some might take Abigail's comment below to mean that the above is somehow incorrect, and worse, because of his reputation they might believe it without first trying it to see for themselves. For those who might be misled:

    $ echo 'print@ARGV' > t; $ perl -le 'for ( 1 .. 3 ) { @ARGV = $_; require"t"; delete $INC{"t"} +}' 1 2 3

    -sauoq
    "My two cents aren't worth a dime.";
    
      for (1 .. 10) { require "./foo.pl"; delete $INC{ './foo.pl' }; }
      In this case, you simply shouldn't be using require. Use do instead:
      for (1 .. 10) { do "./foo.pl"; die $@ if $@; }

      require is the same as do, except it does some things more, things you specifically don't want, except for the error checking.

    A reply falls below the community's threshold of quality. You may see it by logging in.