Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

use vars

by jeffa (Chancellor)
on Jul 17, 2000 at 00:38 UTC ( #22782=perlquestion: print w/ replies, xml ) Need Help??
jeffa has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain what the difference between the two
methods of declaring variable below:
A: my ($rows, $cols); B: use vars qw($rows $cols);
I am not familiar with using use in the second method.
Thanks.

Comment on use vars
Download Code
Re: use vars
by merlyn (Sage) on Jul 17, 2000 at 00:40 UTC
RE: use vars
by autark (Friar) on Jul 17, 2000 at 03:12 UTC
    When declaring a variable with 'my', you say that it is lexical, that is, the variable is only visible within the scope that you declared it.

    If you on the other hand 'use vars', you say that "in this package I want to use variable $foo without having to write out it's fully qualified name ($main::foo). $foo will not be lexically scoped, but will be a global (a package variable).

    In perl 5.6 you may also use the keyword 'our' to declare variables as globals, but with a visibility scope equal to a lexical variable. A global with lexical scope ? That did sound strange :) Writing 'our $foo' inside a block or subrutine will make $foo a global - but you may refer to it as $foo _only_ within the lexical scope. You can still refer to $foo outside the scope, but then you will have to fully qualify it as $main::foo (or $my_package::foo)

    Autark

Re: use vars (expound a bit?)
by ybiC (Prior) on Jul 17, 2000 at 04:34 UTC
    I've employed both these methods for declaring variables with use strict, but like jeffa, am unclear on the difference(s) between my and use vars.

    merlyn's response is, as always, authoritative and to the point. Would a grokful Monk expound a bit, to help an amatuer Perler understand?

    Update: Oops - Autark explained very well the lexical vs. global aspect. That's what I get for composing a question and not posting till after finishing a good show on The History Channel. :^)

    Now I'm merely confused on the main:foo part. To my knowledge, I've never "fully-qualified" a variable, yet my cobbled-up scripts work anyway. At the risk of asking a dumb question - under what circumstances is it necessary to fully qualify?

      You would fully qualify a 'package' variable when you're in another package. For example, if you use the DBI module, you can access error information for the last call in $DBI::errstr. Presumably, your code is in the main:: package, or another self-defined package. Saying $DBI:: (variable name) tells Perl where to find (variable name), since it's not in the current package.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://22782]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-09-22 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (194 votes), past polls