Home : General : Perl Programming :

General: Perl Programming: Re: [jdgamble] use strict?: Edit Log

Here is the list of edits for this post
Re: [jdgamble] use strict?
See the differences between my, local, and our (and 'use vars').
In a nutshell, 'my' is the most widely used of the four. It defines a variable scoped to the current scope (which could be an entire file). It cannot be referred to from different scopes.

'local' lets you redefine a 'global' (package) variable for the current scope. If you want to make printing a list really simple, for instance, you might use something like
Code:
local $, = local $\ = $/;
print @list;


this will set $, (usually '') and $\ (usually '') to $/ (usually "\n") for the current scope.

'our' tells perl that you will be using the names package variable. For a normal program (i.e., not a package) using this, it tells perl that the 'variable' part of 'our variable' will refer to '*main::variable' (where * is the proper glyph). For instance,
Code:
package main;
our $abc = 123;


is exactly the same as
Code:
$main::abc = 123; # but you have to use $main::abc instead of $abc


which is the same as
Code:
no strict;
$abc = 123;


when there is no locally scoped $abc (e.g., with 'my' or 'local') for the current scope. 'our' and 'use vars' do essentially the same thing, but 'our' is now the recommended way to do it.

Code:
use vars qw/$abc/;
$abc = 123;

is the same as
Code:
our $abc;
$abc = 123;

Last edited by:

mkp: Aug 9, 2006, 11:07 AM

Edit Log: