Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: How to access a variable inside subroutine?

by pryrt (Abbot)
on Sep 05, 2020 at 20:43 UTC ( [id://11121389]=note: print w/replies, xml ) Need Help??


in reply to Re: How to access a variable inside subroutine?
in thread How to access a variable inside subroutine?

I had an idea when you used the pre-state closure idea: put your getter inside the context of counter, so it that $add is in scope:

use 5.030; use strict; use warnings; sub counter { my @nums = (1..500); state $add = 0; for my $num (@nums) { $add += $num; } sub getter { $add } # because this sub is inside the scope of coun +ter(), it has visibility on $add! } counter(); say "getter => ", getter();

Replies are listed 'Best First'.
Re^3: How to access a variable inside subroutine?
by Fletch (Bishop) on Sep 06, 2020 at 05:10 UTC

    Side note: be careful doing this with a my variable because some weird things can happen with nested subs referencing lexicals from the enclosing scope. Look for Variable "%s" will not stay shared in perldiag.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      I wanted to give the same warning and I'm not sure this is less of a problem with state

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        It isn't a problem with state.

        It's only a problem with my because a new SV is allocated for each call of the outer sub, but the inner sub only closed over the first one.

Re^3: How to access a variable inside subroutine?
by pritesh_ugrankar (Monk) on Sep 05, 2020 at 21:08 UTC

    Hi,

    Thank you!! It worked!!....I have always been baffled by the "getter/setter" stuff...I still don't understand it, but still was able to run the code you wrote.

      Generically speaking, "getters and setters" are any routine that you can (must ...) call to affect a value that you can't access directly – because it is inside of a closure or an object. In the general sense, they often have "side effects." For instance, calling a "color setter" actually causes the color of something to change, and also completely conceals the mechanics of how that actually happens.
Re^3: How to access a variable inside subroutine?
by perlfan (Vicar) on Sep 05, 2020 at 20:51 UTC
    Very nice, thank you. I was going old school but now this is a lot cleaner and more concise.
      Read the link pointed out by LanX. Best way to learn it is to play around with it.

      Oops replied to the wrong thing. pritesh_ugrankar this message applies to your comment about it now working but you not understanding.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11121389]
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-24 02:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found