perlquestion
djantzen
<p>
Hi All,
<p>
I've got a module that utilizes Unix style octal permissions to regulate whether users can perform certain tasks depending on ownership and group membership criteria. Basically what I want to do is set up class constants for various permissions like:
<code>
use constant OTHER_READ => 0004
use constant OTHER_WRITE => 0002;
use constant OTHER_EXEC => 0001;
use constant GROUP_READ => 0040;
use constant GROUP_WRITE => 0020;
use constant GROUP_EXEC => 0010;
use constant OWNER_READ => 0400;
use constant OWNER_WRITE => 0200;
use constant OWNER_EXEC => 0100;
</code>
<p>
and then use these to create masks for comparison to the actual permissions associated with the object. Unfortunately for me, perl automatically converts these into their decimal form when I really do want them to be octals, which means that to use them I have to run them through [sprintf] like <code>sprintf("%o", OWNER_READ)</code>. But even that's trouble because I can't get the proper zero-padding to work so e.g., '0040' comes back '40'. This means for example, that <code>my $mask = 40 | 400</code> comes back '400' while what I need is '440'.
<p>
Complicating matters is the fact that Oracle drops leading zeros in a NUMBER column so when I pull my permissions from the database I have still another conversion to do. So far, the only way I can actually get this to work is to treat it like a string in Perl e.g., <code>use constant OTHER_READ => '0001';</code> and then prepend the zeros as a string when pulling it from the database, but clearly this is a nasty hack.
<p>
Thanks for any help.
<br><b>
Update:</b> Okay, I realized I was misreading the [sprintf] documentation on padding during numeric conversions. How about this:
<code>
use constant OTHER_READ => sprintf("%.4o", 0004)
</code>
etc?
<p>
<div class="pmsig">
<div class="pmsig-131165">
<hr>
"The dead do not recognize context" -- Kai, <i>Lexx</i>
</div></div>