Introduction to the Perl language, Issue#1
What Will I Learn?
Introductory training support for programming with the Perl language.
- Perl Language Introduction
- PERL Overview
- PERL Language Data types
Requirements
Basic Knowledge of programming and software concept.
Difficulty
- Basic
Tutorial Contents
1 .INTRODUCTION
A .What is Perl?
P.E.R.L. means Practical Extraction and Report Language. What we could (try to) translate as "practical language of extraction and editing".
Created in 1986 by Larry Wall (System Engineer). Initially to manage a "News" system between two networks.
It is
:
_________,
• A programming language
• Free software (which can be obtained on the Internet in particular)
• An interpreted language:
no compilation
slower than a compiled program
each "script" requires the Perl interpreter on the machine to run.
Why Perl became popular
:
________________________________,
• portability: Perl exists on most platforms today (Unix, NT, Windows, Mac,VMS, Amiga, Atari ...)
• free: available on the Internet (as well as an impressive number of bookstores and utilities)
• Simplicity: Some commands allow to do what a program of 500 lines in Cor in Pascal did.
• robustness: No memory allocation to handle, strings, stacks, variable names Unlimited ...
B. What use of Perl?
Originally Perl was created for:
_______________________________________,
- manipulate files (especially to manage several files at the same time),
- manipulate texts (search, substitution),
- manipulate processes (especially through the network).
Perl was mainly for the UNIX world
Why are you using Perl today?
____________________________________,
- generate, update, analyze HTML files (especially for writing CGI),
- "universal" access to databases,
- file format conversion.
Perl is no longer tied to the UNIX world
Perl is not made for:
_______________________________,
• write interactive interfaces (but there is now the Tk module, which allows it, on Unix or Windows),
• scientific computing (Perl is not compiled: performance problem if we want by example do multiplications of matrices, but again there is the module PDL).
C . versions
Three main versions of Perl:
• Perl 4: The old version that did not integrate object programming (sometimes still used)
• Perl 5.6: The old version
• Perl 5.8: The new version that incorporates Unicode (international character encoding)
2 .PERL Overview
Little polite program
-
------------------------------,
#!/bin/perl
print "Bonjour";
Display the number of lines in a file:
------------------------------------------------,
#! / Bin / perl
open (F, '<myfile') || die "Opening problem: $!" ;
my $ i = 0;
while (my $ line = <F>) {
$ I ++;
}
close F;
print "Number of lines: $ i";
F
is a file descriptor, which we can call as we want (the use is that we note the file descriptors in uppercase).Each Perl statement ends with a semicolon
.
3 .PERL Data types
A. Constants
-------------------,
1, -12345, 0.1415, 1.6E16 (means 160,000,000,000,000,000), 'cherry', 'a','the fruits of the palm are the dates'
B. Scalars
-------------------,
Scalars are preceded by the $ character
-
$ i = 0; $ c = 'a';
$ my_fruit_prefere = 'kiwi';
$ root__read_of_2 = 1.41421;
$ string = '100 grams of $ my_fruit_prefere';
=> '100 grams of $ my_fruit_prefere'
$ string = "100 grams of $ my_fruit_prefere";
=> '100 grams of kiwi'
Caution: Do not put spaces in variable names. A name can be as long we want it in the latest version,
C. Table lists
---------------------,
In Perl, arrays can be used as sets or lists.
Always preceded by the"@"
character
@ digits = (1,2,3,4,5,6,7,8,9,0);
@fruits = ('almond', 'strawberry', 'cherry');
@alphabet = ('a' .. 'z'); Both points mean "so much to so much"
@a = ('a'); @nul = ();
@cartes = ('01' .. '10', 'Valet', 'Lady', 'King');
we refer to an element of the table according to its index by:
$ digits [1] (=> 2)
$ fruits [0] (=> 'almond')
NOTE: In Perl (as in C) arrays start at index 0
You can assign a table to another table:
@ch = @ digits;
@alphanum = (@alphabet, '_', @ digits);
## EQU1 ## ',' 8 ',' 9 ',' 0 ')
@ set = (@ digits, 'date', 'kiwi', 12.45);
Notes:
We have a special scalar: $ # array that indicates the last array index (and therefore its size - 1): $ fruits [$ # fruits] (=> 'cherry')
Ability to reference a part of a table
@cards [6 .. $ # cards] => ('07', '08', '09', '10', 'Valet', 'Lady', 'King')
@fruits [0..1] => ('almond', 'strawberry')
D. Indexed (or associative) tables
---------------------------------------------,
They are always preceded by the% character:
% price = ('almond' => 30, 'strawberry' => 9, 'cherry' => 25);
`or :`
% price = (kernel => 30, strawberry => 9, cherry => 25);
We then refer to an element of the array as:
$ price {'cherry'} (=> 25)
`(or $ price {cherry}`
`Examples:`
% digit = ();
$ digit {'a'} = 1; => or $ digit {a} = 1;
print $ digit {'one'};
$ var = 'one'; print $ digit {$ var};
E. Remarks
-------------------,
1 . Perl5 allows combinations, like a table of tables:
% seasons = (
Apricot '=> [' been '],
'strawberry' => ['spring', 'summer'],
'apple' => ['autumn', 'winter'],
'orange' => ['winter'],
'cherry' => ['spring'],
'almond' => ['spring', 'summer', 'autumn', 'winter']);
`or`
@departements = (
['Ain', 'Bourg-en-Bresse', 'Rhône-Alpes'],
['Aisne', 'Laon', 'Picardie'],
['Yonne', 'Auxerre', 'Burgundy']);
2 . No Booleans (as in C language)
We use integers knowing that 0 is evaluated as false (in fact it is the chain of empty character) and 1 as true.
my $ two_greater_what_un = (2> 1);
if ($ two_more_large_one) {
print "Ok!";
}
3 . Two-dimensional tables
Indexed tables can be used to simulate 2 (or n) dimensional tables:
% table_multiplication = ('1,1' => 1, '1,2' => 2, ...,
'9.8' => 72, '9.9' => 81);
$ translation {'almond', 'english'} = 'almond';
$ translation {'almond', 'italian'} = 'amoria';
$ translation {'cherry', 'english'} = 'cherry';
You can also use Perl 5's table tables to do this:
@table_multiplication = (
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # Multiplied by 0
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], # Multiplied by 1
[0, 2, 4, 6, 8,10,12,14,16,18], # Multiplied by 2
...
[0, 9,18,27,36,45,54,63,72,81]); # Multiplied by 9
We will then refer to 2 * 6 by $ table_mult [6] [2]
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not follow the Utopian Rules.
Your contribution doesn't meet the minimum requirement of explaining 3 concepts. The first one about what is perl is not included because it is not a "how to". Also, your use of red formatting significantly makes the post look sharp. There has been rule changes in utopian recently. Please read the new rules clearly before submitting posts.
You can contact us on Discord.
[utopian-moderator]
Sorry to say but can you please be specific? which explaining 3 concepts are concepts are you taking about?