in reply to Question about Binary files

I hate to bother the horse again but... you need to binmode() your filehandle when dealing with binary files.
open FH, "<binary.dat" or die "Can't open " $!\n"; binmode FH; while (<FH>){ # code here } close FH;

--perlplexer

Replies are listed 'Best First'.
Re: Re: Question about Binary files
by Anonymous Monk on Apr 19, 2002 at 15:51 UTC
    That horse was already bothered and I had already used the binary FH c +all and got the same results. But I think I have pinpointed the exact location of the problem: it's +in the encode_base64 code itself - on the last few lines: sub encode_base64 ($) { my $res = ""; my $eol = "\n"; pos($_[0]) = 0; while ($_[0] =~ /(.{1,45})/gs) { $res .= substr(pack("u", $1), 1); chop($res); } $res =~ tr|` -_|AA-Za-z0-9+/|; #!!! Here's where the different characters are created!!!! # my $padding = (3 - length($_[0]) % 3) % 3; # $res =~ s/.{$padding}$/"=" x $padding/e if $padding; # if (length $eol) { # $res =~ s/(.{1,76})/$1$eol/g; # } $res; } I have borrowed this code and don't know what it's trying to do - is i +t a standard? Since I have no MIME access on my host, and I'm using U +NIX - is it possible another encoding function exists, something like + uu?encode? Thanks again. Peter.
      I have borrowed this code and don't know what it's trying to do - is it a standard?

      Don't know if that code is standard (probably not, since it doesn't seem to work). But base64 is certainly a standard, which is documented here -- start at page 23.

      Also, follow up on perlplexer's advice, if you can. Got a home directory, some disk space, and a shell prompt? download the MIME module for perl yourself, make a subdir in your home called "perl_modules", install the download there, then add "-I/home/you/perl_modules" on the shebang line of your perl script that uses the module. Perl will find it.

      www.cpan.org will have some info and methods that will make this easy.

      Well, it wasn't obvious from your post that you used binmode().

      I am not that familiar with Base64 encoding algorithm and I don't know if the code that you posted actually works; What I do know is that it's slow - heavy use of regexes and even one s///e where simple substr() would probably suffice.

      Do you have shell access? If so, download and install MIME::Base64. You do not need any special privileges if you install it into your home directory.

      --perlplexer