Perl has powerful stringwise bit fiddling...use it. The below example allows for variable length bitstrings. If you can make sure they are all the same length (e.g. initialize to "\0"x(MAXBITS/8), then set bits), you can use eq and ne, which might make for clearer code.
sub setpermbybitnum { my $vec=""; vec($vec,shift,1) = 1; $vec } # check if any perms are set sub no_perm { "$_[0]" =~ tr/\0//c == 0} # check if all specified perm(s) are set sub has_perms { no_perm( ("$_[0]" & "$_[1]") ^ "$_[1]") } # check if any specified perms are set sub one_or_more_of_perms { !no_perm("$_[0]" & "$_[1]") } use constant READ_PERM => setpermbybitnum(3); use constant WRITE_PERM => setpermbybitnum(2); use constant EXECUTE_PERM => setpermbybitnum(1); my $allowed = READ_PERM | WRITE_PERM; # Check for permissions: if ( no_perm($allowed) ) { print "PERMISSION DENIED\n"; exit -1; } if ( has_perms($allowed, READ_PERM) ) { print "READ Allowed\n"; } if ( has_perms($allowed, WRITE_PERM) ) { print "WRITE Allowed\n"; } if ( has_perms($allowed, EXECUTE_PERM) ) { print "EXECUTE Allowd\n"; } if ( has_perms($allowed, READ_PERM|WRITE_PERM ) ) { print "READ and WRITE Allowed\n"; } if ( has_perms($allowed, READ_PERM|EXECUTE_PERM) ) { print "READ and EXECUTE Allowed\n"; } if ( one_or_more_of_perms($allowed, READ_PERM|EXECUTE_PERM) ) { print "READ or EXECUTE Allowed\n"; }

In reply to Re: Bitmask or Named permissions by Anonymous Monk
in thread Bitmask or Named permissions by linux454

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.