in reply to Pesky little URL files to HTML

A couple of nice idioms come to mind. I'd code filter() as:
sub filter { my $file = shift; local *FILE; open FILE, $file or die "Couldn't open: $file $!\n"; while(<FILE>) { s/BASEURL=// and return qq|<li><a href="$_">$_</a></li>\n|; } }
Even if that's too concise for you (localizing a glob automagically closes a filehandle when the glob goes out of scope, some people hate returning from within a loop, some people need to see an if there, though it deparses to the same thing), there's no need of doing a match and then a substitution. s/// has a return value for a reason. :) I do much prefer returning things than printing from within a function.

expand() could also be shorter:

sub expand { map { bsd_glob($_) } @_; }
You'll need to call it as @ARGV = expand(@ARGV);, or just do it all in place.

Of course, we could end up with:

print "<ul>\n", map { filter($_) } map { bsd_glob($_) } @ARGV, "\n";
It gets a little ridiculous, optimizing beyond that point. Still, you *could* do it that way.

Replies are listed 'Best First'.
Re: Re: Pesky little URL files to HTML
by hsmyers (Canon) on Dec 17, 2001 at 06:54 UTC
    This (your code that is) is too cool for snappy comebacks! But to make a feeble attempt, I can only ask: How do you put up with all of those annoying key words in your search for better otimization?—all right,all right I said it was feeble<g>

    –hsm

    "Never try to teach a pig to sing…it wastes your time and it annoys the pig."
      Does it help to say it comes with experience? Part of it is learning and becoming familiar with language features, and part of it is learning common programming patterns and technique.

      With Perl, you'll never unlock the full power of the language until you see lists and how to put them together. If you realize that you're just manipulating elements of a list, map jumps out at you. Since print takes a list, it's easy to do the right thing and to decouple the transformation (HTML-izing URIs) from the action of printing them.

      You don't usually see this all in one step, but if you do a couple of little transformations, you can open the door for bigger gains. They start to add up.