in reply to creating an image of all of your fonts

I suggest you spend a bit more effort on naming things better (and that you use strict when it doesn't hinder you, but that's another matter). As an example, try reading the main loop in this:
#!/usr/bin/perl -w use strict; # untested; merely to prove a point use GD; use File::Spec; use constant FONT_PATH => '/full/path/to/fonts/dir/'; use constant HEIGHT => 800; use constant WIDTH => 320 use constant COL_WIDTH => 195; use constant LINE_HEIGHT => 20; use constant LEFT => 5; use constant TOP => 20; my $im = GD::Image->(WIDTH, HEIGHT); $im->colorAllocate(255,255,255); my $black = $im->colorResolve(0,0,0); my @font_name = do { opendir my($dh), FONT_PATH or die "Couldn't open ", FONT_PATH, ": $!\n"; grep { $_ ne "." and $_ ne ".." } readdir $dh; }; my $x = LEFT; my $y = TOP; for (@font_name) { if($y > HEIGHT) { $x += COL_WIDTH; $y = TOP; } my $ttf = File::Spec->catfile($fontDir, $_); $im->stringTTF($black, $tff, 12, 0, $x, $y, $_) or die "Failed trying to draw $_: $!\n"; $y += LINE_HEIGHT; } open my $fh, ">", "fonts.png" or die "Couldn't open fonts.png: $!\n"; binmode $fh; print $fh $im->png;

In your code, it's a lot less obvious what's really going on. I like to believe that the renamed variables and the constants here made it extremly clear what's going on. Also, it's easy to find what "special values" a program uses that way, and they're also easy to customize.

Also, try to use lexical filehandles wherever possible. It just makes sure you'll never accidentally step on anyone else's foot.

Of course this is just a small script and wouldn't really justify the critique, but these things become second nature and happen without thinking even when you're writing small scripts, if you've gotten into the habit. And the habit will serve you well in large projects.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: creating an image of all of your fonts
by AssFace (Pilgrim) on Mar 11, 2003 at 02:56 UTC
    Thanks for the tips! I need to work on that I guess. That was obviously just a quick script to get the job done - but you are right, in terms of sharing with people I guess it was messy, I didn't thing of that as an issue - but I will defintely work on that for any future postings.