whats the purpose of use Data::Dumper;

whats the purpose of use Data::Dumper;

am 01.06.2011 18:07:45 von eventual

--0-1335162251-1306944465=:4197
Content-Type: text/plain; charset=us-ascii

use Data::Dumper;

Hi,
Can someone give me a few examples on the purpose of use Data::Dumper;
I tried reading but I could not understand.
Thanks
--0-1335162251-1306944465=:4197--

Re: whats the purpose of use Data::Dumper;

am 01.06.2011 18:16:48 von Rob Coops

--0015175cb9f625c9a204a4a8da1c
Content-Type: text/plain; charset=UTF-8

On Wed, Jun 1, 2011 at 6:07 PM, eventual wrote:

> use Data::Dumper;
>
> Hi,
> Can someone give me a few examples on the purpose of use Data::Dumper;
> I tried reading but I could not understand.
> Thanks


use Data::Dumper;

simply tells perl to load the Data::Dumper module. You can then use the
module to well dump data. :-)

Try the following, create a complex data structure something like this:

use Data::Dumper;
my $complex = [ { 'value1' => 'key1', 'value2' => [ 1, 2, 3, 4, 5 ] },
'Array value 2', '03', [ 'String 1', 'String 2', 'String 3' ], ];
print Dumper $complex;

and you will see something like this:
$VAR1 = [
{
'value1' => 'key1',
'value2' => [
1,
2,
3,
4,
5
]
},
'Array value 2',
'03',
[
'String 1',
'String 2',
'String 3'
]
];

Now it might seem silly at first but if you are dealing with large and
complex data structures that have a mix of arrays, hashs, strings,
references to data structures etc.. then you will soon come to love the
Data::Dumper for its very helpful output.

If you are not sure about loading external modules in perl please have a
look at: http://perl-begin.org/tutorials/perl-for-newbies/part3/ which will
give you a good beginner guide to what modules are and what they are used
for etc.

Regards,

Rob

--0015175cb9f625c9a204a4a8da1c--

Re: whats the purpose of use Data::Dumper;

am 01.06.2011 20:12:14 von Michael Greb

On Wed, Jun 01, 2011 at 09:07:45AM -0700, eventual wrote:
> use Data::Dumper;
>=20
> Hi,
> Can someone give me a few examples on the purpose of use Data::Dumper;
> I tried reading but I could not understand.
> Thanks

Data::Dumper is a useful modules for converting perl data structures into=
a
string for printing. It can be used for debugging, understanding how
somebody else's code works, tranffering information, etc. The full
docs for the module can be found at
.

Here's a simple example:
#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

use Data::Dumper;

my @foo =3D ( 'one', 'two', 'three' );
my %bar =3D ( 'redfish' =3D> 'bluefish', 'onefish' =3D> 'twofish' );

my $more_complex =3D [
{ foo =3D> 'bar', 'baz' =3D> 'biz' },
'two',
{ a =3D> 'b', c =3D> [ 'one', 'two', 'three' ] }
];

say Data::Dumper->Dump( [ \@foo, \%bar, $more_complex ],
[ 'foo', 'bar', 'more_complex' ] );

Produces this output:

$foo =3D [
        =A0'one',
        =A0'two',
        =A0'three'
      =A0];
$bar =3D {
        =A0'onefish' =3D> 'twofish',
        =A0'redfish' =3D> 'bluefish'
      =A0};
$more_complex =3D [
                  {
                     'baz' =3D> 'b=
iz',
                     'foo' =3D> 'b=
ar'
                  },
                  'two',
                  {
                     'c' =3D> [
                         =A0=
    'one',
                         =A0=
    'two',
                         =A0=
    'three'
                         =A0=
  ],
                     'a' =3D> 'b'
                  }
                ];

--- END --

Hopefully this is useful.

Mike

--=20
Michael
michael@thegrebs.com
M: +1-562-MIKEGRB

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/