How to dump variables in perl

→ Are you a new visitor? Please visit the page guidance for new visitors ←

How to dump variable in perl

Have you guys ever needed to dump variables in perl and see its exact contents? I had and was really tricky. The print function is alright for printing scalar variables, but when you try to print a hash and maybe with multiple levels you start to have headaches trying to read the content…

So, there’s a nifty perl module that you can use to dump the content of an entire array or hash, this is by using Data::Dumper. Its a core perl module, so you should already have it, however cpan would easily install this for you if really need it.

So to install Data::Dumper you can easily run:

wait for it to finish and you can then start using it in your code.

A nice little example on how to use this is bellow:

The output would look similar to:

On test cases it should give you better information to see what is going on and to help you fix the code.

You can also use it in loops or even use OO  by creating objects, for example:

Hope it helps. Cheers.

Request an article ←