pg has asked for the wisdom of the Perl Monks concerning the following question:

I just made this small piece of code to entertain my fellow monks. Guess what are the results from the last two lines (Tomorrow is a holiday, remembrance day, in Canada, so happy holiday, is it also a holiday in some other countries?):
my @a = (); $a[0] = \@a; my $expr = '$a[0]'; my $index; for ($index = 0; $index < 1000; $index ++) { $expr = "\${$expr}[0]"; } print $expr, "\n"; eval "print $expr";

Replies are listed 'Best First'.
Re: Guess what is the print out?
by dws (Chancellor) on Nov 11, 2002 at 05:27 UTC
    Since $expr starts out holding the string '$a[0]' (without quotes), each time through the loop adds '${' to the front, and '}[0]' to the back (without the quotes). I'm guessing that something like   ${${${ .... ${$a[0]}[0]}[0] ... }[0] is printed.

    I haven't a clue what the eval'd print prints, but I suspect it involves a lot of autovivification.

    (After running the snippet: D'oh)

Re: Guess what is the print out?
by diotalevi (Canon) on Nov 11, 2002 at 05:22 UTC

    You'll be happier if you learn a perlier syntax for doing iterations ala for (0 .. 999) { $expr = "\${$expr}[0]"; }. It's at least prettier than your excessivly verbose usage of for()

    __SIG__ use B; printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;
Re: Guess what is the print out?
by submersible_toaster (Chaplain) on Nov 11, 2002 at 05:17 UTC
    Happy holiday ... struth - I thought australiens had it good down here in the 'Land of the Long-Weekend' but no such luck - It's an offical 'day' though , some call it Veteran's Day or Rememberance Day , I believe originally it was Armistice Day