Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found