I think the simplest way to do it is with the &, |, ^, and ~ operators. An example:
#!/usr/bin/perl
use warnings;
use strict;
use constant EXECUTE => 1;
use constant WRITE => 2;
use constant READ => 4;
my $Permissions = 0;
$Permissions |= READ; # Set READ to true
$Permissions |= WRITE; # Set WRITE to true
$Permissions &= ~READ; # Set READ to false
$Permissions ^= EXECUTE; # Toggle EXECUTE
print $Permissions, "\n";
Prints "3" (that's WRITE and EXECUTE).
Is this what you were looking for?
-BronzeWing
Update:
Oops! I completely forgot getting the info back out again. To check the status of a bit:
if ($Permissions & READ) {
print "You can read.\n";
} else {
print "You can't read.\n";
}
Just like that.
The Secret to Fortune Cookies in One Line
print join("... in bed", `fortune fortunes` =~ m/^(.*)(\.|\?|\!)$/), "\n";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|