in reply to Re^3: Unclear about 'our'
in thread Unclear about 'our'

> discourage using subroutines that share a common pool of variables

I have to disagree in this generality, closures are a common technique.

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^5: Unclear about 'our'
by haukex (Archbishop) on Dec 28, 2022 at 17:47 UTC
    I have to disagree in this generality, closures are a common technique.

    I'm confused by this comment, could you show a code example of what you mean?

    My understanding of BillKSmith's node is that it is arguing against file-scoped lexicals that are used by multiple subs in the file, and I fully agree with that.

      One contrived example, using a private config-val instead of a public package var.
      my $config = 42; my $change_allowed = 0; sub mult { $_[0] * $config } sub get_config { $config } sub set_config { $config = shift if $change_allowed; } # yadda

      of course you could also put only those subs inside an extra block together with the closure var.

      Cheers Rolf
      (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
      Wikisyntax for the Monastery

        of course you could also put only those subs inside an extra block together with the closure var.

        Ok, I see what you mean. Personally, I tend to think of closures as something you can make multiple instances of (sub foo { my $x=0; return sub {$x++} }), but if the code you showed was in a block, then I guess that can fit the definition of a closure too, since the subs are then the only thing to hold references to the variables.