Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: state and file perms confusion

by Tanktalus (Canon)
on Aug 04, 2012 at 13:55 UTC ( [id://985434]=note: print w/replies, xml ) Need Help??


in reply to Re^2: state and file perms confusion
in thread state and file perms confusion

There aren't a lot of use cases for storing an octal "number" in a scalar. There are some, but they're all to do with formatting output, not actual use as a number.

If your goal is to use this for chmod, you have to realise that it won't work. If you do this:

$mode = sprintf "%o", 420; # $mode = 644 chmod $mode, $file;
you'll actually be doing the same thing as this:
chmod 01204, $file; # 01204 octal == 644 decimal
Definitely not what you wanted. Leave the number as-is if you plan on using it in chmod. Note that the following lines are equivalent:
# 1. octal number chmod 0644, $file; # 2. decimal number chmod 420, $file; # 3. number in a scalar $mode = 0644; chmod $mode, $file; # 4. number in a scalar (from decimal) $mode = 420; chmod $mode, $file; # 5. number with bitmasks $mode = 0666 & ~022; # like umask chmod $mode, $file; # 6. decimal with bitmasks $mode = 438 & ~022; # 438 == 0666 chmod $mode, $file;
So, to answer your question, the number is already in the scalar. You don't actually need to do anything about it.

And, if you use $mode = sprintf "%o", $mode;, remember what you just did: you took a number, you printf'd it into a string, and put that back in $mode. So $mode no longer carries a number but a string. Perl will try to force it back to a number if that's what you try to do, but it won't be the number you started with.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://985434]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-24 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found