in reply to Passing package-level/helper variables to user coderefs

What I've learned from fellow monks and from reading 'Code complete' from McConnell, it appears to be the best to make your interfaces as "independent" as possible. So in general, only use the interface (@_) to pass data between your package and the user-sub. Globals could be messed with by other subs, and so mess up your code. Moreover, they are harder to maintain in the long run.

The cleanest way to go is indeed the hashkeys way. Several advantages: Clean namespace, extendible/ flexible, not dependent on the order of args. You say it uglifies the user code. Well, that may be true (qw/beaty eyes beholder/), but it is very readible:

sub user_sub( my %input = @_; $input{numberX} * $input{multiplier} + $user_glob; }
This is a bit more writing than your examples maybe, but very clear.

And I think you summed up the pro's/contra's pretty well. Try to think more like: "is it clean/functional code?" than "is the code very nicely looking?". At least, that would be my approach.

Jeroen
"We are not alone"(FZ)
Fixed error

Replies are listed 'Best First'.
Re: Re: Passing package-level/helper variables to user coderefs
by Anonymous Monk on May 18, 2001 at 19:20 UTC
    or you could use "named params"
    sub user_sub( my %input = @_; $input{numberX} * $input{multiplier} + $user_glob; }