Generally, a Perl script consists of a sequence of comments, declarations andstatements.
You can insert comments in your script wherever you want by using the # symbol, and anything you type (after it) until the end of the line will be ignored by Perl. However, there is an exception and we are speaking about the first line of a Perl script:
#!/usr/local/bin/perl
This line is not necessary in Windows because the Perl interpreter is determined by the file association of the system. However, even if you use Perl for windows, the interpreter does take note of this first line in order to determine any additional command line switches that you might need to apply. The #! character sequence known as "hash-bang" lets the linux/unix shell know what interpreter to use and where this interpreter is located.As you know, there is no need to declare variables in a Perl script. There are global declarations like subroutine and format declarations that we typically include at the beginning or the end of the script. You may either declare scoped variable using my or our preceding the variable name. However, a Perl declaration can be placed anywhere a statement could be placed, i.e anywhere in the script file.
A is an expression for the computer to process or evaluate. Generally, a statement ends in a semicolon which means that the statement is complete. After evaluated, each sentence has a value which is used by Perl to evaluate or process other statements. The statements can be grouped in blocks by surrounding them in curly braces. The Perl interpreter views a statement block as an individual statement. There are:
- like if and unless, which use a relation expression to check if a condition is true
- like while, for, foreach, do-while, until which check a condition and execute the statements included in curly braces until that condition is evaluated true
The Perl language has three : scalars, and . For storing the data, like any other programming language, Perl uses which are of course of the type enumerated above. To process or manipulate the data objects, Perl has a lot of which could be numerical, string or special.To process some actions, Perl made available a various number of grouped by categories, such as functions for scalars and strings, input/output functions and so on. You can build your own functions, calledsubroutines.
If you want to reuse some piece of code already written by others, you can use the
#!/usr/bin/perl
print "Hello World";
This Perl script, after executing, will print the message "Hello World".Now, we’ll show you a more complicated Perl script, for educational purposes only, where we’ll try to use some of the topics mentioned above.
#!/usr/bin/perl # "hash-bang" line
# A Perl script
# this is a comment line
# some declarations with the use clause
# =====================================
use warnings; # it warns about undefined values
use strict; # it's a lexically scoped declaration
# a simple statement
# ==================
# the next code initializes $pi scalar variable
# any scalar variable begins with $ symbol
my $pi = 3.14; # the statement ends in a semicolon
# a conditional statement
# =======================
# also note the numeric operator == and
# the use of the print function
print "pi = $pi\n" if $pi == 3.14;
# it will output pi = 3.14
# a while loop statement
# ======================
my @numbers = ();
# this line declares and empties the
# @numbers array variable;
# any array variable begins with the @ symbol
while(my $line = )
{
chomp $line;
push(@numbers, $line);
}
# we read some lines from using the
# $line scalar variable
# chomp function will remove the ending newline
# push function will append the read number
# to @numbers array
# the loop will end when we type Ctrl/Z
# calling a subroutine
# ====================
# in the following lines we will declare and
# call a subroutine that will add two numbers
# and it will return their sum
sub add2Numbers
{
my $firstNumber = $_[0]; # first argument
my $secondNumber = $_[1]; # second argument
return $firstNumber + $secondNumber;
}
# calling the subroutine declared above:
print add2Numbers(3, 5), "\n";
# it will output 8
# using a module
# ==============
# the following snippet code will show you
# how to use a module in your script;
# we’ll exemplify with the DateTime-0.47 module;
# in order to run this script you must have
# already installed this module on your system
use DateTime;
# the module must be declared first through use clause
my $dt = DateTime->now;
# $dt variable is initializes with the current date
print "Current Date:\n";
print "Month:", $dt->month, "\n";
print "Day:", $dt->day, "\n";
print "Year:", $dt->year, "\n";
# it will print the current month, day and year
阅读(1248) | 评论(0) | 转发(0) |