Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
Writing some Perl code to extract information about a file.
Specifically, I want to find an md5 checksum, the file size
in bytes, the mime string and the mime human readable.
Currently, I do this using shell executions (listed below)
which works fine except that the size is rounded to the
nearest KB (or block...all my readings are the same), but
was wondering if there was a native way in Perl to do this
(quicker, easier). I'm sure there is, but I've had a difficult
time finding anything online or in my books. It seems
surprisingly simple too. Can anyone help?How I am currently grabbing the information....throgh
Linux utilities...md5 = `md5sum <file_name>`
size = `du -b <file_name>'
mime (human readable) = `file -b <file_name>`
mime (string) = `file -ib <file_name>`Sample of results...
md5 = 9dkdnasksdjfkjalfjlajljf;ja; (whatever..a 32
characater hash)
size = 57382 (in bytes)
mime (human readable) = JPEG image data, JFIF standard
1.02, resolution (DPI), 72 x 72
mime (string) = image/jpeg
Many thanks for your help!!!!!!!

Since perl wraps a lot of the std C library functions I know there is 'stat' for the filesize (man perlfunc) and if perl doesn't do md5 natively then there's bound to be a module at CPAN, [just looked - there is] But the mime stuff is almost certainly not going to be native - look for a CPAN module or just console yourself with the fact that using tool chaining is the *nix way of solving problems efficiently anyway :-)

Thanks Wolfbone...the *nix tools work fine with the exception of the filesize. Above, I use 'du -b <filename>' but get a rounded out result. I need something more exact. So, I suppose the follow-up questions is, what is a *nix way of getting the 'exact' file size.
thanks for your comments. perhaps my way of doing things wasn't that bad in the first place.
Have a nice weekend!

Did you look up "stat" in the perlfunc manual page? It does just what the C library 'stat' does and one of the fields it returns is the filesize in bytes:
$bytes = (stat($filename))[7];
Other *nix commands like 'stat' and 'ls' will get you the filesize in bytes too but perl can do it without external help.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |