On Thu, Feb 14, 2002 at 10:43:41AM -0500, zentara wrote: > On Wed, 13 Feb 2002 15:14:39 -0900, [EMAIL PROTECTED] (Michael Fowler) wrote: > > >You have it backwards. Octal 100500 is 33088 in decimal. You just did the > >conversion with the oct operator. > > > >You're confused about what oct does; it returns the decimal representation > >of an octal value. Also, Perl is going to trip you up here; the decimal > > After putting on the dunce cap and typing 100 times: > "the oct function takes a decimal value and returns octal, > and (stat file)[2] returns a decimal value" I finally see it.
Heh, you still have that backwards. oct() interprets its argument as an octal value, and returns its decimal equivalent. Also, it's best not to think of functions returning numbers in a specific base. Functions simply return numbers, its when you output them that they have to be interpreted in a certain base. > #!/usr/bin/perl > use strict; > use warnings; > #usage testmode file, file defaults to self > my $open = shift || $0; > my $mode = (stat $open)[2]; > print '(stat(file))[2] is ',$mode,' which is decimal representation of the octal > value with file types',"\n"; Correct. > printf "Permissions are %04o",$mode & 07777; > print ' with file type masked off',"\n"; Correct. > my $val = sprintf("%o", $mode); > print "$mode in oct is $val\n"; Correct. > my $perms = (stat("$open"))[2] & 07777; > my $oct_perms = sprintf "%lo", $perms; > print 'decimal perms are ',$perms,' with file type masked off',"\n"; Correct. > print 'octal_perms are ',"$oct_perms",' with file type masked off',"\n"; Correct. > print 'oct(',$perms,') ','is ',sprintf("%o",$perms),"\n"; Given all that I'm not sure how you came to this incorrect conclusion. oct($perms) is not sprintf("%o", $perms). Given a decimal $perms of 320, sprintf("%o", $perms) returns "500", and oct($perms) returns 208. I'm tempted to say oct and sprintf's %o are symmetric, meaning they reverse each other. You can think of it that way if it helps, but it might confuse you in the case of oct(0500) and sprintf("%o", 0500). Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]