Hi,
Here is my entry for the Supremely Unorthodox/Artistic Prizes, for 30
characters:
#!/usr/bin/perl -p0
y/
//d;fork||die y///c."
"
Here comes the explanation:
One of my first tries was this:
32 -n0 y/\n//d;print;die y///c."\n"
-0 changes $/ to "\0" so combined with -n it gives $_ the whole file at he
first iteration of the loop. Then the y///d removes all the newlines at
once.
warn and die do not print "at script line x, <> line y" if the message
ends with a newline. $/ is much shorter than "\n" (even with an embedded
newline), but I can't use it because of -0, which I already used to modify
$/.
-p would be nice to use, since it would spare me the use of a print.
This is what Perl adds around your code when you use -p
LINE: while (defined($_ = <ARGV>)) {
#( your code );
}
continue {
print $_;
}
The main problem is that you can't die in the middle of a -p loop.
If you do, you die *before* the print statement is ever reached and $_ is
never printed (since the print happens in the continue statement).
Anyway, since you can't use -p, you have to print before you die. Which i
did in my earlier version.
But really, the cool trick would be to die AND to use -p.
I don't remember exactly how I thought about it, but the main idea is to
continue after you die. That's what children are for, aren't they?
So I came up with this:
33 -0p y/\n//d;die y///c."\n"if fork
fork returns 0 to the child process, and the pid of the child to the
parent process. So 'if fork' really means "if I am the parent". Well, if
you are the parent, you die. And your child lives on, inheriting the same
value of $_ that you patiently expunged of all its \n, which you left for
it to print.
This can be shortened a little with an embedded newline in the y///
31 -0p y/
//d;fork&&die y///c."\n"
And another one in the warning:
30 -p0 y/
//d;fork||die y///c."
"
Another fun thing to notice, is that you can choose which process does
what when you share this load between them:
fork&&die let the parent die and print the length of the string to STDERR,
while the child continues and prints $_ to STDOUT
fork||die let the parent survive until it can print $_. It's the child
that dies and print the length of the string.
I suppose fork&&die is more moral.
-- BooK
Off-topic Post Scriptum: how to use fork to explain the meaning of life:
* fork||die infanticide
* fork&&die one heir
* {fork||redo} a dynasty of only children
* {fork&&redo} how rabbits conquered Australia
--
Philippe "BooK" Bruhat
It matters not how grand your plans when they are built on a faulty
foundation. (Moral from Groo The Wanderer #19 (Epic))