1. %ENV
Example:
To set a path "PATH" environment variable, we can just perform:
$path = $ENV{'PATH'};
$ENV{PATH} = '/bin/usr/bin';
$ENV{SHELL} = '/bin/sh'
Environment variables are stored in an associative array %ENV .
2. Post and Get
There are two ways to send data from an HTML to a server form via CGI:
GET and POST. In the GET method, the input values from the form are
sent as part of the URL, and saved in the QUERY_STRING environment variable.
With POST, data is sent as an input stream to the program.
Example:
--------
Save these scripts in a file.cgi and request it
on a browser.
#!C:/Perl/bin/perl.exe -w
use CGI;
$cgi = new CGI;
print qq{Content-type: text/html
<html><head></head><body>};
print qq{<form method="POST" ><input type="submit" value="Request by POST">
<input name="postfield"></form>};
print qq{<form method="GET" ><input type="submit" value="Request by GET">
<input name="getfield" ></form>};
print qq{</body></html>};
3. flock
If we need that only one user at a time should access a data file,
we can lock a file with the Perl flock function.
Example:
--------
open (FILE, "data.txt");
flock (FILE, 2);
The file is now locked. Closing the file will unlock it.
4. chop and chomp
The chop function is used to remove that last character
of a string variable.
The chomp function will remove the last character of a
string, but only if that character is an input record separator ,
for example \n
Example:
--------
$sentence1 = "regards and good luck !";
chop ($sentence1);
print "$sentence1\n";
print "-----------------\n";
$sentence2 = "regards andgood luck !\n";
chomp ($sentence2);
print "$sentence2";
outputs:
regards and good luck
-----------------
regards andgood luck !
5. My
My $sentence;
declares the variables is local in an enclosing block
(such as a subroutine). If several values are listed,
the list is be placed in parentheses.
Without "use strict;" and without declaring a variable
with a "my", Perl initializes it to undef, which is "" for strings
or 0 for numbers.
6. Begin, die and end
This example shows the related priorities:
--------
die "something";
END { print "11\n" }
BEGIN { print "1\n"; }
END { print "22\n" }
BEGIN { print " 2\n"; }
outputs:
Begin 1
Begin 2
something
End 22
End 11
BEGIN
Run some code as soon as it has all been read
in by the parser and before the rest compiles.
END
7. eval
The eval function can be used to evaluate or to execute>
Example:
------
is something of a hybrid between an expression evaluator and
a statement executor. It returns the result of the last expression evaluated (all
statements are expressions in Perl), and allows the final semicolon to be left off.
Example as an expression evaluator:
#!C:/Perl/bin/perl.exe -w
$result = 15;
print eval('$result + 10'), "\n";
$result = 15;
eval('$result= $result - 10; print "$result\n";');
ouputs
25
5
8. qw() function
The qw() function makes things easier to handle lists.
Example:
--------
my @books = ('Physics', 'Mathematics', 'Chemistry');
Simply, this array can be written equivalently as:
@books = qw(Physics Mathematics Chemistry);
@books = qw/Physics Mathematics Chemistry/;
@books = qw'Physics Mathematics Chemistry';
@books = qw{Physics Mathematics Chemistry};
The qw() function takes a list of elements and
quotes them.
qw(Physics Mathematics Chemistry) will do:
'Physics','Mathematics','Chemistry'
9. Concatenation and repetition
With string concatenation, we use the period character "."
To repeat "n" times the "y" character, we use:
my $example = 'y' x n;
Concatenation example:
my $sentence = 'best' . "regards";
Repetition example:
my $hyphen_line = '-' x 40;
10. Length of a string
To find out how many characters are in a string, we
use the function length().
Example:
my $sentence = "best regards\n":
my $leng = length($sentence);
That will outputs 12.
11.Split and join
Split:
------
split(/PATTERN/, STRING_EXPRESSION,LIMIT);
We use the split function to break a string into
components separated by a pattern (like \t :tab).
Example,
my $sentence = "Best\tregards\t..";
= split(/\t/, $sentence);
Join:
-----
join('SEPARATOR STRING', STRING1, STRING2, ...);
Example:
my $sentence = join(" + ", 'xx', 'yy', 'zz');
print "$sentence ."; will outputs : xx + yy + zz .
|