4.2. Object Use

Let's demonstrate the object use cycle on a very useful Perl class called Data::Dumper. This class accepts a set of perl data structures, and renders them into a string which is an easy-to-read Perl representation of them.

Here's a program that uses it to display a perl data structure on the screen:

#!/usr/bin/perl

use strict;
use warnings;

# Import the Data::Dumper class
use Data::Dumper;

# Define a sample data structure
my $data =
{
    "a" => [ 5, 100, 3 ],
    "hello" =>
    {
        "yes" => "no",
        "r" => "l",
    },
};

# Construct a Data::Dumper instance that is associated with this data
my $dumper = Data::Dumper->new([ $data], [ "\$data" ]);

# Call its method that renders it into a string.
print $dumper->Dump();

Written by Shlomi Fish