Hi.

I am trying to learn by hacking Perl code instead of reading a book. I am very new. I am also new here to Perlmonks.com. Please be nice.

Here is the code.

############## #processfile.pl ############## use Fruits; #using a pm file here. $filehandle = "logfile.isf"; my $packets=[0AA0,0FFA]; my obj= new Fruits($filehandle, $packets); my $numApplepackets= $obj->{records}->{0AA0}->{totalRecords}; my $numBananapackets= $obj->{records}->{0FFA}->{totalRecords}; my BananaNum; my fruitobj; for($BananaNum=1; $BananaNum le $numBananapackets; $BananaNum++){ $fruitobj = $obj->parse0FFA($BananaNum); for(my $j=0;$j<2;$j++){ if($obj->{$j}->{rxABC} > -100){ print "Found"; } } } ############# #Fruits.pm ############# sub parse0FFA{ my $self=shift; my $i=shift; my $size=$self->{records}->{0FFA}->{totalRecords}; my $len=$self->{records}->{0FFA}->{$i}->{_length}; my $data=$self->{records}->{0FFA}->{$i}->{_data}; my ($obj,$tmp,$j); $obj->{timestamp}=$self->{records}->{0FFA}->{$i}->{_timestamp}; for($j=0;$j<2;$j++){ $len=$len-13; ($tmp, $obj->{$j}->{txABC},$obj->{$j}->{txDEF},$obj->{$j}->{txGHI} +, $obj->{$j}->{txJKL}, $obj->{$j}->{rxABC}, $obj->{$j}->{rxDEF}, $dat +a)=unpack("C v v v v v v a$len",$data); $obj->{$j}->{State} = int($tmp/32); $obj->{$j}->{Mode} = int($tmp/16); if($obj->{$j}->{txABC} >= 2*5){ $obj->{$j}->{txABC} -= 2*16;} $obj +->{$j}->{txABC} /= 256; if($obj->{$j}->{txDEF} >= 2*5){ $obj->{$j}->{txDEF} -= 2*16;} $obj +->{$j}->{txDEF} /= 256; if($obj->{$j}->{txGHI} >= 2*5){ $obj->{$j}->{txGHI} -= 2*16;} $obj +->{$j}->{txGHI} /= 256; if($obj->{$j}->{txJKL} >= 2*5){ $obj->{$j}->{txJKL} -= 2*16;} $obj +->{$j}->{txJKL} /= 256; if($obj->{$j}->{rxABC} >= 2*5){ $obj->{$j}->{rxABC} -= 2*16;} $obj +->{$j}->{rxABC} /= 256; if($obj->{$j}->{rxDEF} >= 2*5){ $obj->{$j}->{rxDEF} -= 2*16;} $obj +->{$j}->{rxDEF} /= 256; } return $obj; } #################END

Please Read. I am sorry if the above code is Big. I am new here, and I don't know all the rules and how things are done here. I am sorry if it sounds complicated.I also want to tell you that the .pl file I have works and runs fine. And it returns Found when you run from command line. I have some idea what we are doing here in this code. I am parsing something out and checking if a certain packet is there or not. I am only concerned about rxABC packet inside the 0FFA (Banana packets). Please don't worry about the calculations inside the for loop inside the pm file. My questions are here below.

2.What is happening here? Am I trying to fetch totalRecords which is inside 0AA0 which is inside records and storing that value in numApplepackets?? And is obj a reference to the subroutine Fruits?

my $numApplepackets= $obj->{records}->{0AA0}->{totalRecords};

3.What is happening here?

$fruitobj = $obj->parse0FFA($BananaNum);

4.Please explain this step to me? THe -> operator confuses me.

for($BananaNum=1; $BananaNum le $numBananapackets; $BananaNum++){ $fruitobj = $obj->parse0FFA($BananaNum); for(my $j=0;$j<2;$j++){ if($obj->{$j}->{rxABC} > -100){ print "Found"; } } }

5.What is a shift keyword for here. What does it do?

my $self=shift;

6.Explain this step very briefly (i have a small idea)?

my $size=$self->{records}->{0FFA}->{totalRecords};

7.What is happening here? Am I fetching _timestamp of i from 0FFA from records to obj of i ??

$obj->{timestamp}=$self->{records}->{0FFA}->{$i}->{_timestamp};

8.What is happening in this part of the for loop?

for($j=0;$j<2;$j++){ $len=$len-13; ($tmp, $obj->{$j}->{txABC},$obj->{$j}->{txDEF},$obj->{$j}- +>{txGHI}, $obj->{$j}->{txJKL}, $obj->{$j}->{rxABC}, $obj->{$j}->{rxDE +F}, $data)=unpack("C v v v v v v a$len",$data);

I know it might sound a little complicated. Basically its a big file which contains a lot of packets. The packet of concern here is rxABC. Ignore the rest. I know the unpack command is just trying to expand the values inside the string. I am very grateful for you to have read this code and trying to help me. I am new to Perl and I really need to know what's going on here. This is not homework. And there is no time to go fetch a Perl book to read/learn right now, but I am. Just haven't gotten the advanced parts yet. And I have little time to figure this code out to make some changes. Also, its been some yrs since I have programmed. People forget things. Your help is much much appreciated. Thank-you!!

p.s: user ikegami from stackoverflow told me come here to ask q's :)

In reply to I have a perl snippet. And I need help understanding it. Can you help answer these questions. by Fighter2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.