Ankit.11nov has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
How to pass arguments within two perl scripts?

I am trying with require but it is failing with error "Too many arguments for require at D:\Logs\Scripts\Test44.pl"

Replies are listed 'Best First'.
Re: Passing arguments to a Perl script
by moritz (Cardinal) on Sep 24, 2009 at 14:18 UTC
    There are various options:
    • change the called script into a module, and use functions from that module, passing arguments to these functions just like you normally do (preferred)
    • Execute via system and pass the arguments as command line arguments
    • Simply put the arguments into a global variable and restore them from there in the called script (discouraged)
    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Passing arguments to a Perl script
by ikegami (Patriarch) on Sep 24, 2009 at 14:21 UTC
    Your question and your error message don't match. require loads a module if it hasn't already been loaded. It doesn't execute scripts. To launch a script (or any other program), you use system, backticks, open '-|', open '|-', or ... Give us more details, please.
    system('script.pl', $arg1, $arg2); system('script.pl', @args); system($^X, 'script.pl', @args);

    In case you actually do have a module, the whole concept of arguments doesn't make sense with modules because modules can be used by multiple other modules in the same process. Your best alternative to passing arguments to a module is to make a class from the module and pass the arguments to the constructor.

    use Parser; my $parser = Parser->new(@args);
Re: Passing arguments to a Perl script
by Bloodnok (Vicar) on Sep 24, 2009 at 16:22 UTC
    It occurs to me (after much interpolation) that you have seen the idiom use Some::Module qw/arg1 arg2/; i.e. passing compile time arguments to a module, and are asking about the implementation of it [the idiom].

    Declaring the sub Some::Module::import facilitates this. The list of compile time args, if any, preceded by the module name, are passed to the sub by the compiler (thus Some::Module::import behaves as a class method) when the module is used.

    This effect can be observed by considering the oft-quoted equivalence of the following 2 snippets:

    use Foo qw/arg1 arg2/;
    and
    BEGIN { require Foo; Foo->import(qw/arg1 arg2/); }
    A user level that continues to overstate my experience :-))
Re: Passing arguments to a Perl script
by BioLion (Curate) on Sep 24, 2009 at 14:19 UTC

    Can you show us what you have got ( or better a small example script that demonstrates the problem ), so we can have a better idea of what you are trying to do?

    Traditionally arguments passed to scripts are in @ARGV, or you can use one of the many GetOpt modules, if that helps though?

    Just a something something...