in reply to Re: Closure producing 'Modification of read-only value' error
in thread Closure producing 'Modification of read-only value' error

You can avoid modifying $_ within the sub, by doing the initialization of $hash_ref in a BEGIN block

Of course that doesn't avoid modifying $_, just moves the modification to BEGIN time - if this ends up in a module, for example, it will instead corrupt the caller's $_ at the point they require This::Module, which is much nastier (and usually pretty hard for the caller to debug).

I'd recommend always localising the scope of any changes to $_, with either a local $_ or a local *_ as appropriate.

Hugo