in reply to (tye)Re: our/my inconsistency.
in thread our/my inconsistency.

It looks like strict and warnings won't be entirely strict on subroutines and my.
use strict; use warnings; my %hash = (one => "one", two => "two", three => "three" ); print "$hash{one}\n"; test(); exit; sub test { for (keys %main::hash) { print "$_ $main::hash{$_}\n"; } }
This will print one.
Substituting our for my will print something more expected.
Interesting behaviour.
Update:
Try it out with arrays and a scalar.
An array will warn of a var used once.
A scalar will warn of unused var and die because of uninialized variable.
Update++: Questions like this and the explanations from people like danger and tye really are cool. They show the inner workings of Perl and point out the nuances.

Replies are listed 'Best First'.
(tye)Re2: our/my inconsistency.
by tye (Sage) on Jan 03, 2001 at 00:03 UTC

    my %hash does nothing to %main::hash, even if you are in package main. That is why "package variables" are also called "globals" and "my variables" are called "lexicals". Lexical variables are not in any symbol table. For example:

    package one; my $x= "hi"; package two; print "$x\n"; # prints "hi"
    This also illustrates a "feature" of our (that might be useful but could also be a source of problems):
    package one; our $x= "hi"; use vars qw($y); $y= "lo"; package two; print "$one::x $one::y\n"; # prints "hi lo" print "$x\n"; # prints "hi" print "$y\n"; # prints nothing # and would give an error under C<use strict>

            - tye (but my friends call me "Tye")
Re: Re: (tye)Re: our/my inconsistency.
by danger (Priest) on Jan 03, 2001 at 00:00 UTC

    Strict means you must declare your variables with my(), our(), or 'use vars', or use fully qualified package names. Using our() or 'use vars' is just a way of saying we want a global variable but we don't want to have to use the fully qualified name all the time -- you do not have to declare globals that way if you do use fully qualified names (as you do in your sub).

    Your example has two hashes: the global %main::hash (created on the fly in your subroutine) and the lexical %hash (declared near the top with the my() statement).

    No warnings are produced by your sub because the main::hash is referenced twice (no 'used only once' warning), and the print() never executes because there are no keys to loop over (no 'unitialized' warnings).