http://qs1969.pair.com?node_id=218342

I'd just like to propose that developers of larger packages use bitmasks instead of lots of 0,1 (true or false) variables. This may be the standard for enlightened coders, or it might be a foolish notion by me, so I decided to ask you guys to assess the validity of my reasoning.

My reasoning for this is that Bitmasks can:
Save space, if you are shifting a lot. ( No more  my($is_alive,$is_cool,$is_a_smurf) = @_; ) However, this does not seem to be a valid purpose in and of itself, since most maintenance programmers may not come with knowledge of bitmasks, but will definitely understand true and false values.

Therefore, I reduce code, but the cons outnumber the pros, right? But no! What if I want to add a value to my subroutine checks? Maintenance, and code changes become a big hassle. I'll have to add that extra value in every subroutine that uses it. So how do we solve this? You could use a hash with all the values! A hash is especially a good fit if you are using an OO construct, where a HoH and perldsc types of things are right up your alley.

But I believe that bitmasks are still more helpful than hashes with 0,1 values, because they help you organize your packages, by allowing you to put constants somewhere else. I like to remind myself, and force myself to use the same constructs everywhere in my code. Code consistency means code clarity for a maintenance programmer, and it means simplicity for me. (My reasoning for using strict.) The same way I like to remind myself of the constructs I'm using, and be able to have one persistent variable that I can pass around to subroutines. Why us a hash, when you can use a scalar? And so... I use Bitmasks.

Does this sound valid to you, or am I just being an idiot? ( A why would be helpful) Is there somewhere else where Bitmasks should be used, instead? If you use Bitmasks for a different reason in this case, what is it?
In addition, here's a quick example of where I think Bitmasks are helpful: Pretend this is a random module encapsulated by a greater package. Instead of:
sub test_stuff { my($is_alive,$is_cool,$is_a_smurf,$is_a_moose) = @_; if ( $is_alive ) { print "It's alive!"; } elsif ( $is_cool || $is_a_smurf ) { print "It's either cool xor a smurf"; } elsif ( $is_a_moose ) { print "It's a moose"; } else { print "It's something strange :("; } } sub test_more_stuff { my($is_a_cow,$is_a_pow,$is_a_how,$is_a_now) = @_; if ( $is_a_cow ) { print "It's alive!"; } elsif { etc... etc...} }
package main; use Rev::Bitmasks qw(ALIVE COOL SMURF MOOSE COW POW HOW BOW); sub test_stuff { my $persistent = shift; if ( $persistent & ALIVE ) { print "It's alive!"; } elsif ( $persistent & (COOL | SMURF) ) { print "It's either cool xor a smurf"; } elsif ( $persistent & MOOSE ) { print "It's a moose"; } else { print "It's something strange :("; } } sub test_more_stuff { my $persistent = shift; if ( $persistent & COW ) { print "It's alive!"; } elsif { etc... etc...} } package Rev::Bitmasks; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(ALIVE COOL SMURF MOOSE COW POW HOW BOW); sub ALIVE () { 1; }; sub COOL () { 2; }; sub SMURF () { 4; }; sub MOOSE () { 8; }; sub COW () { 16; }; sub POW () { 32; }; sub HOW () { 64; }; sub BOW () { 128; };