Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Fairly new to Perl, just wondered if anyone could tell me what the following line is setting up / achieving:- use vars qw($opt_h);

Replies are listed 'Best First'.
Re: What does this mean ?
by valdez (Monsignor) on Jan 18, 2003 at 15:32 UTC
Re: What does this mean ?
by gjb (Vicar) on Jan 18, 2003 at 15:36 UTC

    See the vars pragma docs for an explanation (Many Perl docs can be found on http://www.perldoc.com/).

    In short, use vars qw($opt_h); declares the global variable $opt_h. In Perl one doesn't need to declare variables prior to using them, but for anything non-trivial it's strongly advised to do so nevertheless. This can be enforced by using the strict pragma (use strict;), from this line on, all variables must be declared with my or our (see the docs for the strict pragma).

    Hope this helps, -gjb-

Re: What does this mean ?
by FamousLongAgo (Friar) on Jan 18, 2003 at 16:39 UTC
    The script you're looking at is probably using the Getopt::Std module, which makes it easy to pass in parameters as flags from the command line. For example, a program called like this:
    ./grobilize.pl -t 20 -n 44
    will end up containing the variables $opt_t and $opt_n, set to the appropriate value. Like the earlier posts have explained, if you have use strict on (as you should), you need to pre-declare those as globals to avoid getting a compile-time error.

Re: What does this mean ?
by OM_Zen (Scribe) on Jan 19, 2003 at 21:25 UTC
    Hi ,

    The vars is for the package scoped global variable and usually used like you say when the program is to be run with options ,the use vars being used for globally scoped options and variables