working with binary data in Perl...

Lennart Sorensen lsorense-1wCw9BSqJbv44Nm34jS7GywD8/FfD2ys at public.gmane.org
Fri Sep 29 21:02:51 UTC 2006


On Fri, Sep 29, 2006 at 03:42:35PM -0400, Lance F. Squire wrote:
> I'm attempting to wright a conversion program for an old byte oriented 
> picture description language (NAPLPS/Telidon) to SVG.
> 
> I currently have a script in Perl that will give me a human readable 
> output from a NAPLPS file. However the code looks like it could have 
> been written in BASIC.
> 
> eg:
> 
> if ( $Byte eq "\e" ) { $Desc = "ESCAPE"; }
> elsif ( $Byte eq chr(15) )
> 	{
> 	$Desc = "SHIFT-IN - SETTING 'GL' TO 'ASCII (G0)'";
> 	$GL = "ASCII";
> 	}
> elsif ( $Byte eq chr(14) )
> 	{
> 	$Desc = "SHIFT-OUT - SETTING 'GL' TO 'PDI (G1)'";
> 	$GL = "PDI";
> 	}
> 
> And doing bit shifts gets really interesting...
> 
> #Correcting for missing bytes
> $Pel{X} = chr(ord($Pel{X})<<$Shift);
> $Pel{Y} = chr(ord($Pel{Y})<<$Shift);
> 
> My question is: Is there a more elegant way to work with the binary data 
> in Perl?

I somewhat doubt it.  There are things like vec and pack/unpack, but
overall perl really prefers text.  Perl seems to like binary about as
much as C likes text. :)  And watch out for automatic type conversion in
perl.  It can really bite you.

$x = 0;
print "X is $x\n";
ioctl(MYFILE,0x5401,$x);

Does something different than

$x = 0;
ioctl(MYFILE,0x5401,$x);
print "X is $x\n";

While this does the same as the first

$x = 0;
print "X is $x\n";
$x += 0;
ioctl(MYFILE,0x5401,$x);

When playing with binary this just might matter.

--
Len Sorensen
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list