Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

showascii

by photon (Novice)
on Jul 20, 2003 at 03:37 UTC ( [id://275985]=CUFP: print w/replies, xml ) Need Help??

- dumps ascii values of all characters matching \W - reads from file or stdin).. - useful for analyzing unkown binary file formats.. - files must fit into ram
#!/usr/bin/perl $filename = $ARGV[0] || '/dev/stdin'; $/=undef; open(FILE,$filename) or die $!; $file=<FILE>; close(FILE); $file=~s/(\W)/&replace($1)/eg; print $file; sub replace { return "(".ord($_[0]).")" }

Replies are listed 'Best First'.
(jeffa) Re: showascii
by jeffa (Bishop) on Jul 20, 2003 at 13:56 UTC

    If you are going to slurp an entire filehandle into memory, then do it like this:

    my $text = do {local $/;<FILE>};
    More consise, but also safer in that the undefining of $/ has been tucked away in a 'temporary' scope. But there is no reason to slurp the entire file into memory - you could have just as easily used a while loop to read one line at a time. In fact:
    perl -pe 's/(\W)/"(".ord($1).")"/eg' foo.bin
    works just as well and is more justified for not using strict. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://275985]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found