Introduction to the Perl language, Issue#1

in #utopian-io7 years ago (edited)

perl.jpg
Image Source

<h4><code>What Will I Learn? <p dir="auto">Introductory training support for programming with the Perl language. <ul> <li>Perl Language Introduction <li>PERL Overview <li>PERL Language Data types <h4><code>Requirements <p dir="auto">Basic Knowledge of programming and software concept. <h4><code>Difficulty <ul> <li>Basic <h4><code>Tutorial Contents <hr /> <h5><code>1 .INTRODUCTION <hr /> <p dir="auto"><code>A .What is Perl? <hr /> <p dir="auto">P.E.R.L. means Practical Extraction and Report Language. What we could (try to) translate as "practical language of extraction and editing".<br /> Created in 1986 by Larry Wall (System Engineer). Initially to manage a "News" system between two networks. <p dir="auto"><code>It is :<br /> _________,<br /> • A programming language<br /> • Free software (which can be obtained on the Internet in particular)<br /> • An interpreted language:<br /> no compilation<br /> slower than a compiled program<br /> each "script" requires the Perl interpreter on the machine to run. <p dir="auto"><code>Why Perl became popular:<br /> ________________________________,<br /> • portability: Perl exists on most platforms today (Unix, NT, Windows, Mac,VMS, Amiga, Atari ...)<br /> • free: available on the Internet (as well as an impressive number of bookstores and utilities)<br /> • Simplicity: Some commands allow to do what a program of 500 lines in Cor in Pascal did.<br /> • robustness: No memory allocation to handle, strings, stacks, variable names Unlimited ... <p dir="auto"><code>B. What use of Perl? <hr /> <p dir="auto"><code>Originally Perl was created for:<br /> _______________________________________, <ol> <li>manipulate files (especially to manage several files at the same time), <li>manipulate texts (search, substitution), <li>manipulate processes (especially through the network). <p dir="auto">Perl was mainly for the UNIX world <p dir="auto"><code>Why are you using Perl today?<br /> ____________________________________, <ol> <li>generate, update, analyze HTML files (especially for writing CGI), <li>"universal" access to databases, <li>file format conversion. <p dir="auto">Perl is no longer tied to the UNIX world <p dir="auto"><code>Perl is not made for:<br /> _______________________________,<br /> • write interactive interfaces (but there is now the Tk module, which allows it, on Unix or Windows),<br /> • scientific computing (Perl is not compiled: performance problem if we want by example do multiplications of matrices, but again there is the module PDL). <h2><code>C . versions <p dir="auto"><code>Three main versions of Perl:<br /> • Perl 4: The old version that did not integrate object programming (sometimes still used)<br /> • Perl 5.6: The old version<br /> • Perl 5.8: The new version that incorporates Unicode (international character encoding) <hr /> <h5><code>2 .PERL Overview <hr /> <p dir="auto"><code>Little polite program -<br /> ------------------------------, <pre><code>#!/bin/perl print "Bonjour"; <p dir="auto"><code>Display the number of lines in a file:<br /> ------------------------------------------------, <pre><code>#! / Bin / perl open (F, '<myfile') || die "Opening problem: $!" ; my $ i = 0; while (my $ line = <F>) { $ I ++; } close F; print "Number of lines: $ i"; <p dir="auto"><code>F is a file descriptor, which we can call as we want (the use is that we note the file descriptors in uppercase).<code>Each Perl statement ends with a semicolon. <hr /> <h5><code>3 .PERL Data types <hr /> <p dir="auto"><code>A. Constants<br /> -------------------,<br /> <code>1, -12345, 0.1415, 1.6E16 (means 160,000,000,000,000,000), 'cherry', 'a','the fruits of the palm are the dates' <p dir="auto"><code>B. Scalars<br /> -------------------,<br /> <code>Scalars are preceded by the $ character - <pre><code>$ 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' <p dir="auto"><code>Caution: Do not put spaces in variable names. A name can be as long we want it in the latest version, <p dir="auto"><code>C. Table lists<br /> ---------------------,<br /> <code>In Perl, arrays can be used as sets or lists.<br /> Always preceded by the<code>"@" character <pre><code>@ 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'); <p dir="auto"><code>we refer to an element of the table according to its index by: <pre><code>$ digits [1] (=> 2) $ fruits [0] (=> 'almond') <p dir="auto"><code>NOTE: In Perl (as in C) arrays start at index 0 <p dir="auto"><code>You can assign a table to another table: <pre><code>@ch = @ digits; @alphanum = (@alphabet, '_', @ digits); ## EQU1 ## ',' 8 ',' 9 ',' 0 ') @ set = (@ digits, 'date', 'kiwi', 12.45); <p dir="auto"><code>Notes:<br /> We have a special scalar: $ # array that indicates the last array index (and therefore its size - 1): $ fruits [$ # fruits] (=> 'cherry')<br /> <code>Ability to reference a part of a table <pre><code>@cards [6 .. $ # cards] => ('07', '08', '09', '10', 'Valet', 'Lady', 'King') @fruits [0..1] => ('almond', 'strawberry') <p dir="auto"><code>D. Indexed (or associative) tables<br /> ---------------------------------------------,<br /> <code>They are always preceded by the% character: <pre><code>% price = ('almond' => 30, 'strawberry' => 9, 'cherry' => 25); `or :` % price = (kernel => 30, strawberry => 9, cherry => 25); <p dir="auto"><code>We then refer to an element of the array as: <pre><code>$ price {'cherry'} (=> 25) `(or $ price {cherry}` `Examples:` % digit = (); $ digit {'a'} = 1; => or $ digit {a} = 1; print $ digit {'one'}; $ var = 'one'; print $ digit {$ var}; <p dir="auto"><code>E. Remarks<br /> -------------------,<br /> 1 . <code>Perl5 allows combinations, like a table of tables: <pre><code>% 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']); <p dir="auto">2 . <code>No Booleans (as in C language)<br /> We use integers knowing that 0 is evaluated as false (in fact it is the chain of empty character) and 1 as true. <pre><code>my $ two_greater_what_un = (2> 1); if ($ two_more_large_one) { print "Ok!"; } <p dir="auto">3 . <code>Two-dimensional tables<br /> <code>Indexed tables can be used to simulate 2 (or n) dimensional tables: <pre><code>% 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 <p dir="auto">We will then refer to 2 * 6 by $ table_mult [6] [2] <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@meblogger/introduction-to-the-perl-language-issue-1" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Sort:  

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?