store them in a scalar in binary. e.g. "0755"
I just want to store those 4 numbers in a scalar.
sprintf returns a string, and the "4 numbers" includes a leading zero which is just decoration indicating that the number is octal (base 8). Converting that string into a numeric will only remove the leading zero. If you want the value as an integer (which I assume is what you mean by 'binary') then ignore the
printf or
sprintf:
use warnings;
use strict;
use Devel::Peek;
my $file = $0;
my $mode = sprintf '%04o', (stat $file)[2] & 07777;
print "sprintf Permissions are $mode\n";
print "\twhich is ",oct($mode)," in decimal\n";
print Dump(\$mode),"\n\n";
my $n_mode = (stat $file)[2] & 07777;
print "stat Permissions are $n_mode decimal\n";
print Dump(\$n_mode),"\n";
In the output the string scalar $mode is "0666" but the integer scalar "$n_mode is 438:
sprintf Permissions are 0666
which is 438 in decimal
SV = RV(0x3a1b8) at 0x3a1ac
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x18abfd4
SV = PV(0x3807c) at 0x18abfd4
REFCNT = 2
FLAGS = (PADMY,POK,pPOK)
PV = 0x18b1614 "0666"\0
CUR = 4
LEN = 8
stat Permissions are 438 decimal
SV = RV(0x3a1b8) at 0x3a1ac
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x18bada4
SV = PVIV(0x18a0f4c) at 0x18bada4
REFCNT = 2
FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
IV = 438
PV = 0x18a1f2c "438"\0
CUR = 3
LEN = 4
You can see that $mode does not have an integer value (IV) at that point, only a string value (PV).
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.