learning to use warnings, the hardway

Trying to write to file names instead of file handles by way of typographical error, yes I am a super schmuck! Trying to figure out for at least 5 minutes why the parant isn’t getting any output from the child process, and sure enough…. Don’t print $filename when you mean print $filehandle, haha! (or better yet, don’t use ($lhs, $rhs) and ($lfd, $rfd) for the file names and handles when tired)

Tried running my scratch-file under use warnings, and sure enough the expected warnings poped up. This is what I get for not enabling warnings when writing a function in a separate file for testing crap. Hmm, what was it she said to me years back? Something like “We need to pump some caffeine into you”, maybe it’s not a bad idea lool.

Rule #1 of Perl, use warnings or die $!
Rule #2 of Perl, use strict or warn $!
Rule #3 of Perl, goto Rule #0

(use what or … is not actually perl syntax, let a long setting $!, and is infact a compiler error that would have to be trapped in an eval, but hey at this time of morning it works well enough in ascii :-P)

A C programmer can write C in any language, until he gets lazy and remembers Perls regular expressions still work at four thirty in the morning.

GET SOME SLEEP SPIDEY01 !!!!!!

I’m wondering for an hour, why the frig I’m getting missmatched checksums…. then it hit me, and I feel like punching myself in the head lol.

I tried this:

sub checksum {
my $file = shift || return;

my $md5 = Digest->new("MD5");
my $sha = Digest->new("SHA-256");
open my $fh, $file or die "Can't checksum file: $!";
binmode($fh);

return ( MD5 => $md5->addfile($fh)->hexdigest();
SHA256 => $sha->addfile($fh)->hexdigest();
);
}

and then compared the output agaisnt checksums generated by FreeBSDs md5/sha256 utilities (they are actually the same program). The MD5 matched perfectly, but the SHA256 were different.

After much thinking, I figured it out….

sub checksum {
my $file = shift || return;

my $md5 = Digest->new("MD5");
my $sha = Digest->new("SHA-256");
open my $fh, $file or die "Can't checksum file: $!";
binmode($fh);
my $mh = $md5->addfile($fh)->hexdigest();
seek($fh, 0, 0); # <----- you've got to rewind it to the start of the file before the next I/O
my $sh = $sha->addfile($fh)->hexdigest();
return ( MD5 => $mh,
SHA256 => $sh,
);
}

The thing that really irks me, I was wondering if the Digest:: classes would be so nice, as to rewind the file handle when they were done, but I think that idea occured to me about 4 hours ago, lool. Actually, if I’ve got to rewind the fscking handle between objects, I may as well just walk the file and append the data to each as we go… then get the digests.

I really need more sleep….

EDIT: Take III

=pod internal

checksum FILENAME

Open filename, and return a hash containing ALGORITHM => check sum
key/value pairs, for each algorithm supported. Both MD5 and SHA256 will
always be supported.

=cut

sub checksum($) {
my $file = shift || return;

my $md5 = Digest->new("MD5");
my $sha = Digest->new("SHA-256");
open my $fh, $file or die "Can't checksum file: $!";
binmode($fh);

# suck the file, and Digest::add it. We would have to parse
# the entire file per object, and seek to 0,0 between calls other wise.
while (my $ln = <$fh>) {
$md5->add($ln);
$sha->add($ln);
}
close $fh;

return ( MD5 => $md5->hexdigest(),
SHA256 => $sha->hexdigest(),
);
}

Hmm you know you’re crazy when

instead of writing the file path, and using tab completion to view a file listed in a programs output: you pipe the programs output into a command to get the desired line, pipe then pipe that into AWK, so you can pipe that into xargs ….

or maybe it’s just being lazy….

Terry@dixie$ perl script-name project --debug | head -n 1 | awk '{ print $2 }' | xargs cat

Perl promotes laziness

#
# We need to open a set of files for writing, and reference the paths later;
# the hash gives us a nice way to work with them later.
#
my %lgfiles = ( pidfile => "${datadir}/${0}.pid",
errlog => "${datadir}/${0}.err"
... => ... );

# but how do we open the set of files?


# this is a lot of typing, but common style in some places
open PIDFILE, ">", $lgfiles{pidfile} or die "some message: $!";
open ERRLOG, ">", $lgfiles{errlog} or die "some message: $!";
open ..., ">", $lgfiles{...} or die "some message: $!";


# this is better, but still too much typing
{
my $msg = 'some message:';
open PIDFILE, ">", $lgfiles{pidfile} or die "$msg $!";
open ERRLOG, ">", $lgfiles{errlog} or die "$msg $!";
open ..., ">", $lgfiles{...} or die "$msg $!";
}


# this is handy
#
# open each key in %lgfiles for writing to as KEY
#
while (my ($k, $v) = each %lgfiles) {
open uc($k), '>', $v or die "can't open $v: $!";
}

# same thing, but faster to read (IMHO)
#
# open each key in %lgfiles for writing to as KEY
#
map {
my $fn = $lgfiles{$_};
open uc($_), '>', $fn or die "can't open $fn: $!";
} keys %lgfiles;

For some reason, the intregration of regular expressions, qw/quote words/, map {} @list, grep {} @list, the $_ default variable, and the do_something or die $! thing are my favorite features of Perl. While in most other languages, the only great feature I get to enjoy is the trinary/ternary ?: operator, when there’s a place that it improves readiblity and reduces visual clutter ;-).

Ok, so my biggest beefs about Python 2.5 is no ? : and having to import re, hehe

Camels rock

Been reading a bit more of Programming perl, I left off at the start of Chap. 7 “Formats” for the night, so I could get to taking care of things before work tomorrow (I haven’t been on the computer all day lol). I enjoyed the later parts of the chapter on regular expressions; although I’ve known regular expressions (hence forth regex) for a long time now, I *loved* getting to read more about how the parser & engine works; it has changed the way I think about the /PATTERN/ (and just how much hard work perl does to deal with greedy people).

