Updated April 4, 2023
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:
$var = 1562.45; $strn = ("Welcome To My Domain"); printf("Hi user your input is \$%6.3f and converted output is %s\n\n", $var, $strn);
- 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.
printf("Welcome To My Domain:\n"); printf("'%7d'\n", 7); printf("'%7d'\n", 596903); printf("'%7d'\n", -94758); printf("'%7d'\n", -904685); printf("\n"); printf("Have a Nice Day:\n"); printf("'%-8d'\n", 785); printf("'%-8d'\n", 1023894785); printf("'%-8d'\n", -923487568); printf("'%-8d'\n", -1293475); printf("\n"); printf("\n"); 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);
- 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.
printf("'%4s'\n", "Welcome"); printf("'%76s'\n", "Welcome"); printf("'%-76s'\n", "Welcome"); printf("Welcome To My Domain\n"); printf("Have a Nice Day\n"); printf("kjadgvds hasjkgdf ashdjfh jsdhfhj ajsg\n"); printf("Welcome\tTo My Domain\n"); printf("jasg hsagdfj skjdfhk skdh shfj sj\n"); printf("Welcome\n To My Domain\n"); printf("kjshdfj kjahksj nkjh 2734 shj2983 3974y k\n"); printf("C:\Users\Kripya-PC\Documents\articles\\\n"); printf("hsdfjg jhajs kjaskdj kjaksd jask\n"); printf("/Downloads\n"); printf("weh jkhj kawj kwejk jwlkl lkwqejkwlkqjlk\n"); printf("'%.3f'\n\n", 873.974); printf("87v hk jskj oi3y khk38 i347 bi3748\n"); printf("'%.5f'\n", 873.975); printf("jdshj38 3784 3748687j he83sk 94\n"); printf("'%6.4f'\n", 748.28374); printf("u3928 oiwu498y bu4589 io349 b3974\n"); printf("'%7.14f'\n", 734.837495); printf("3749 yiwe b843 sekk48905 je48\n"); printf("'%06.4f'\n", 83.8346); printf("2638 hy3w8 j8939475 jbi34y8b wu4985b 3u984y bu498\n"); printf("'%-6.3f'\n\n", 857.98374); printf("26837 hjkwehr234yv hj gu4 s h jkh jkjkh 3987\n"); 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.
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 –