Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Legitimate use for combined 'local' and 'our'

by jeteve (Pilgrim)
on Nov 15, 2010 at 10:41 UTC ( [id://871437]=perlquestion: print w/replies, xml ) Need Help??

jeteve has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,

A question stroke me recently, what could be a legitimate use of both 'local' and 'our' on a variable in a package?

I wrote the following code to try to figure it out:

package Foo; use strict; use warnings; local our $V; sub print_v{ print $V."\n"; } 1; package main; use strict; use warnings; $Foo::V = 'Blabla'; print $Foo::V."\n"; Foo::print_v();

It prints 'Blabla' twice and removing the 'local' on $V doesn't make any difference.

Can you think about any situation where local-izing an 'our' variable could be useful?

Replies are listed 'Best First'.
Re: Legitimate use for combined 'local' and 'our'
by JavaFan (Canon) on Nov 15, 2010 at 10:57 UTC
    package Foo; use 5.012; use strict; use warnings; $Foo::V = "fred"; { local our $V = "waldo"; say $V; sub bar {say $V}; sub set_v {$V = shift}; } package Baz; Foo::bar; Foo::set_v "fnord"; Foo::bar; __END__
    Running this gives output:
    waldo fred fnord
    Removing the local, and running it gives:
    waldo waldo fnord
    And it won't compile if, instead of removing the local, you remove the our.

    Of course, you may argue this code isn't very useful, but it does show there can be differences between local, our and local our. Given that I don't use our much (typically just for @ISA, and @EXPORT and friends), and neither local, I don't think I've ever had the need to use local our.

Re: Legitimate use for combined 'local' and 'our'
by moritz (Cardinal) on Nov 15, 2010 at 10:48 UTC
    #!/usr/bin/perl our $V = 'non-local'; sub print_v { print "$V\n"; } { local our $V = 'local'; print_v; } print_v __END__ # output: local non-local

    Since local affects variables in code you call, you have to call the sub from the scope in which you used <c>local our<c>.

      Not a good enough example as you can freely remove the our in local our, and it will still produce the same result (even under strict). Only if you place the first part in a different scope, and enable strict, you'll see a difference:
      #!/usr/bin/perl use strict; use warnings; { our $V = 'non-local'; sub print_v { print "$V\n"; } } { local our $V = 'local'; print_v; } print_v __END__
      If you now remove the second our, the program doesn't compile.
Re: Legitimate use for combined 'local' and 'our'
by jettero (Monsignor) on Nov 15, 2010 at 11:04 UTC
    It may be that local doesn't do what you think it does. It would make a lot more sense to write something like this:
    package blarg; use strict; use warnings; sub hrmph { our $global = "blarg"; print "$global\n"; local $global; $global = "hrm, just in here!"; print "$global\n"; # when this scope is done, global goes back to "blarg" automatical +ly }

    Note that use vars is pretty much the same thing as our except that the package variable isn't bound locally to a sub as a pseudo-lexical. But if it was a case of use vars local would work the same way. It doesn't make the variable lexical, it makes the values you use in it local to the scope.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://871437]
Approved by moritz
Front-paged by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-23 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found