Good Place. Good Things. Good Time.

Perl printf

Perl printf

Introduction to Perl printf

The Perl printf is defined as the function that can be used for to print the list of values. It can be interpreted by using some formats and specified to the outputs. While we have handled files to the one that can be printed, the user-input datas to the screen with formatting can be any type of string. It same as the sprint function, but the output results of the printf function do not mean the actual output results. The printf formatter may be any specifiers, especially the printf function not append the new lines to the output screen through automatically on the screen.


The Perl scripts have default keywords, functions and scalar variables that means the variable accepts only the single value. It can be any numbers, letters, characters, digits with operators, but the first character of the variable always must be the letter or symbol; like the underscore operator, it’s not a digit.

$ variable name = value;
printf(“ “, $variable name);
---some Perl script logics---

The above code is the basic syntax for using the printf() function in the Perl scripts. While we used the printf function on the script, it may have some advanced properties utilized on the field specifiers on the function.

How does the printf Function Work in Perl?

  • The Perl script printf() method prints only the user datas with the specified format specifiers on the screen. Actually, the function prints only the values, which is interpreted using the List datas through via using some format specifiers with the current result as the output. The filehandle concept is used for more space in the printf function, which handles the errors and exceptions by using exception classes and modules but the printf() function that prints the outputs by using some format specifiers. Generally, the printf function used some properties which are related to their fields and attributes.
  • Like that this function as many of the properties are to be same as the print function in the script. That’s the way it shows the better output format, which has already related to their user inputs. This is more similar to the field specifiers with co-related to their text string format specifiers with some delimiters like comma, semi-colon, underscore etc. Using some Regular Expressions concepts, the user inputs are to be validated and evaluated at the back end codes, showing the output on the user screen.

Examples of Perl printf

Given below are the examples of Perl printf:

Example #1


$var = 1562.45;
$strn = ("Welcome To My Domain");
printf("Hi user your input is $%6.3f and converted output is %snn", $var, $strn);


Perl printf 1


  • In the above example, we used two variables like $var and $strn, and both the variables are used with some formats like $var variable assigned values are only the float type of numbers so the decimal point of results to be expected once the given variable is executed on the script like that it will be printed on the user screen.
  • Same time the next variable $strn accepts only the string values like “Welcome to My domain”, so it’s a string format it specifies the user information’s the printf function which has passed the calls to the string and also the field is specified to the variables and then it will insert into the memory locations for separate positions.

Example #2


printf("Welcome To My Domain:n");
printf("'%7d'n", 7);
printf("'%7d'n", 596903);
printf("'%7d'n", -94758);
printf("'%7d'n", -904685);
printf("Have a Nice Day:n");
printf("'%-8d'n", 785);
printf("'%-8d'n", 1023894785);
printf("'%-8d'n", -923487568);
printf("'%-8d'n", -1293475);
printf("Thanks for your interest in our application:n");
printf("'%09d'n", 3);
printf("'%09d'n", 223);
printf("'%09d'n", 9234687);
printf("'%09d'n", -9238747);
printf("'%09d'n", -2390478);
printf("'%04d'n", 23);
printf("'%04d'n", 'Welcome');
printf("'%04d'n", 'Welcome34');
printf("'%04d'n", -9243747);
printf("'%04d'n", -4590478);
printf("'%02d'n", 'Tyujk');
printf("'%02d'n", 'udg 4857 sdhfg');
printf("'%02d'n", 9238747);
printf("'%03d'n", -93476538747);
printf("'%02d'n", -239754478);


Perl printf 2


  • In the second example, we have used the same printf function in different ways. We can pass the inputs in numbers that will be the integer, float, decimal, short, long and double; it can be any of the types passing into the function, and it is converted into the required output format.
  • Some formats will follow the rules and control the fields with some heights, widths, and even other fields like printing left-justified, right-justified and zero-filled numbers stored in the variables for calculating their functions and storages the memory locations. It accepts both positive and negative numbers followed by its signatures and its relevance to their usages.

Example #3


printf("'%4s'n", "Welcome");
printf("'%76s'n", "Welcome");
printf("'%-76s'n", "Welcome");
printf("Welcome To My Domainn");
printf("Have a Nice Dayn");
printf("kjadgvds hasjkgdf ashdjfh jsdhfhj ajsgn");
printf("WelcometTo My Domainn");
printf("jasg hsagdfj skjdfhk skdh shfj sjn");
printf("Welcomen To My Domainn");
printf("kjshdfj kjahksj nkjh 2734 shj2983 3974y kn");
printf("hsdfjg jhajs kjaskdj kjaksd jaskn");
printf("weh jkhj kawj kwejk jwlkl lkwqejkwlkqjlkn");
printf("'%.3f'nn", 873.974);
printf("87v hk jskj oi3y khk38 i347 bi3748n");
printf("'%.5f'n", 873.975);
printf("jdshj38 3784 3748687j he83sk 94n");
printf("'%6.4f'n", 748.28374);
printf("u3928 oiwu498y bu4589 io349 b3974n");
printf("'%7.14f'n", 734.837495);
printf("3749 yiwe b843 sekk48905 je48n");
printf("'%06.4f'n", 83.8346);
printf("2638 hy3w8 j8939475 jbi34y8b wu4985b 3u984y bu498n");
printf("'%-6.3f'nn", 857.98374);
printf("26837 hjkwehr234yv hj gu4 s h  jkh jkjkh 3987n");
printf("'%-5.25f'n", 8347958.892365);




  • In the printf() function, we used both numbers and string characters in various format specifiers, and even some comparison operators are used and called with the same reference names. Because the printf function used some operators allowed with string formats and it has generated the reports with formats and even though the formatted holds with the some numbers that can be called through with the help of some conversions.
  • And these conversions will used some operators like the % sign with the end of the characters even though the same number of characters are assigned through its conversions. Using some exponential notations is required for the printf function.


In conclusion, they must have some built-in methods for printing the outputs on the screen for each programming languages. Likewise, each method has its own attributes and properties for showing its results as much as needed from the user perspective. In Perl, the printf function used some format specifiers for displaying the outputs on the user screen.

Recommended Articles

This is a guide to Perl printf. Here we discuss the introduction; how does the printf function work in Perl? and examples, respectively. You may also have a look at the following articles to learn more –

  1. Perl join
  2. Perl for Loop
  3. Perl chomp()
  4. File Handling in Perl

The post Perl printf appeared first on EDUCBA.

Read original article here: Perl printf

Read original article here: Perl printf

Disclaimers and Denial of responsibility..!

Denial of responsibility! is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.