Solution [was Re: New Quiz: "What does this code do?" (3-Jan-2006)]
Shlomi Fish <shlomif-ik1l9ssToec+JF/[email protected]> Sat, 7 Jan 2006 11:32:44 +0200
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Well, what the program does is:
1. It accepts as a first command line argument a pathname. (In my case the
current working directory.)
2. It processes the rest of the command line arguments, and expects an alias
and then an equal sign and then an path. If the command line arg does not
contain a slash it dies.
3. It tries to see if the CWD begins with the path. If it does (and it is
indeed a full path that is either the entire CWD or has a slash afterwards.),
it replaces the path with the alias and prints the modified string (and
exits).
4. If nothing matches, it just prints the string.
What is it good for? In my bash setting for working on the Test::Run module in
which there are long path names, I have the following:
<<<
prompt()
{
perl ~/bin/prompt-cmd.pl "$(pwd)" "\$trunk=$trunk" "\$base=$base" \
"~=$HOME"
}
PS1="\\u:\$(prompt)\\$ "
>>>
~/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
>>>
Which leaves me a lot of space to type the rest of the command.
Regards,
Shlomi Fish
On Tuesday 03 January 2006 14:36, Shlomi Fish wrote:
> In the Israeli Perl Mongers mailing list we used to hold quizzes in which I
> would paste Perl code snippets and people needed to understand what they
> did, and possibly guess for what purpose they served.
>
> I am going to import this tradition here, and have a "What does this code
> do?" quiz. You are given the following Perl program, and have to understand
> what it does, and why was it written.
>
> Here goes:
>
> <<<<
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $string = shift;
> foreach (@ARGV)
> {
> if (/^([^=]+)=(.*)$/)
> {
> my ($v, $s) = ($1,$2);
> if (substr($string, 0, length($s)) eq $s)
> {
> if ((length($string) > length($s)) ?
> (substr($string,length($s), 1) eq "/") :
> 1
> )
> {
> print $v . substr($string, length($s));
> exit(0);
> }
> }
> }
> else
> {
> die "Bad";
> }
> }
> print $string;
>
>
> Before the 60 hours quota is over, you can send your solutions to me in
> person (shlomif-ik1l9ssToec+JF/[email protected]).
>
> Regards,
>
> Shlomi Fish
>
> ---------------------------------------------------------------------
> Shlomi Fish shlomif-ik1l9ssToec+JF/[email protected]
> Homepage: http://www.shlomifish.org/
>
> 95% of the programmers consider 95% of the code they did not write, in the
> bottom 5%.
--
---------------------------------------------------------------------
Shlomi Fish shlomif-ik1l9ssToec+JF/[email protected]
Homepage: http://www.shlomifish.org/
95% of the programmers consider 95% of the code they did not write, in the
bottom 5%.