Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^5: Hash versus chain of elsifs

by eyepopslikeamosquito (Archbishop)
on Nov 22, 2021 at 10:39 UTC ( [id://11139013]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Hash versus chain of elsifs
in thread Hash versus chain of elsifs

You can make your %junksites variable persist either by moving it outside the sub (in a block lexical scope) or by making it a state variable.

For a simple example of these two approaches, see the %rtoa variable at:

Replies are listed 'Best First'.
Re^6: Hash versus chain of elsifs
by NERDVANA (Deacon) on Nov 22, 2021 at 11:04 UTC
    Or better, declare it outside the function as "our %junksites". That way someone can make changes to it if needed.
      "Better"? The advantage of lexical variables in the smallest possible scope is that the reader can be certain that all changes are local. state allows us to have a persistent lexical variable.
      Bill
        Yes, better. They showed an example of putting the function as an export of a package. There is absolutely no reason to have a package export close over a "my" variable. The only thing that accomplishes is getting in other people's way when they need to customize the behavior of the module. A module that advertises itself as a mini-database of known-bad URLs is more useful when it can be modified.

        I believe that the purpose of a module is to provide re-usable code, for whatever purpose the user might need it for. The author has no business dictating what users can or can't do with the code, unless they are legitimately trying to prevent a common mistake they think users will make, and feel that users won't immediately realize it was a mistake.

        If the author wants to retain the freedom to make internal changes to a module without potentially breaking other people's customizations, all they need to do is prefix those internals with _ to convey that intent, and the rest of the responsibility is on the user.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-24 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found