Does Perl have something similar to print_r? The question has been asked before by people at all levels of expertise with Perl and PHP. The answer has often been Data::Dumper, but that is both more powerful and more complex than needed. This code will produce basically the same results as the popular PHP print_r without any settings or objects to worry about...
use strict; use warnings; my @array1 = qw( four five six ); my @array = qw( one two three ); push( @array, \@array1 ); my $string = "four"; my %hash = ( 'first' => 'one', 'second' => 'two', 'third' => 'three', 'fourth' => \@array, ); #print_r( @array ); print_r( \%hash ); sub print_r { package print_r; our $level; our @level_index; if ( ! defined $level ) { $level = 0 }; if ( ! defined @level_index ) { $level_index[$level] = 0 }; for ( @_ ) { my $element = $_; my $index = $level_index[$level]; print "\t" x $level . "[$index] => "; if ( ref($element) eq 'ARRAY' ) { my $array = $_; $level_index[++$level] = 0; print "(Array)\n"; for ( @$array ) { main::print_r( $_ ); } --$level if ( $level > 0 ); } elsif ( ref($element) eq 'HASH' ) { my $hash = $_; print "(Hash)\n"; ++$level; for ( keys %$hash ) { $level_index[$level] = $_; main::print_r( $$hash{$_} ); } } else { print "$element\n"; } $level_index[$level]++; } } # End print_r

In reply to print_r by GhodMode

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.