Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: local our $var; What does it do?

by BrowserUk (Patriarch)
on Jun 18, 2015 at 09:20 UTC ( [id://1130952]=note: print w/replies, xml ) Need Help??


in reply to local our $var; What does it do?

It's equivalent to:

our $var; local $var;

The first line makes the global $var name visible at the current scope with use strict vars.

The second line localises it in the normal way.

Presumably aimed at simplifying aliasing. As in:

sub sum { our @a local *a = shift; my $total = 0; $total += $a[ $_ ] for 0 .. $#a; return $total; } my @data = getNums(); my $sum = sum( \@data );

Which is nicer than:

sub sum { my $aref = shift; my $total = 0; $total += $aref->[ $_ ] for 0 .. $#{ $aref }; return $total; } my @data = getNums(); my $sum = sum( \@data );

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

Replies are listed 'Best First'.
Re^2: local our $var; What does it do?
by Anonymous Monk on Jun 18, 2015 at 10:30 UTC

    And with the new refaliasing feature, as I understand it, that becomes:

    use v5.22; use feature 'refaliasing'; no warnings 'experimental::refaliasing'; sub sum { \local our @a = shift; my $total = 0; $total += $a[ $_ ] for 0 .. $#a; return $total; }
      our @a; local *a = shift;

      can be safely rewritten as

      \local our @a = shift;

      but you'd normally want

      \my @a = shift;

      since the only reason package variables were used is because lexical variables couldn't be aliased like that.

      That means the code should really become

      sub sum { \my @a = shift; my $total = 0; $total += $a[ $_ ] for 0 .. $#a; return $total; }

      So we loose one line at the point of use; and add 3 at the top of the code. That's progress :)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
      In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        It's clearly worth it only when used at least four times.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^2: local our $var; What does it do?
by kcott (Archbishop) on Jun 18, 2015 at 11:31 UTC

    Thanks for the explanation and examples.

    I believe I'm completely across this now.

    -- Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-25 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found