I am running perl v5.8.0 on Windows Vista via ActiveState perl build 824.
The following code parses HPGL commands out of a print stream and prints the command's offset in the file, the
command, and any parameters.
The code compiles without any errors or warnings when I run "perl -c hpgl2.pl".
However the output is mangled - some of the parameters are either missing or replaced with ';'.
I must be making some silly assumption or logic error in my code, but I cannot seem to find it.
Does anyone have any hints/suggestions?
The script:
This is what I am expecting to print:
This is what is actually printing:
Thank you for your time.
The following code parses HPGL commands out of a print stream and prints the command's offset in the file, the
command, and any parameters.
The code compiles without any errors or warnings when I run "perl -c hpgl2.pl".
However the output is mangled - some of the parameters are either missing or replaced with ';'.
I must be making some silly assumption or logic error in my code, but I cannot seem to find it.
Does anyone have any hints/suggestions?
The script:
Code:
#! /usr/bin/perl
use warnings;
use strict;
use constant OFFSET => 3_050;
{
local $/ = \1;
my ($pos, $line, $command, $value);
my %HPGL = (
'AC' => \&AC,
'IR' => \&IR,
'IW' => \&IW,
'PP' => \&PP,
'PW' => \&PW,
'RO' => \&RO,
'SC' => \&SC,
'SP' => \&SP,
'TD' => \&TD,
'WU' => \&WU
);
sub AC {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub IR {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub IW {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub PP {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub PW {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub RO {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub SC {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub SP {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub TD {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub WU {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, $value;
return 1;
}
sub ERROR {
my ($pos, $command, $value) = @_;
printf "%2s|%s|%s\n", ($pos - OFFSET), $command, "Unknown command";
return 0;
}
sub peek {
my ($nextChar) = @_;
my ($pos, $nextRecord);
$pos = tell DATA;
$nextRecord = <DATA> || "EOF\n";
seek (DATA, $pos, 0);
return 1 if $nextRecord =~ /($nextChar)/;
return 0;
}
$pos = tell(DATA);
until (peek("~")) {
$line .= <DATA>;
<DATA> if peek("[;\\s]");
if ((length($line) == 2) && (peek("[A-Za-z]"))) {
($command, $value) = unpack("A2a*", $line);
($HPGL{$command} || \&ERROR) -> ($pos, $command, $value);
$line = "";
$pos = tell(DATA);
} elsif ((length($line) == 2) && (peek("[^A-Za-z~]"))) {
until (peek("[A-Za-z~]")) {
$line .= <DATA>;
<DATA> if peek("[;\\s]");
}
($command, $value) = unpack("A2a*", $line);
($HPGL{$command} || \&ERROR) -> ($pos, $command, $value);
$line = "";
$pos = tell(DATA);
}
}
}
__DATA__
WU;AC14,4;SP1;PP1;PW0;TD1;IW;SC0,1.6933,0,-1.6933,2;RO0;IR0,100,8,100;~
This is what I am expecting to print:
Code:
0|WU|
3|AC|14,4
10|SP|1
14|PP|1
18|PW|0
22|TD|1
26|IW|
29|SC|0,1.6933,0,-1.6933,2
52|RO|0
56|IR|0,100,8,100
This is what is actually printing:
Code:
0|WU|
3|AC|14,4
10|SP|1
14|PP|1
18|PW|;
22|TD|1
26|IW|
29|SC|,1.6933,,-1.6933,2
52|RO|;
56|IR|,1
62|0,|Unknown command
Thank you for your time.