Updated April 4, 2023
Introduction to Perl sprintf
The Perl sprintf() is one of the methods that can be used for the user to print the input formats. It can be of any data type values, but the function’s return type should always be the string type because it is interpreted as the sprint function. It will always be emulated like the c language. sprint function the data type conventions of the c language library functions. Normally the printf function will print and display the current user datas on the screen. The filehandles and manipulation also handled these function; the sprint function will always return the string values with the scalar type of variables.
Syntax
The Perl script has the default variables, keywords, and functions to handle the applications. The sprint function uses some formats like string-based user input values in the data list. Using some conversion specifiers, the string format is handled by the corresponding user inputs.
#!/usr/bin/perl
$variable name = sprintf( '%conversion specifiers', input values);
—some Perl script codes based on the application requirements—
The above codes are the basic syntax for creating applications with the help of the sprint function. The variables and scalar datas will be considered as the singular type of inputs. It may be the data types like strings or integer numbers.
How sprintf function works in Perl?
The Perl script has its own sprintf formats that can be used while the function it been executed in the application, and it can be emulated and similar to the other programming languages like c etc. The printf method is used to creating the user inputs as formatted one on the screen. Still, the sprintf method will be executed and accepts only the same type of arguments. Still, it will be returned using the formatted strings instead of printing options it can get and retrieved the user inputs with string formats. It will print the data to the files, or any other storage devices will be sent to via network protocols and concepts like emails, chats, etc.
The sprintf function will use some format specifiers, and these formats will mainly handle all the user input data types; the string formats will take and count the number of tokens that describe and handle user input values. Generally, the printf() function is the number of tokens that describe and print the variables whatever we needed in the project. Each variable format specifiers will start with the specifier like a % and also moreover the zero or n number of the optional modifiers ending with the conversion specifiers like if we use %d as the format specifier of the method while executing the method, the variable value will automatically convert to the user input values like integer numbers. We can insert these values to the format string values, and the library functions like sprint will use some default exponents, including the scientific notations like %e, %E, etc., for each input value; the numbers also calculated using the modules with exponents.
The method having the default parameters using some indexes will be both implicit and explicit conversions. In default, sprintf() function will use some type of arguments in the numbers list; this will be allowed with the type of arguments which has both valid and invalid number formats, so the invalid identifiers are in the out of the order formats which we have used the same input as the looping conditions.
The flags are also set as the valid input identifiers; the % symbol will be used for the convert the input conversions while the input values like numbers are the more set of precisions when it’s incremented in the looping conditions. For Each set of vector flags, the integer number values are interpreted with the supplied set of string values; those values are allotted for each character in the input strings Because the method will be calculated only the scalar type of input values which are to be passed in the method as the argument, so the Perl script which has applied the user input formats for each set of number integers those integer values are manipulated and concatenated using the string values which has to get the execution results after the method. The vector flags are also generally displayed using some sorting order values of the specified characters in the main, arbitrary set of strings.
Examples of Perl sprintf
Here are the following examples mention below
Example #1
Code:
$valu1 = sprintf("%03d %1$d", 'siva');
$valu2 = sprintf("%03d%1$d", 'raman');
$valu3 = sprintf("%04d%1$d", 'sivaraman');
$valu4 = sprintf("%05d", 'arun');
$valu5 = sprintf("%06d", 'kumar');
$valu6 = sprintf("%07d%d%1$d", 'arunkumar');
$valu7 = sprintf("%08d%1$d", 'hareesh');
$valu8 = sprintf("%09d", '898589');
$valu9 = sprintf("%10d", '2-80');
$valu10 = sprintf("%11d", '4356');
$valu11 = sprintf("%12d", '`142');
$valu12 = sprintf("%13d", '98765');
$valu13 = sprintf("%14d", 'fg5');
$valu14 = sprintf("%15d%d%1$d", '45grea');
$valu15 = sprintf("%16d", 'AF34r');
$valu16 = sprintf("%17d", 'ASCfdfd45');
print "$valu1\n$valu2\n$valu3\n$valu4\n$valu5\n$valu6\n$valu7\n$valu8\n$valu9\n$valu10\n$valu11\n$valu12\n$valu13\n$valu14\n$valu15\n$valu16\n$valu17";
Output:
Example #2
Code:
use strict;
use warnings;
my $valu1 = sprintf("%b", 198988721);
my $valu2 = sprintf("%b", 79587);
my $valu3 = sprintf("%b", 90247548);
my $valu4 = sprintf("%x", 79587);
my $valu5 = sprintf("%4f", 198988721);
my $valu6 = sprintf("%.2f %.3f", 1989.88721, -678.786);
my $valu7 = sprintf("%x", 90247548);
my $valu8 = sprintf("%x", 198988721);
my $valu9 = sprintf("%x", 732);
my $valu10 = sprintf("%x", 82349587);
my $valu11 = sprintf("%x", 192374654790439);
my $valu12 = sprintf("%x", -912837459072980);
my $valu13 = sprintf("%x", 12398457);
my $valu14 = sprintf("%x", 923948577);
my $valu15 = sprintf("%b", 192374654790439);
my $valu16 = sprintf("%x", 109238475);
my $valu17 = sprintf("%x", 2349857);
my $valu18 = sprintf("%x", 12938475);
my $valu19 = sprintf("%x", 2938475);
my $valu20 = sprintf("%x", 102394875);
my $valu21 = sprintf("%x", 8192384578);
my $valu22 = sprintf("%x", 712394857);
my $valu23 = sprintf("%x", 19238475);
print "$valu1\n$valu2\n$valu3\n$valu4\n$valu5\n$valu6\n$valu7\n$valu8\n$valu9\n$valu10\n$valu11\n$valu12\n$valu13\n$valu14\n$valu15\n$valu16\n$valu17\n$valu18\n$valu19\n$valu20\n$valu21\n$valu22\n$valu23\n";
Output:
Example #3
Code:
#!/usr/local/bin/perl
use strict;
use warnings;
my $varlen = 19;
my $inp = "Welcome To My Domain";
my $re= sprintf("'%${varlen}s'", $inp);
print "$re\n";
$re = sprintf("'%*s'", $varlen, $inp);
print "$re\n";
Output:
Conclusion
In the Perl script, we have used the default methods in different scenarios; among these, the sprintf is the value conversion using format specifiers. The values precision will be of any data types like integer numbers, decimal, and floating-point numbers, but it always returns the string characters as a result.
Recommended Articles
This is a guide to Perl sprintf. Here we discuss How the sprintf function works in Perl and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –