http://qs321.pair.com?node_id=330050

djantzen has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

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:

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;

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 sprintf("%o", OWNER_READ). 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 my $mask = 40 | 400 comes back '400' while what I need is '440'.

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., use constant OTHER_READ => '0001'; and then prepend the zeros as a string when pulling it from the database, but clearly this is a nasty hack.

Thanks for any help.
Update: Okay, I realized I was misreading the sprintf documentation on padding during numeric conversions. How about this:

use constant OTHER_READ => sprintf("%.4o", 0004)
etc?


"The dead do not recognize context" -- Kai, Lexx