LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-24-2006, 04:40 PM   #1
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Perl experts: what is this code doing?


Hello all,

I have translated a Perl module to Ruby and it went pretty well except for one little bit that I cannot figure out. Here is the Perl code:
Code:
$info{'flags_raw'}		= unpack('V', $self->_readAndIncrementOffset(4));

$info{'flags'}->{'broadcast'}	= ($info{'flags_raw'} & 0x0001) ? 1 : 0;
$info{'flags'}->{'seekable'}	= ($info{'flags_raw'} & 0x0002) ? 1 : 0;
Now on a test run, the value of raw_flags is '2', broadcast is '0', and seekable is '1'.
I have the identical Ruby code:

Code:
flags_raw                   = readAndIncrementOffset(4).unpack("V")[0]
@info['broadcast']          = (flags_raw & 0x0001) ? 1 : 0
@info['seekable']           = (flags_raw & 0x0002) ? 1 : 0
Ruby has a ternary operator too, so this should be identical results but it is not. When I run the Ruby code I get '2' for raw flags, and '1' for both broadcast and seekable.

So: I don't know Perl all that well, but there must be some of that trademark Perl voodoo going one here right? Can anyone tell me why the same operation (ie: a bitwise '&' and a ternary operator) are giving two different results for the same input?

I must admit I do not know much about bitwise operations, so maybe the problem here is differences between how Ruby and Perl evaluate bitwise operations?

Any insight appreciated.
 
Old 06-24-2006, 07:51 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
It seems to me that the Perl code does exactly what you expected it to do. It seems that the only difficulty is the Ruby code. From what you have told us, it seems that (2 & 0x0001) evaluates to true in Ruby; is this right? Can you verify this?
 
Old 06-24-2006, 08:03 PM   #3
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760

Original Poster
Blog Entries: 4

Rep: Reputation: 78
Quote:
It seems that the only difficulty is the Ruby code. From what you have told us, it seems that (2 & 0x0001) evaluates to true in Ruby; is this right? Can you verify this?
Thanks man...

I should have realized: It is evaluating to '0' which in ruby is considered true whereas in Perl it is false.

So simple...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What do those perl code do? cn_chopsticks Programming 10 06-06-2006 11:03 PM
What would this code do in PERL? jdruin Linux - Software 2 11-22-2004 03:12 AM
Perl: What's this mean? (Code sample) Benr Programming 5 10-23-2004 07:20 AM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl experts RHlinux9 Programming 1 12-08-2003 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration