Perl do not have any data types. Really all you have is stings or numbers. There are no defined data types such as available in other languages for example int, float or char types available in Java. In Perl you just declare a variable and assign value to it. That value can be number or string.
Talking about numbers, variables in perl can store integers or floating point numbers a.k.a real numbers. Few examples are:
You can perform calculations on numbers using numeric operators.
Strings in Perl are nothing but sequence of characters. These characters can be ASCII or utf-8. To use utf-8 characters in your program you will have add an extra line at the top of your perl program.
The first line above in the program tells about Perl compiler (more on that later).
We can create strings in Perl as:
As you may have noticed in code above, I've create strings in single as well as in double quotes. The difference between the two is important and we talk about it when I'll discuss string interpolation.
You can concatenate two strings using dot operator as:
String repetition can be done as:
Perl automatically converts between numbers and strings as needed.
That's all for today.
Thank you for reading. Happy coding :)
Talking about numbers, variables in perl can store integers or floating point numbers a.k.a real numbers. Few examples are:
12 # integer
0b1111 # an integer in binary
0732 # an integer in octal
0xab9f # an integer in hexadecimal
456_789_231 # an integer with underscore for better readability
12.00 # a floating point number 12
6.45e15 # 6.45 times 10 to the 15th power
0b1111 # an integer in binary
0732 # an integer in octal
0xab9f # an integer in hexadecimal
456_789_231 # an integer with underscore for better readability
12.00 # a floating point number 12
6.45e15 # 6.45 times 10 to the 15th power
You can perform calculations on numbers using numeric operators.
12 + 3 # addition
22 - 3 # subtraction
9 * 8 # multiplication
5 / 2 # division, 5/2 is 2.5
10 % 3 # modulus operator, gives remainder.
22 - 3 # subtraction
9 * 8 # multiplication
5 / 2 # division, 5/2 is 2.5
10 % 3 # modulus operator, gives remainder.
Strings in Perl are nothing but sequence of characters. These characters can be ASCII or utf-8. To use utf-8 characters in your program you will have add an extra line at the top of your perl program.
#!/usr/bin/env perl
use utf8;
use utf8;
The first line above in the program tells about Perl compiler (more on that later).
We can create strings in Perl as:
'california' # string in single quotes
"london" # string in double quotes
"los angeles" # multi word string
'new york' # another multi word string
"london" # string in double quotes
"los angeles" # multi word string
'new york' # another multi word string
As you may have noticed in code above, I've create strings in single as well as in double quotes. The difference between the two is important and we talk about it when I'll discuss string interpolation.
You can concatenate two strings using dot operator as:
'One' . 'by' . 'one'
"Hello" . "World"
"Hello" . "World"
String repetition can be done as:
"Hello" x 3 # HelloHelloHello
"5" x 4.3 # 5555
"5" x 4.3 # 5555
Perl automatically converts between numbers and strings as needed.
"5" x 2 # 55
"5" * 2 # 10
"jhon" x "3abc" # jhonjhonjhon
"5" * 2 # 10
"jhon" x "3abc" # jhonjhonjhon
That's all for today.
Thank you for reading. Happy coding :)