For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > August 2007 > how to write a "od -bc" like function?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author how to write a "od -bc" like function?
jck11

2007-08-30, 10:45 pm

hi all
Could any one give me a direction to write a function to match the unix-like
function "od -bc" do.
Now I only get the idea of opening the file.

open(IN, "filename")or die;
my $str=<IN>;
..............
close(IN);

By the way, if the file is a exe or image type, the $str seems don't work
correctly.



John W. Krahn

2007-08-31, 5:00 am

jck11 wrote:
> hi all
> Could any one give me a direction to write a function to match the unix-like
> function "od -bc" do.


http://search.cpan.org/src/CWEST/ppt-0.14/bin/od


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
A. Sinan Unur

2007-08-31, 5:00 am

"jck11" <jck11@msr.pchome.com.tw> wrote in
news:fb7vth$fq5$1@netnews.hinet.net:

> hi all
> Could any one give me a direction to write a function to match the
> unix-like function "od -bc" do.
> Now I only get the idea of opening the file.
>
> open(IN, "filename")or die;


Prefer the three-argument form of open.
At least give an indication why the call failed.

open my $in, '<', 'filename'
or die $!;

> my $str=<IN>;



Here, you read only one line from the file.


> .............
> close(IN);
>
> By the way, if the file is a exe or image type, the $str seems don't
> work correctly.


Study my hexdump example at:

http://www.unur.com/comp/ppp/hexdump.html

You should be able to adapt that to produce the same output od -bc
produces by changing only the hexdump_line subroutine.

Keep in mind:

You must read the input file in binary mode.

Line-by-line processing does not mean much here as you are trying to
display fixed sized blocks of bytes.


Sinan

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

Josef Moellers

2007-08-31, 5:00 am

jck11 wrote:
> hi all
> Could any one give me a direction to write a function to match the unix=

-like
> function "od -bc" do.
> Now I only get the idea of opening the file.
>=20
> open(IN, "filename")or die;
> my $str=3D<IN>;
> .............
> close(IN);
>=20
> By the way, if the file is a exe or image type, the $str seems don't wo=

rk
> correctly.


Apart from the usual: three-argument "open", lexical filehandles, and=20
explicit '<':
1. if you're dealing with binary files, you must tell perl this:=20
"binmode IN;" after open(...), and
2. you're not reading "strings" of *characters* but "blocks" of *binary* =

data, so better use "read(...)".

so
perldoc -f binmode
perldoc -f read

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef M=F6llers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

John W. Krahn

2007-08-31, 5:00 am

Josef Moellers wrote:
> jck11 wrote:
>
> Apart from the usual: three-argument "open", lexical filehandles, and
> explicit '<':
> 1. if you're dealing with binary files, you must tell perl this:
> "binmode IN;" after open(...), and


Or use the open pragma:

use open IO => ':raw';



perldoc open



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com