Updated April 1, 2023
Introduction to Perl Data Dumper
The data structures in Perl can be converted into strings using a function called Dumper() function which takes a list of scalars or list of reference variables and outputs their contents in the syntax and by default, the reference variables are presented as output in the form $VARn where n represents the number of reference variables and by making use of Dumper() function, the data structure can be printed out as text and if it is loaded to another script, we can rely on Perl only to parse the text as structure and the indentation style can be controlled for the output obtained.
Syntax to Declare Dumper() function
use Data::Dumper;
Working of Dumper() Function in Perl
- The data structures can be converted into strings using a function called Dumper() function.
- It takes a list of scalars or list of reference variables and outputs their contents in the syntax.
- By default, the reference variables are presented as output in the form $VARn where n represents the number of reference variables.
- By making use of Dumper() function, the data structure can be printed out as text and if it is loaded to another script, we can rely on Perl only to parse the text as Perl structure and the indentation style can be controlled for the output obtained.
Examples
Given below are the examples mentioned:
Example #1
Perl program to demonstrate the usage of Dumper() function to convert the given data structure to syntax and display the output on the screen.
Code:
#making use of Dumper() function to convert the given reference variable to Perl syntax
use Data::Dumper;
$varname=10;
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given reference variable in Perl syntax is:\n";
print Dumper($varname);
Output:
In the above program, we are making use of Dumper() function to convert the given reference variable to Perl Syntax. Then the reference variable converted to Perl syntax is displayed as the output on the screen.
Example #2
Perl program to demonstrate the usage of Dumper() function to convert the given array data structure to syntax in Perl and display the output on the screen.
Code:
#making use of Dumper() function to convert the given array data structure to Perl syntax
use Data::Dumper;
@varname=(10, 20, 30, 40);
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given array data structure in Perl syntax is:\n";
print Dumper(\@varname);
Output:
In the above program, we are making use of Dumper() function to convert the given array data structure to Syntax. Then the array data structure converted to syntax is displayed as the output on the screen.
Example #3
Perl program to demonstrate the usage of Dumper() function to convert the given hash data structure to syntax in Perl and display the output on the screen.
Code:
#making use of Dumper() function to convert the given hash data structure to Perl syntax
use Data::Dumper;
%varname=(first => 10, second =>20, third =>30, fourth =>40);
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given hash data structure in Perl syntax is:\n";
print Dumper(\%varname);
Output:
In the above program, we are making use of Dumper() function to convert the given hash data structure to Perl Syntax. Then the hash data structure converted to syntax is displayed as the output on the screen.
Example #4
Perl program to demonstrate the usage of Dumper() function to convert the given data structure to syntax in Perl and display the output on the screen.
Code:
#making use of Dumper() function to convert the given reference variables to Perl syntax
use Data::Dumper;
$varname=100;
$varname1=200;
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given reference variables in Perl syntax is:\n";
print Dumper($varname);
print Dumper($varname1);
Output:
In the above program, we are making use of Dumper() function to convert the given reference variables to Perl Syntax. Then the reference variables converted to syntax are displayed as the output on the screen.
Example #5
Perl program to demonstrate the usage of Dumper() function to convert the given array data structure to syntax in Perl and display the output on the screen.
Code:
#making use of Dumper() function to convert the given array data structure to Perl syntax
use Data::Dumper;
@varname=(100, 200, 300, 400);
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given array data structure in Perl syntax is:\n";
print Dumper(\@varname);
Output:
In the above program, we are making use of functions to convert the given array data structure to Perl Syntax. Then the array data structure converted to Perl syntax is displayed as the output on the screen.
Example #6
Perl program to demonstrate the usage function to convert the given hash data structure to syntax in Perl and display the output on the screen.
Code:
#making use of Dumper() function to convert the given hash data structure to Perl syntax
use Data::Dumper;
%varname=(first => 100, second =>200, third =>300, fourth =>400);
#displaying the converted Perl syntax as the output on the screen
print "The converted form of the given hash data structure in Perl syntax is:\n";
print Dumper(\%varname);
Output:
In the above program, we are making use of functions to convert the given hash data structure to Perl Syntax. Then the hash data structure converted to Perl syntax is displayed as the output on the screen.
Recommended Articles
This is a guide to Perl Data Dumper. Here we discuss the introduction, working of Dumper() function in perl and examples respectively. You may also have a look at the following articles to learn more –