in reply to 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. Given that, what's "wrong with [his] perception of how Perl works"? Sounds to me like he just wants to wrap an existing script that uses @ARGV and that's perfectly doable as long as the script doesn't explicitly exit or die. He can do preprocessing on @ARGV as well as additional processing after the require'd code...

echo 'sub foo { print for @ARGV } foo(); 1;' > foo.pl perl -le 'require "foo.pl"; foo()' bar baz

Not that I'd argue such a thing is wise, but it is doable and therefore thinking it is doable doesn't indicate a flawed understanding of how Perl works.

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Passing a required script arguments
by Abigail-II (Bishop) on Sep 26, 2003 at 16:16 UTC
    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

      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.