This will recurse endlessly until the heat death of the universe or you run out of swap, whichever comes first. Then again, perhaps the PHP implementation does as well.
You're right: +1 :) I don't know how PHP's print_r handles this either. I guess a solution to this problem would be to add an arbitrary limit to the
$level variable and have it stop calling
print_r when it gets to the limit. I have to ask, though, why would you have an array that references itself?
Minor issue: you don't want to explicitly state the package (main::print_r) when you call the function within itself. Using the unqualified name will allow it to work regardless of which package defines the routine.
I did this initially, but I got the following error:
Undefined subroutine &print_r::print_r called at ./print_r.pl line 55.
To make it easy to use in other programs, I gave it's own package so that there would be no chance of another global variable with the same name in the main namespace, but I still wanted
print_r to be in in the main namespace. I guess it was just a warning, but this was the only way I could get it to work without errors.
--
-- Ghodmode
Blessed is he who has found his work; let him ask no other blessedness.
-- Thomas Carlyle