perl function printf
The printf
function in Perl is used to format and print text and variables to the output. It is similar to the print
function, but allows for more control over the formatting of the output. The basic syntax of the printf
function is:
printf FORMAT, LISTSourec:www.theitroad.com
where FORMAT
is a string that specifies the formatting of the output, and LIST
is a list of values that will be printed according to the format.
Here's an example:
my $name = "Alice"; my $age = 30; my $salary = 50000; printf("Name: %s, Age: %d, Salary: \$%.2f\n", $name, $age, $salary);
Output:
Name: Alice, Age: 30, Salary: 000.00
In this example, the printf
function is used to print the name, age, and salary of a person. The FORMAT
string contains placeholders for the variables: %s
for a string, %d
for a decimal number, and %.2f
for a floating-point number with two decimal places. The values of the variables are passed in the LIST
argument, in the order that they appear in the format string.