CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
stat perl

 



zapzap
User

Oct 17, 2013, 12:25 AM

Post #1 of 6 (1050 views)
stat perl Can't Post

Can anybody explain why having to 'XOR' is needed here:


Code
 
$mode = (stat($file))[2];
printf "Mode is %04o",$mode & 07777


First of all, I'm not really sure what XOR is? (exclusive or)
Rather, I'm not really sure why it's so important.
11010101 & 10101010 yields 01111111
but big wow!
And after that is cleared up, why is it necessary to perform:
$mode & 07777
I was hoping $mode would be equal to something like:
$mode = 0642

Please Explain,
Thanks in advance


BillKSmith
Veteran

Oct 17, 2013, 7:37 AM

Post #2 of 6 (1045 views)
Re: [zapzap] stat perl [In reply to] Can't Post

AND, OR, and XOR are logical operators. They all take two arguments each of which has the value 'TRUE' or 'FALSE'.

AND returns TRUE if both arguments are TRUE

OR returns TRUE if at least one of the arguments is TRUE

XOR returns TRUE if either argument is TRUE and the other is FALSE.

Perl defines two symbols for each of these (They differ only in presidence.)

Perl also defines three "Bitwise Operators". Their exact behavior depends on whether the arguments are numbers or strings. Your question seems to deal only with numbers. Inside the computer, numbers are stored in base two. (Only the 'digits' 0 and 1 are used). Perl allows base 8 (octal) and base 16 (Hex) as 'shorthand' way to write binary numbers. The operators do thir usual thing on each bit position. (One is TRUE, zero is FALSE)

Lets look at your example of exclusive or. I assume that you intend your numbes to be binary.


Code
Arg1:    11010101 
Arg2: 10101010

Result: 01111111


In the first column, both arguments are true so the result is false. In all the other columns, only one of the arguments is true, so the result is true.

Part of your confusion is that atleast in Perl, they symbol '&' is not the symbol for bitwise or. It is the symbol for bitwise and.

Repeating your example for and we see:

Code
Arg1:    11010101 
Arg2: 10101010

Result: 10000000



In the code you show, the variable $mode probably contains other information in the high order bits. The & 07777 discards all but the last twelve bits which are enough to hold mode numbers as large as 4095.
Good Luck,
Bill


zapzap
User

Oct 18, 2013, 3:06 PM

Post #3 of 6 (1021 views)
Re: [BillKSmith] stat perl [In reply to] Can't Post

So then what exactly does the stat function return? Can you:
$l = stat($file)
print Data::Dumper($l)

what exactly is (stat($file))[2]??

Is it a scalar, a hash, binary number, octal?

I know it returns a list of ten variables.
I was under the impression that when perldoc stated that the third value it returned was the permissions, that was what it meant. It should have state something much clearer.

Can you explain what is returned, an octal value?


BillKSmith
Veteran

Oct 18, 2013, 8:13 PM

Post #4 of 6 (1015 views)
Re: [zapzap] stat perl [In reply to] Can't Post

Sorry, I did not understand that you were refering to the perl documentation for stat.

You already know that stat is a perl built-in function which returns a thirteen element status of a file specified by a filehandle.

The statement

Code
$mode = (stat($filename))[2];

selects the third (mode) element of that list. (The syntax is the same as selecting the third element of an array)

That element contains both file type and persmissions.

Permissions are stored in the least significant 12 bits.


Code
$mode & 07777


returns the least significant 12 bits of $mode.

The printf format specifier (%04o) tells printf to display these bits as four octal digits. (This is the unix convention for displaying persmissions)
Good Luck,
Bill


zapzap
User

Oct 20, 2013, 12:11 PM

Post #5 of 6 (994 views)
Re: [BillKSmith] stat perl [In reply to] Can't Post

What is the output of $mode if
$mode = (stat($file))[2];

print $mode?
How can I view $mode before I perform & 0777 on it?


Laurent_R
Enthusiast / Moderator

Oct 21, 2013, 1:32 PM

Post #6 of 6 (987 views)
Re: [zapzap] stat perl [In reply to] Can't Post

Just try it. Example under the Perl debugger:


Code
$ perl -de 42 

Loading DB routines from perl5db.pl version 1.33
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1): 42
DB<1> $file = "get_row.pl"; # just any file in my directory

DB<2> $mode = (stat($file))[2];

DB<3> print $mode;
33261
DB<4>


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives