This is a meditation about solving a problem which was inadvertently introduced into a working Perl program, and and an invitation to the reader to discover for himself/herself the cause of the problem.

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:

  1. Instead of typing <RETURN>, type ^D (in Linux) or F6 (in Windows), or whatever the corresponding end-of-file character is for the current O/S.
  2. Instead of <STDIN>, have the subroutine return scalar <STDIN>.
  3. Change the caller of the subroutine from try_char($c); to scalar try_char($c);.
  4. Change the caller of the subroutine, so that instead of try_char($c); it does my $s = try_char($c);.

These are also all made easy to verify by adding the function wantarray to the subroutine:

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 ); ... }
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.

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".


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

Replies are listed 'Best First'.
Re: Taken out of Context
by Aristotle (Chancellor) on Jul 25, 2006 at 12:02 UTC

    I always considered it good practice to stick an explicit plain return in every sub that’s not meant to have an actual return value, in order to avoid random stray bits traveling around unexpectedly. Your meditation just gives me another reason for the practice.

    FWIW, I’m not sure why you thought map would be easier than foreach. You have more control over the latter (you can use next and last, whereas with map you have to clumsily produce an empty list in order to skip a value and can’t abort early at all).

    PS.: your title gave away the riddle prematurely…

    Makeshifts last the longest.

      Hi Aristotle,

          I’m not sure why you thought map would be easier than foreach.

      Originally I didn't have a foreach, it was actually just a for (my $i = 32; $i < 127; ++$i).  That's why I said "... basically something like this ..."  I modified it later to have a similar "look" as the (almost) equivalent map construct.

      And your point about "next" and "last" is both correct and ironic, because the first thing that happened when I switched to map was that I ran into the problem of next not being usable and having to be written differently, which reminded me of this recent question.

      I hoped the title, being a common idiom, wouldn't give it away to too many.  Of course, now that you've brought it up, your reference to the title might actually be a spoiler in and of itself ... :)


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/