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

In reply to Unwanted Octal to Decimal Conversion by djantzen

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.