5.1.4 Tools & code analysis: Analyzing Scripts in Perl Flashcards
Coding in Perl: what is Perl?
A language commonly used in Linux and Windows web servers to run dynamic code
Coding in Perl: what is the starting line of Perl code?
!/bin/perl
Coding in Perl: how to write comments in Perl?
This is the first line of my script
# This script is used to do backups of my systems
Coding in Perl: explain variable in Perl and they are marked in Perl?
In Perl, a variable is a named storage location used to hold data that can be changed during the program’s execution. Variables in Perl are marked by a sigil, which is a special character that indicates the variable’s type. For example, $ for scalar variables, @ for array variables, % for hash variables, and so on.
To declare a variable in Perl, you simply use the appropriate sigil followed by the variable name, like $scalar_variable, @array_variable, or %hash_variable. Perl is a loosely typed language, so you don’t need to explicitly declare the data type of a variable. Instead, Perl determines the variable’s type based on its usage
Coding in Perl: provide example of variable for name, array and hash, and how they are marked and how to show their output
my $name = “Alice”; # Scalar variable
my @numbers = (1, 2, 3); # Array variable
my %person = (‘name’ => ‘Bob’, ‘age’ => 30); # Hash variable
print $name; # Output the value of $name
print $numbers[0]; # Output the first element of @numbers
print $person{‘name’}; # Output the value associated with the ‘name’ key in %person
Coding in Perl: provide the basic numeric writing for arithmetic in Perl (=,<,> etc) (6)
● is equal to: if ($a == $b)
● is not equal to: if ($a != $b)
● is greater than: if ($a > $b)
● is greater than or equal to: if ($a >= $b)
● is less than: if ($a < $b)
● is less than or equal to: if ($a <= $b)
Coding in Perl: provide the basic string comparison in Perl (=,<,> etc) (6)
● is equal to: if ($a -eq $b)
● is not equal to: if ($a -ne $b)
● is greater than: if ($a -gt $b)
● is greater than or equal to: if ($a -ge $b)
● is less than: if ($a -lt $b)
● is less than or equal to: if ($a -le $b)
Coding in Perl: how are the Logical Operators (AND / OR) marked in Perl (2)
&& is AND
|| is OR
Coding in Perl: explain IF (which is a conditional statement) in Perl
In Perl, conditional statements are used to make decisions in a program based on certain conditions. The most common conditional statement in Perl is the “if” statement, which allows you to execute a block of code if a specified condition is true. Here’s an example of an “if” statement in Perl:
my $age = 25;
if ($age >= 18) {
print “You are an adult”;
}
In this example, the “if” statement checks if the variable $age is greater than or equal to 18. If the condition is true, the code block within the curly braces is executed, and the message “You are an adult” is printed.
Coding in Perl: explain ELSIF and ELSE (which is a conditional statement) in Perl
Perl also provides additional conditional statements such as “else” and “elsif” to handle alternative conditions. Here’s an example using “else” and “elsif”:
my $temperature = 25;
if ($temperature > 30) {
print “It’s hot outside”;
} elsif ($temperature > 20) {
print “It’s warm outside”;
} else {
print “It’s cold outside”;
}
In this example, if the temperature is greater than 30, the message “It’s hot outside” is printed. If the temperature is not greater than 30 but is greater than 20, the message “It’s warm outside” is printed. Otherwise, the message “It’s cold outside” is printed.
Coding in Perl: explain loops in Python (3)
1/ The “for” loop is typically used when you know in advance how many times you want to execute a block of code. It consists of three parts: initialization, condition, and iteration. Here’s an example of a “for” loop in Perl:
for (my $i = 0; $i < 5; $i++) {
print “Iteration: $i\n”;
}
In this example, the “for” loop will execute the block of code 5 times, starting with $i=0 and incrementing $i by 1 in each iteration, until $i is no longer less than 5.
2/ The “while” loop is used when you want to execute a block of code as long as a certain condition is true. Here’s an example of a “while” loop in Perl:
my $count = 0;
while ($count < 5) {
print “Count: $count\n”;
$count++;
}
In this example, the “while” loop will execute the block of code as long as the condition $count < 5 is true. It will continue to iterate and increment $count until the condition is no longer met.
3/ The “until” loop is the opposite of the “while” loop. It executes a block of code until a specified condition becomes true. Here’s an example of an “until” loop in Perl:
my $x = 0;
until ($x >= 5) {
print “Value of x: $x\n”;
$x++;
}
In this example, the “until” loop will execute the block of code until the condition $x >= 5 becomes true. It will continue to iterate and increment $x until the condition is met.