I also learned a few new things, namely just how advanced Perls regexes are: I’ve always felt that Perl must be the king & queen of regex implementations…. after seeing all the (?…) stuff that is referenced in the book (the ones I didn’t know yet), I think Perl must be the king, queen, jack, and ace; or as my hanging jaw expressed, “Fucking brilliant!” ;-). The other things, is how in the universe can ‘pos($str) = 1;’ could possibly be legal, rather then just an interesting form of magic; and how functions that skip the dang comma after a block, like the built in ‘map { block } @list’ does, can be defined. Which interestingly, also explains how modules like Error and Exception from CPAN likely do there stuff.

This is getting interesting hehe.

Just finished the preface maybe 15min ago, and even by then, I could tell I was gonna like this book xD

stressful, yet economical day

I logged off around 0530~0545R, I couldn’t sleep…. was around 0730’ish when Ma got up, and aroun d800 by the time the woke me up to start shuffling crap around; operating under the assumption that the maintenance guy would have to replace the entire water heater in order to deal with the now kaput elements. Managed to clear the forums a bit & get throuhg it. Then she decided to go shopping; I managed to get to BestBuy and Barnes & Noble.

Spent my gift card towards a (DDR-333) PC-2700 SO-DIMM, got my laptop sold 512MB one sitting on a dresser now. So far, it seems the system has become more processor limited, but hey, GNU emacs starts quickly now >_> (but I’m a vim user :-P). I also bought a little Jumbo LapDesk to replace the chestboard I’ve been using as a tray table for my laptop. I don’t like the slippage, but it’s sound enough when in ‘use’, but I wouldn’t want to use it for storage though – for fear the laptop will slide off the lapdesk, off the table, and onto the hard deck lol. As a replacement for the chessboard, I miss how the laptop used to stick in place on the surface, I kinda wish that I got the wooden lapdesk for about $10 more, but hey… I’m a cheap bastard. The only reason why I’m replacing the chessboard, is because last week I was wondering why my left leg was hurting around the inner-side (Tibia?), when about 3 days later I found the edges of the chess board were starting to cut into my leg :.

Found a much wider scope of books at the store then I expected, basically every kind of Linux (especially Fedora / Ubuntu) and Windows server book out there; VisualBasic, heck of a lot of ASP.net (barf), sweet stockpile of Java & JavaScript books, even C#, Python, and Ruby books; even PHP & MySQL. I didn’t find anything I had *wanted* to find though, stuff like the works of W. Richard Stevens (especially those on network programming), or some of the books on LaTeX I have interest in (as opposed to flipping through webpages of documentation). not to much of a surprise though. I was rather disappointed with the amount of computer related and science books there, but alas, that’s the world we live in — I didn’t really expect to find anything better then “Learn Visual Basic in 20 minutes” or something useless like that 8=). I did however find something worth buying, the llama, Camel, and the Cookbook. I hated to pay the price ($50), but I bought Programming Perl; hopefully it will come in handy someday.

I know both programming and Perl well enough, that the cook book doesn’t interest me that much, not for $50 lol. Learning Perl, is good for refreshing my memory if I never touch the language for over a year or so… But I’ve never read Programming Perl, no idea where to put it on my book shelve yet either lol. The thing I’m mostly interested in, is not having to look for the manual pages / plain old documentation for various things and switch between windows back / forth while coding. Although, I do have both perldoc.perl.org and search.cpan.org on keyword access in Fx, it’s a bit of a poor substitute for a few book marks hehe. I was also a good boy, I kept myself firmly away from the science fiction and fantasy sections…. I’d be to tempted to pick up good reading material, lol.

I almost never buy computer books really, most of the ones I have were $1 at a library book sale; since I’m the only mook around who would care, bought’em lol. That’s actually how Java entered my skill-set, even though I often avoid writing Java code when I can. Most of the books I get, usually are Sci-Fi or technical material related to such topics >_>. On computers, I’m used to using documentation and reading comprehension to understand stuff.

All in all, I guess a fair day…. NOW IF ONLY I COULD GET SOME SLEEP !!!

Why I love Perl…..

For a little project, I basically need a quick but simple way of translating things from one format to one of another; the catch? How to do it! I figured, a hash would be a great way to do it, since each of the translations can be quickly processed as translate_from => translate_to. The problem? There’s a fair # of keys and values, most hashes I encounter in Perl are written like this:

# modern
my %hash = ( foo => 1, bar => 'string', );
# classic
my %hash = ( 'foo', 1, 'bar', 'string', );

the main value of the arrow operator => is that we get to skip quoting the hashes key… but wtf good is it, if we have to quote each value, because they are all strings in our hash? So I sat and thunk for about 45 seconds and wondered…. wouldn’t it be so wonderful, if I could say something like qw/key1 value1 key2 value2 …/ and format it much like the classic style for legibility, but not have to write *any* quotes for the strings?

Attempting it 2 minutes later:

my %hash = qw/foo  1
bar string
... ...
/;

It never actually occurred to me before, b/c almost every time I care about hash keys, is w hen I can use a hash as a form of quick dynamically created storage for data, that I will probably want to walk across both the keys and values on later. But since qw/foo bar/ is equivalent to (‘foo’, ‘bar’), it works just perfectly here!

Perl is not the most beautiful language, it’s a language for *getting the job done*, and in that spirit, has 1000s of little things that help make things less painful -> like not having to quote every dang gum key value in a hash lol. One thing I also like abut Perl, I can always read my scripts 6++ months later 😉

I love this tool, lol.