Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: OUR declaration

by shmem (Canon)
on Sep 23, 2006 at 07:26 UTC ( #574491=note: print w/ replies, xml ) Need Help??


in reply to OUR declaration

As davido said, our variables create an entry in the packages symbol tables in which they are declared, while a my variable doesn't. A my variable hides an existing (package) global from that point on to the rest of the variable's scope. That scope is always within the same file and can be delimited with blocks.

$ourVAR = "123 ABC"; { my $ourVAR = "456 RST"; print "my packAA ourVAR = $ourVAR \n"; } print "our packAA ourVAR = $ourVAR \n"; __END__ my packAA ourVAR = 456 RST our packAA ourVAR = 123 ABC

In your example, you don't delimit the scope for the my version of $ourVAR, so it's valid unto the rest of the file.

our variables are shared variables in two ways: they are visible to code

  • of different packages in the same file
    package Foo; our $foo = 'bar'; package main; print $foo,"\n"; # bar
  • in different files belonging to the same package
    # file foo.pl use strict; our $shared = "one fish";
    # file bar.pl use strict; our $shared =~ s/e f/e half f/;
    #/usr/bin/perl use strict; our $shared; require "foo.pl"; require "bar.pl"; print $shared,"\n"; __END__ one half fish

They behave just like they are qualified - with my, and our.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}


Comment on Re: OUR declaration
Select or Download Code
Re^2: OUR declaration
by ioannis (Priest) on Sep 23, 2006 at 10:19 UTC
    Your points are true, but they probably need both repeatition and clarification. I provide two examples to clarify your points, for the earlier ones were too simple.

    Example 1. Here we show that the our var can also overide the scope of the my var: the winner is the one who declares last:

    my $foo = 1; our $foo = 2; print $foo ; # prints 2
    The result will be different when we reverse the order of declaration:
    our $foo = 2; my $foo = 1; print $foo ; # prints 1

    Example 2. Here we show that inter-package scopes are only valid during 'no strict'; plus, access to the our variable is possible only if the LEXICAL scope of the our variable is still in force (that is, it reins through the whole file and is not limited just to a block):

    no warnings; package Foo; use Data::Dumper; { our $foo = 'bar'; } print 'Foo: '. $foo, "\n"; # prints 'bar' package Bar; no strict; print 'Bar: '. $foo, "\n"; # $foo is undefined
    Having already acknowledged that there were no mistatements in your post, these exaples were meant only as repeatition to the concepts you have already advanced. Same concepts through different examples.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2014-11-27 11:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (183 votes), past polls