Variables. Be strict with them!
Variables in Perl are defined by the "$" symbol before their name.
Code: Select all
#!/usr/bin/perl
$a = 5;
$b = 6;
$c = $a + $b;
print($c);Perl variables have no type. They can be used as string, as a number, or even as a boolean value (we'll look into this later).
Code: Select all
#!/usr/bin/perl
$a = "5";
$a++;
print($a);Code: Select all
use strict;Code: Select all
#!/usr/bin/perl
use strict;
my $a = "5";
$a++;
print($a);Strings can be declared using single (') or double (") quotes. This is not only a matter of taste: it affects the variable resolving inside a string. Double-quoted string do resolve variables and sigle-quoted string do not. But what's that variable resolving thing? I think it's better shown in an example:
Code: Select all
#!/usr/bin/perl
use strict;
my $a = "5";
my $b = "$a";
my $c = '$a';
print($b);
print("\n");
print($c);Code: Select all
#!/usr/bin/perl
use strict;
my $a = 5;
my $b = "A dang string";
my $c = '$a\n$b';
my $d = "$a\n$b";
print("$a $b $c $d\n");Code: Select all
+ Addition
- Substraction
* Multiplication
/ Division
% Modulo
** PowerCode: Select all
. Concatenate
x RepeatCode: Select all
#!/usr/bin/perl
use strict;
my $a = "Hello ";
my $b = "world\n";
print($a . $b);
print($a x 3)length($string) -> Returns string length
reverse($string) -> Returns the string reversed
split(/regex/, $string) -> Splits a string on a regular expression (more about this later on) and returns an array
uc($string) -> Returns string all uppercase
lc($string) -> Returns string all lowercase
substr($string, [$initial_index,][$number_characters,][$substitution]) -> Returns a substring (replaced if 4th argument indicated) of the original string
printf($format, $string) -> Prints string on standard output with C-like printf() formatting
sprintf($format, $string) -> Same as printf() but instead of printing it it returns the formatted string
chop($string) -> Removes last string character (affects passed string, returns character removed)
<< Previous | Next >>
Advertising

