Re: Solution [was Re: New Quiz: "What does this code do?" (3-Jan-2006)]

Aaron Crane <[email protected]> Sat, 7 Jan 2006 18:22:54 +0000
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
Shlomi Fish writes:
> perl ~/bin/prompt-cmd.pl "$(pwd)" "\$trunk=$trunk" "\$base=$base" "~=$HOME"
> 
> ~/bin/prompt-cmd.pl is this script, and as you can see it translates the 
> current working directory into something managable like:
> 
> $trunk/modules/Test-Shlomif-Harness

I can see how that would be handy.

I'd have written the program something like this, expressing the string
processing logic with a regex substitution rather than with substr and
explicit conditions:

  my $dir = shift @ARGV;
  for (@ARGV) {
      my ($abbrev, $prefix) = /\A ([^=]+) = (.*) \z/xms or die "Bad";
      $dir =~ s{\A \Q$prefix\E (?= \z | /) }{$abbrev}xms or next;
      print "$dir\n";
      last;
  }

-- 
Aaron Crane