Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: OUR declaration

by shmem (Chancellor)
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 use strict; our $shared = "one fish";
    # file use strict; our $shared =~ s/e f/e half f/;
    #/usr/bin/perl use strict; our $shared; require ""; require ""; print $shared,"\n"; __END__ one half fish

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


_($_=" "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}

Replies are listed 'Best First'.
Re^2: OUR declaration
by ioannis (Monsignor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://574491]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-12-11 14:29 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (297 votes). Check out past polls.