A couple of days ago I was toying with the idea of creating a new obfuscation/Japh, and towards that goal created a very simple program to display random characters from a given set so that I could see which symbols looked good together.
My first stab at the program was working okay, and was basically something like this:
#!/usr/bin/perl -w use strict; use warnings; # # Try chars from space ' ' (ascii 32) up until # tilde '~' (ascii 126). # foreach (32 .. 126) { my $c = chr($_); try_char($c); } sub try_char { my ($new_char) = @_; print "=== Character '$new_char' ===\n"; my @chars = qw( . o O ); if ($new_char) { push @chars, $new_char; } for (my $i = 0; $i < 800; $i++) { my $r = $chars[int(rand @chars)]; print $r; } print "\n"; <STDIN>; # Wait for user to hit [RETURN] }
The program would display a screenful of random characters each time the subroutine try_char was called, from the basic set {'.', 'o', 'O'}, with a single additional character temporarily added to the basic set on each call to the subroutine. At the end of the subroutine, the user (me) could view the random mix of characters, and then type <RETURN> to continue to the next subroutine invocation.
This worked fine, and produced the following sample outputs:
=== Character ' ' === OoOO oOo.ooOoO ooO o . oOOOOOO.OO oo . O o.o.Oo...O..OoOOooOO oooo..O +OOo......O O.o o .O.o o .O O. OO. . o OooO ..oO.ooOOOo Oo . o.ooooOoo .O oO. +oO..Oo.ooO O.o Oo. O O.ooO.oo Oo. oO. Ooo..o..O Oo.OO.O.O..Oo oOOo.OO..oOoOOo. +OooOoOOOo. OO..O. oOOOoooOOoO.OO. OO. O.o.. O.O..oO.O.oo oo.OoO OOoo.oOOoOo.Oo. + OOOOoo O OO O oO.oO o .Oo..oo. .Oo.Ooooooooo Oo o. O.o .Oo.oO... o.. O +Oo o.o.O . . oo oo .O.o .O ..oO.OOo.OO O OO .OooOooooOo . O. .OO...OoooOOO. .O.o +oOo OoO. o o OoO. OoOo.OO O o. oO .o .. o.oOOO Oo.OOo . o.oO. .oo ..oOoo oO +..OO O.O . Ooo OO.O.Ooo o o O. ..OooO O oo Oo.O.oo.o OO.o oo OO ooo .. .Oo O + o.ooO. . oOOO. .oo.o. oo O O ..o Oo.O...O o.O.ooo O oOoo OoO .oo.OO.O.O.OO + .. ... oo OOo o .Oo. ooO Ooooo.O O.oOoOooo OOoo. O...oO OOOoO.O.O o. .Ooo.OoOo. + .O OO O.o === Character '!' === !.!oo!oO!!Oo!!.o..OO.oOoO.O.ooo!.!OO!!oooOoO!!.OO..!OO!!!O!.!!O!!.!O.O +!.oO..o.oO OO.o.O..o.!oOO..O.oOoo!!!..!O....!o!!!!.oo!o.O.o!.OO!.OooO.!.OO.OO!OOo +.!OO!O!... .O!O.ooOOo!!!o!o!.ooo.o!..!o!O.oO.oOo!O.!.O.Oo.!O!!!!oo!.!.oo..o.oOo!! +.OO..O!ooo oo.!.!!OOO!.Oo!O.o.O!.!o.O!Oo!O.oO.!oOo!!OOO..!!OO!O.!OOOOO!!...o!oOo! +OooOOO!!oO .ooO.oOO.oo!oo.o.O.!.!..o.oO!!..O..oo.oo!!oo!.!!.O!Oo!!ooo.!!!o.!o.oOo +!OO.O.ooo! OOOO!o.O!!Oo!O.Oo!!!Oo.!..O!!o.o.O!!O.!OOooo!!!.Oooo..!o.O.oO..!oOO.O! +o.!ooo.O!! ..o.o!O.OoOo.!Oo..!.O.oo!o!O!O.!!!O!!O.oOOo!ooO.!!!.!oo.o.!oo.o.!!.O!o +oo!O!o!OO. !o!.!!.O..!.O.oooo.O!.oOO.!..!o.OOo.oOOO.o..!!O.!!OOOoo.o..!!oo..o!.Oo +oOOoO!!!.. .OOO.!oO!!!!ooO!..o.!!.oo..!OOo!.o..OO!.O!.O!o.o!OOOOo!o!O!!oOo.O.O.!o +oo.!.Oo..O .o.oOOOOo.o!o.oo.ooOooOO!!OO.o!.!O!O..O.!.O!!oOOoo!!.Ooo.O.OoO!o!.oO.o +OOo.Oooo!o === Character '"' === ""o.o.o..oo.O.O."o"OoOO"""o..O"OOo.OOOoO".."".oO.."..o.""O.oOoo.o.""o" +.o.o.o"."" O.o.."..O.o.".O".oo."OOOoo"Oo..".O...o."oo"."oOo"O.o."OO."o"O.o"O".oO" +oOO..""""" ""OoO""O.."O.Oo."oOooooO"o.OoO".""OO"OOO".."oOo"o".OoO""Oo.OOoooO."..o +O"..."ooo" o.ooO.O""o"o.."o""O".".Oo..oO.o.".oo""Oo"o..oOOooo""o.Oo"."O"".oO"".O. +."OOO...". Oo.."."oO....o"ooo."Oo."o.o.o""."Oo.O"."oo".o.o.""oO.oooO"Oo..O""OO.oO +O.O.o.oo"o o".O"oOoOooO"oooooO.OO.."..""o"oOo."O"o.o.""ooOo.ooO.ooOo.oOo"Oo.o"".. +o".OoOoo.. O."oO""OOO"oOO"oOOoo.oo""o.O""o"..o.O""o".o.Oo"oOO".""OooO""Oo."oO."O" +oO""".OOO. oOO"".o"OOo"oO.Ooo.""oOO"o."o"oOOoOooO.o..Oo.o"oOO"oOOoo".."O."Oo."O." +"O"O"..O"o "o."o".oO"O.OooO.."oo"."""...o..o."O".O.O"o...""..OO"o"o.""Oo"O."o"Ooo +Oo".o""... oO...o""OOO.".O.oOOoo"oo".o."o"."oo.o"""OO."o.o.O.o.."o."o.""..O"o"o"O +oO."."OOo"
At this point I decided to skip all of the alphanumerics ('a' ... 'z', 'A' ... 'Z', '0' ... '9'), and thought it would be easier to manage it in a map, so as a first step, I rewrote the foreach loop thusly:
#foreach (32 .. 126) { # my $c = chr($_); # try_char($c); #} map { my $c = chr($_); try_char($c); } (32 .. 126);
I was surprised to see that typing <RETURN> no longer let me advance to the next iteration! Instead, the program would echo the newline, and yield no new output at all.
At this point, you may want to try the program for yourself to see if you can discover what's happening. If you'd prefer not to (or if you get stuck), here is what I learned:
I put a number of print statements into the program to try to see where the program was "blocking". When I did the following, though ...
sub try_char { ... print "\n"; <STDIN>; # Wait for user to hit [RETURN] print "TFD> Leaving subroutine 'try_char()' ...\n" }
... the program resumed working as before. It finally dawned on me that, by changing a foreach to a map, the program was now calling try_char() in list context instead of scalar context. What that means is that the subroutine try_char() was being asked to return a list of items, and the return value from the subroutine, <STDIN>, in list context, returns all lines from standard input until the input stream is exhausted (not just a single line).
This is easy to verify by doing a number of things:
These are also all made easy to verify by adding the function wantarray to the subroutine:
Which will display either "I do NOT want an array" or "I DO want an array", depending on what the subroutine thinks it is being asked to produce.sub try_char { my ($new_char) = @_; print "=== Character '$new_char' ===\n"; printf "\e[%s want an array\e[m\n", wantarray? "101mI DO": "102mI +do NOT"; my @chars = qw( . o O ); ... }
Although at first glance something quite strange was going on, it was nice to see that Perl was simply doing exactly as I had asked it to. The default for a map is to evaluate things in LIST context, which is why my subroutine was no longer being satisfied with a single newline returned from the input stream, but rather wanted to "have it all".
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Taken out of Context
by Aristotle (Chancellor) on Jul 25, 2006 at 12:02 UTC | |
by liverpole (Monsignor) on Jul 25, 2006 at 12:26 UTC |