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

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

am 31.08.2007 04:52:01 von jck11

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=;
..............
close(IN);

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

Re: how to write a "od -bc" like function?

am 31.08.2007 06:00:13 von Dummy

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

Re: how to write a "od -bc" like function?

am 31.08.2007 06:21:05 von 1usa

"jck11" 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=;


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:

Re: how to write a "od -bc" like function?

am 31.08.2007 08:56:52 von Josef Moellers

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;
> .............
> 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öllers (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

Re: how to write a "od -bc" like function?

am 31.08.2007 10:22:46 von Dummy

Josef Moellers wrote:
> 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.
>>
>> open(IN, "filename")or die;
>> my $str=;
>> .............
>> close(IN);
>>
>> By the way, if the file is a exe or image type, the $str seems don't work
>> correctly.
>
> 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