Close
Close

More print


We will start this chapter by taking a variable and giving it some value. Let's see how it is done.

$a = 10;
Output

In Perl, scalar variables (having single value) are written after dollar sign ($). So, 'a' written after dollar sign in the above example is a variable.
$a = 10; - We have taken a variable 'a' and assigned it a value of 10.

Variables in Perl can have any number of letters, digits and underscores (_) but the first letter of a variable must be a letter or underscore and not a digit.

Now, let's print the value of a variable.

$a = 10;
print "$a\n";
Output
10

You can see that the value of 'a' got printed in the previous example in place of $a and it is this easy to print the value of a scalar variable. To print the value of a scalar variable, we have to just write the variable after the dollar sign ($) and that's it.

Let's see one more example:
$a = 10;
$b = 20;
$c = $a+$b;
print "$c\n";
print "$a+$b\n";
Output
30
10+20

You can see that in place of "$a+$b", "10+20" got printed. This means that the values of 'a' and 'b' got printed and the expression ( $a+$b i.e., 10+20 ) was not evaluated. We can also evaluate an expression inside the print statement. Let's see an example explaining this.

$a = 10;
$b = 20;
print "Product of $a and $b is ",$a*$b," and their difference is ",$b-$a,".\n";
Output
Product of 10 and 20 is 200 and their difference is 10.

$a = 10; - It means that we are taking a variable 'x' and giving it a value 10.
$b = 20; - Similarly, we are giving a value of 20 to 'y'.
print "Product of $a and $b is ",$a*$b," and their difference is ",$b-$a,".\n";
Let's learn about this line of code.
Expressions written in (" ") are printed as they are and are not evaluated (only variables written after $ are replaced with their values.). So, 'Product of' is printed and then the value of 'a' is printed (i.e. 10), then 'and' is printed as it is. Then the value of b (i.e. 20) and then $a*$b is calculated and printed. (i.e. 200).

Take care of how commas (,) are used in the above code.

Let's see one more example:

$a = 10;
$b = 20.23;
$c = "Hey";
$d = "5";
print "$a\n$b\n$c\n$d\n";
Output
10
20
Hey

Here, 'a' is an integer, 'b' is a floating point (decimal number) and 'c' is a string.
String is a collection of characters. In simple English, it is a letter, word, sentence or collection of sentences. You will go through a whole topic on string. So, leave it for that time if you are getting confused and just keep in mind that strings are characters, words or sentences and these are written between ' ' or " ". Since 'Hey' and '5' are written between " " i.e. "Hey" and "5", so they are strings.

Using here documents for multi-line printing


A here-document can also be used to create a string with multiple lines and preserve whitespaces and newlines. Its syntax is:

$str = <<"END";
String
	with
multiple lines
END

Everything written before END will be part of the string "str" (with whitespaces and newlines preserved). Let's see an example on this:

$str = <<"END_MAIL";
Hey there,

I am just creating this mail to learn here-docs.

Thanks for your time.

XYZ
END_MAIL
print "$str";
Output
Hey there,

I am just creating this mail to learn here-docs.

Thanks for your time.

XYZ

You can see that everything written before END_MAIL becomes a part of string 'str' with preserved newlines.

Concatenating two strings


We can concatenate or join two strings by using . operator. Let's see its example.
$a = "Codes";
$b = "Dope";
print ($a.$b,"\n");
Output
CodesDope

You can see that the (.) operator joined two strings ("Codes" and "Dope") to form a new string, "CodesDope".

concating strings in Perl

In Perl, strings and numbers are seamlessly converted into each other depending on the context in which they are used. An example will make it clear.

print ("5"+"2","\n");
print (5 . 2,"\n");
Output
7
52

When we used (+) on two strings ( "5" and "2" ), they got converted to integer and we got 7 as the result and when (.) on integers ( 5 and 2 ), they got converted to strings and we got 52 ( after joining both ) as the result. And this is what I was trying to say that in Perl, strings and numbers are converted into each other according to the requirement.

int() function


int() function takes a number and rounds it down to the nearest integer. It means that int(10.97) will give 10. Let's see an example for the same.

$a = 10.97;
print int(10.97),"\n";
Output
10

Escape Sequences


We know that \n designates a newline character and it is one of the escape sequences. There are many others in Perl. Here are the most important ones:

  • \\ - backslash (to print \ on screen)
  • \$ - dollar sign (to print $ on screen)
  • \r - return sign
  • \@ - at-sign (to print @ on screen)
  • \n - a newline character
  • \" - double-quote character (")
  • \t - a tab character (prints a tab space)

Let's see an example to see them working.

print "Hey, \"How are you\".
I am just asking but I can give you \$100 for your answer
\tYes, you can contact me \@ xyz.com for money.\n"
Output
Hey, "How are you".
I am just asking but I can give you $100 for your answer         Yes, you can contact me @ xyz.com for money.

You know how to print something on the screen. In the next chapter, you will learn to take input from the keyboard.

Swapping


Swapping means interchanging the values of two variables. eg - if x is 10 and y is 5, then after swapping x will be 5 and y will be 10.
So, let's see how it is done.

$x = 10;
$y = 5;
($x, $y) = ($y, $x);
print "x is $x\n";
print "y is $y\n";
Output
x is 5
y is 10

And it's done.
Don't forget to put the variables which you want to swap in brackets.

sprintf and printf


sprintf is used to print in a formatted way. Suppose you have a variable having a value of 3.14159, then by using sprintf function you can control the precision of digits after decimal while printing. It means that you can print only 3.14.

There are specific characters which start with % (percentage sign) which are converted into specific type. For example, %s converts into a string, %i into an integer, etc. Let's first see an example:

print sprintf ("My name is %s and I am %i years old","Sam",19);
Output
x is 5
y is 10
printf can be used instead of print sprintf. It formats using sprintf and then prints using the print function.

You can see that string got printed in place of '%s' and an integer got printed in place of '%i'. Let's see the table of supported conversions in Perl with examples.

Conversion sign Function Example
%s String "%s","Sam" -> Sam
%d or %i Integer "%i",10 -> 10
"%i",10.1 -> 10
%f Float "%f",10.1 -> 10.100000
%e Floating point number in scientific notation "%e",10.1 -> 1.010000e+01
%c Character from ASCII number "%c",97 -> a
%b An integer in binary "%b",10 -> 1010
%% Actual percent sign "%%" -> %
%o An integer in octal "%o",10 -> 12
%x and %X An integer in hexadecimal (%X for uppercase) "%x",10 -> a

We can specify the space used by a string on the screen by writing a number between %s. For example, "%5s","Hey" prints "  Hey" and "%-5s","Hey" prints "HEY  ".

We can also use a number between %f for precise printing of decimal number. For example, ""%.2f",3.14159" prints "3.14"

Let's see an example:

printf ("%5s\n","Hey");
printf ("%-5s\n","Hey");
printf ("%.2f\n",3.14159);
Output
  Hey
Hey  
3.14

Ask Yours
Post Yours
Doubt? Ask question