irDanR has asked for the wisdom of the Perl Monks concerning the following question:
'ello Monks!
I'm still quite the noob, as will soon be evident. In being tasked with modifying an ancient parser I soon noticed that it was using local $/=undef. Which I gathered is generally frowned upon. This is a stripped down version of the original file:
local $/=undef; open (NOTFOUND, "<transact.QDX"); open (NOTOUT, ">NotOut.txt"); my $lenOfFile = (stat(NOTFOUND)) [7]; while ($position <= $lenOfFile - 64) { sysread NOTFOUND, $record,64; chomp($record = $record); $opcode = unpack ("H*", substr($record, 0,1)); $subcode = unpack ("H*", substr($record, 1,1)); $position +=64; sysseek NOTFOUND, $position, 0; if ($opcode eq "60" and $subcode eq "01" ){ $plu = unpack("H*", substr($record,2,7)); syswrite NOTOUT, $plu . "\t" . $ymd . "\r" . "\n"; } }
In my situation there isn't a performance issue to worry about, I just wanted to do things 'right' from the get-go.
while ($position <= $lenOfFile - 64) { sysread NOTFOUND, my $record,64; push(@records, chomp($record)); $position += 64; sysseek NOTFOUND, $position, 0; } foreach my $currentrecord (@records) { $opcode = unpack ("H*", substr($currentrecord,0,1)); $subcode = unpack ("H*", substr($currentrecord, 1,1)); syswrite DEBUG, $opcode . "\t" . $subcode . "\n"; if ($opcode eq "60" and $subcode eq "01" ) { $plu = unpack("H*", substr($currentrecord,2,7)); syswrite NOTOUT, $plu . "\t" . $ymd . "\n"; }
In using my method I get blank output. The debug file I created shows that the opcode is always 30, and the subcode is just a box in notepad(i'm only using windows because i'm forced to at work, promise!). Funny thing is, there is no opcode 30 in the QDX file being read.
My fragile perl reality is crumbling around me. The two blocks of code seem like they should be accomplishing nearly the same thing. Perhaps my understanding of "local $/=undef" isn't well grounded? Is it a peculiarity with QDX files? What is going on?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Avoid using local $/=undef?
by moritz (Cardinal) on Nov 12, 2009 at 23:25 UTC | |
by irDanR (Novice) on Nov 13, 2009 at 00:26 UTC | |
by chromatic (Archbishop) on Nov 13, 2009 at 01:16 UTC | |
by desemondo (Hermit) on Nov 13, 2009 at 03:19 UTC | |
|
Re: Avoid using local $/=undef?
by bart (Canon) on Nov 13, 2009 at 00:19 UTC | |
by cdarke (Prior) on Nov 13, 2009 at 08:58 UTC | |
|
Re: Avoid using local $/=undef?
by desemondo (Hermit) on Nov 13, 2009 at 00:19 UTC | |
|
Re: Avoid using local $/=undef?
by graff (Chancellor) on Nov 13, 2009 at 03:18 UTC | |
|
Re: Avoid using local $/=undef?
by markkawika (Monk) on Nov 13, 2009 at 00:39 UTC | |
by JavaFan (Canon) on Nov 13, 2009 at 07:18 UTC | |
|
Re: Avoid using local $/=undef?
by shmem (Chancellor) on Nov 13, 2009 at 17:50 UTC |