in reply to Passing a required script arguments

Huh?

How do you intent to process the arguments?

Something is wrong with your perception of how Perl works. If you want to call a program (with or without arguments), use system, not require. If you want to load a module, and give that some arguments, use "use", like:

use Module "arg1", "arg2", "arg3";
the arguments are then passed on to the import() subroutine.

Abigail

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

    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.";
    
      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.";
        
        A reply falls below the community's threshold of quality. You may see it by logging in.