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

Having got a handle on 'local our', here's an example showing that compared with '\local our'.

#!/usr/bin/env perl use 5.022; use warnings; use feature 'refaliasing'; no warnings 'experimental::refaliasing'; @::data = (1 .. 3); say "Data before pop_sum: @::data"; say "Sum of data: ", pop_sum(\@::data); say "Data after pop_sum: @::data"; say '---'; say "Data before pop_sum_ref: @::data"; say "Sum of data: ", pop_sum_ref(\@::data); say "Data after pop_sum_ref: @::data"; sub pop_sum { local our @data = @{+shift}; my $total = 0; say "\tTotal: $total; Data: @data"; while (1) { $total += pop @data; say "\tTotal: $total; Data: @data"; last if $#data < 0; } return $total; } sub pop_sum_ref { \local our @data = shift; my $total = 0; say "\tTotal: $total; Data: @data"; while (1) { $total += pop @data; say "\tTotal: $total; Data: @data"; last if $#data < 0; } return $total; }

Output:

Data before pop_sum: 1 2 3 Total: 0; Data: 1 2 3 Total: 3; Data: 1 2 Total: 5; Data: 1 Total: 6; Data: Sum of data: 6 Data after pop_sum: 1 2 3 --- Data before pop_sum_ref: 1 2 3 Total: 0; Data: 1 2 3 Total: 3; Data: 1 2 Total: 5; Data: 1 Total: 6; Data: Sum of data: 6 Data after pop_sum_ref:

So, with 'local our', we can do whatever we want with @data and, once local's scope is exited, @::data gets back its previous values. As we'd expect with local.

However, the usefulness of '\local our' in this scenario eludes me. @::data does not get back its previous values. Changing

sub pop_sum_ref { \local our @data = shift;

to

sub pop_sum_ref { \our @data = shift;

produces exactly the same results.

Perhaps it simply not useful in this scenario. Perhaps its not operating as intended.

Update: I've raised a bug report for the \local our issue. I'll update again when I get a ticket number.

Update2: Got the ticket: [perl #125436]

For those wishing to view this, there may be some issues.

I used the perlbug utility, which appeared to run successfully:

Message sent Thank you for taking the time to file a bug report! Please note that mailing lists are moderated, your message may take a while to show up. If you do not receive an automated response acknowledging your message within a few hours (check your SPAM folder and outgoing mail) please consider sending an email directly from your mail client to perlbug@perl.org.

I waited about 10 hours, received no automated response, so followed the above advice and emailed directly.

Thunderbird somehow mangled what was sent: multiple instances of embedding font tags dozens of levels deep. All you'll initially see is:

"Message body is not shown because it is too large."

If you really want to, you can download the text/html content (31.3k) and view it in a browser. However, other than my perl -V output, it mostly contains the same content as you'll find in this thread, so there's little point in doing this unless you really are that interested.

-- Ken

Replies are listed 'Best First'.
Re^2: local our $var; What does it do?
by BrowserUk (Patriarch) on Jun 18, 2015 at 13:07 UTC
    However, the usefulness of '\local our' in this scenario eludes me. @::data does not get back its previous values.

    In that case, as a feature, it still needs work, because it doesn't produce the same results as the existing syntax its intended to simplify:

    our @data = 'fred'; sub x{ our @data; local *data = shift; print $data[ $_ ] for 0 .. $#data; };; my @junk = reverse 1 .. 10; x( \@junk ); print ">>@data<<<";; 10 9 8 7 6 5 4 3 2 1 >>fred<<<

    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

      Thanks for reviewing. I've raised a bug report.

      -- Ken