Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • I think that gcc's nested functions are fun. I wish they'd been
    added to the C99 standard....

    A fun ML thing is partially applied functions: If foo takes two
    args, a and b, then (foo a) is the same as (fun b -> foo a b).
    Of course, to do this you need static typing.
  • Or even more easily generated choices:
    foreach my $cmd (keys %choices) {
      for my $i (1..length $cmd) {
        $choices{substr($cmd, 0, $i)} = $choices{$cmd};
    N, NO, NOR, NORT, NORTH....


    • That will only work as long as the choices are unique on the first letter.

      It would be much better to use the Text::Abbrev module (it's part of the core). It'll create unique abbreviations. Of course the code would be a little more convoluted:

      use Text::Abbrev;
      my %abbrev_choices;
      my %abbrevs = abbrev(keys %choices);
      foreach my $abbrev (keys %abbrevs) {
        $abbrev_choices{$abbrev} = $choices{$abbrevs{$abbrev}};
      %choices = %abbrev_choices;

      (or something like that - completely untested and of my hat)

    • Or, rather than filling the hash with small keys, make a smarter dispatcher:

      # find those commands which could match
      @cmd = grep { /^$cmd/ } keys %cmd;
      # die if none or several commands match
      die "Ambiguous command $cmd (@cmd)" if @cmd != 1;
      # call the routine

      Mmm, $cmd, @cmd and %cmd used in 3 lines... That's one point in the Perl purity test. :-)