Updated July 3, 2023
Introduction to Perl unpack
In Perl, the unpack function is utilized to manipulate binary data by creating or expanding binary values. Binary data, consisting of 0s and 1s, can be processed using encryption and decryption techniques. This function is particularly useful for handling sensitive information such as passwords and other related data.
Syntax:
The unpack is one of the functions that can be converted or transform the binary data type values into the user-defined data type templates with the help of representations that are related to the perl script functions, keywords, and other built-in syntax. The below codes are the basic syntax for creating and using the unpack function in the perl scripts.
#!/usr/bin/perl -w
$vars= pack("");
@vars1=unpack("$vars");
—some Perl script logic codes depend upon the requirement—
How does unpack function work in Perl?
Generally, the perl script does not have to access the memory randomly. It has some structure and represents the same with some translators like a pack and unpack() functions. It has some default representations. It can be called a template like that if we use unpack() function; it seems like the opposite process of the pack() function. The binary data with some specific structures has its own reference of each value stored in the memory. So the pack function contains the specific format for listing out the values and packed or converted into the character strings but unpacks takes only the format with the character strings with some breaks to the strings with the specified formats and assigned with the variables in the script.
Examples
Let us discuss examples of Perl unpack.
Example #1
Code:
#!/usr/bin/perl -w
$vars = pack("siva", 7849367);
print "Welcome To My Domain $vars\n";
$vars = pack( "" );
print "Have a Nice Day $vars\n";
$vars = pack( "siva", 6354, "I", 6858 );
print "Welcome User please find your packed values $vars\n";
@vars1 = unpack( "siva", "$vars" );
print "Thank You User your first Input user values $vars1[0]\n";
print "Thank You User your second Input user values $vars1[1]\n";
print "Thank You User your third Input user values $vars1[2]\n";
print "Thank You User your first Input user values $vars1[3]\n";
Output:
In the above example, we used the pack() and unpack() function at the time, and also we can use the variable like $vars. We assigned the value to the specific variable and used the pack and unpack() function based on their needs.
Example #2
Code:
#! perl -w
use strict;
sub exa {
shift;
}
sub demo1 {
unpack "i*", shift;
}
sub demo2 {
unpack "V*", shift;
}
sub demo3 {
unpack "I*", shift;
}
sub demo4 {
unpack "a*", shift;
}
sub demo5 {
unpack "b*", shift;
}
sub demo6 {
unpack "c*", shift;
}
sub demo7 {
unpack "d*", shift;
}
sub demo8 {
unpack "h*", shift;
}
sub demo9 {
unpack "l*", shift;
}
sub demo10 {
unpack "n*", shift;
}
sub finsa{
my $vars= shift;
my $vars1 = "Welcome To My DOmain wdejfh bwefvhd gdv gv jgfjw egf wkejg jerfg jewrgf ekjwrhw kjejhrw kjergkw j ehgw ejhrgke jrgf kjergk ejhrgfkw ejhrge jhrgfkweh jrk ehjkw jerhke jh ekhrj hg";
my $vars2 = "983465 ifsdg8 shdfgh he sdjsb khbwdsh 9876asnbd 87 hjg 90 jh999 jhjhj g89 7987987 hjh j j979 8jkhj 9jh egdf gh8888 wgefh w8 hh98878 878 87 7 79 7 778 78 89 9 98 98 98 98 98 9 9 ";
my $vars3 = $vars->($vars1);
if ($vars3 =~ /^$vars2/i) {
print "Welcome Users\n";
}
else {
print "Have a Nice Days |$vars2|$vars3|\n";
}
}
finsa \&exa;
finsa \&demo1;
finsa \&demo2;
finsa \&demo3;
finsa \&demo4;
finsa \&demo5;
finsa \&demo6;
finsa \&demo7;
finsa \&demo8;
finsa \&demo9;
finsa \&demo10;
Output:
In the second example, we used the pack and unpack() function with separate sub-classes, and each class has called with the parent sub-classes named exa.
Example #3
Code:
#!/usr/bin/perl -w
$vars = pack("B*", 192, 168,42, 76);
print "Welcome To My Domain $vars\n";
@vars1 = unpack("B*", "welcome\n");
print "Array $vars1[0]\n";
Output:
In the final example, we used the basic pack() and unpack() functions in the ip numbers. Basically, with the help of ‘.” Operator or symbol, we can split the numbers and set it as the system’s ip address.
Conclusion
Finally, we used Perl as default control basic structures, functions, user input, and output operations. These are the concepts that are held through the script for creating the task depending upon the user’s requirement.
Recommended Articles
This is a guide to Perl unpack. Here we discuss the introduction and How does unpack function work in Perl? with Examples for better understanding. You may also have a look at the following articles to learn more –