https://script.spoken-tutorial.org/api.php?action=feedcontributions&user=AmolBrahmankar&feedformat=atomScript | Spoken-Tutorial - User contributions [en]2024-03-29T09:49:24ZUser contributionsMediaWiki 1.23.17https://script.spoken-tutorial.org/index.php/PERL/C2/Overview_and_Installation_of_PERL/EnglishPERL/C2/Overview and Installation of PERL/English2014-05-30T06:29:18Z<p>AmolBrahmankar: Created page with ''''Title Of Script: PERL Overview and Installing Perl on WIndows and Linux''' '''Author:''' Amol Brahmankar '''Keywords: '''PERL Overview,''' '''Installing Perl video tutorial,…'</p>
<hr />
<div>'''Title Of Script: PERL Overview and Installing Perl on WIndows and Linux'''<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords: '''PERL Overview,''' '''Installing Perl video tutorial, Perl on Windows, Perl on Ubuntu Linux.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual Clue'''</center><br />
| style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on <br />
<br />
* '''PERL Overview '''and <br />
* Installation of '''Perl.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In this tutorial, I'll take you through''' '''<br />
<br />
* '''PERL Overview '''along with <br />
* '''Installation steps '''for''' PERL '''on''' Ubuntu-Linux '''and''' Windows '''operating system.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For this tutorial,<br />
<br />
* You must be connected to the '''Internet'''.<br />
* You must have''' '''either '''Ubuntu''' '''Linux '''and '''Windows''' Operating System.<br />
<br />
For demonstration purpose, I will be using '''Ubuntu''' '''Linux12.04''' and '''Windows 7 '''operating system.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For '''Ubuntu Linux '''installation, you must have '''Synaptic Package Manager '''installed on your system. <br />
<br />
<br />
You must have administrative rights.<br />
<br />
And you must also have knowledge of using '''Terminal''' and '''Synaptic Package Manager''' in '''Ubuntu'''. <br />
<br />
If not, please see the '''Linux series''' available on the spoken tutorial website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let me give you an overview of '''PERL''' Language. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''PERL''' is an acronym which stands for''' Practical Extraction and Reporting Language.'''<br />
* It is a general-purpose programming language.<br />
* It was originally developed for '''text manipulation'''.<br />
* Now, it is used for '''web development, network programming, GUI development''' etc<br />
* It is simple and very easy to understand.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * It does not have complex '''data structures''' like '''C''' or '''JAVA.'''<br />
* It is well known for '''pattern matching'''.<br />
* And most importantly, '''PERL''' is an open source language.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
Installing '''PERL''' on '''Ubuntu 12.04''' Operating System<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''PERL''' comes pre-loaded on '''Ubuntu Linux '''12.04 OS.<br />
<br />
<br />
Therefore, there is no specific procedure to be followed for installation.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us check the installed version of '''PERL '''on''' Ubuntu 12.04. '''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''CTRL+ALT+T''' keys<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the terminal by pressing '''''ctrl + alt + t '''''simultaneously on the keyboard.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Type '''perl -v '''>> Press '''Enter'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then, type '''perl hyphen v''' <br />
<br />
<br />
and then press '''ENTER.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight output on '''terminal.'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get an output as displayed here, on the terminal.<br />
<br />
<br />
This output shows us the current installed version of''' PERL.'''<br />
<br />
<br />
In my case it is '''PERL 5.14.2'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us check the default '''PERL packages '''available on '''Ubuntu 12.04. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Go to the '''launcher bar'''>> Click on '''Dash Home.'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let's go to the '''launcher bar '''and click on '''Dash Home.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Type '''Synaptic '''in the '''search bar'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In the '''search bar, '''type '''Synaptic.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Synaptic Package Manager '''icon.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Synaptic Package Manager '''icon will be visible.<br />
<br />
Click on it. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Enter admin password >> click on '''Authenticate.'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will be prompted for your '''admin''' '''password''', for '''Authentication '''purposes.<br />
<br />
<br />
Enter your '''admin password '''and click on '''Authenticate.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Immediately, '''Synaptic Package Manager '''will load the package list.<br />
<br />
<br />
This may take some time depending on your internet and system speed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Type '''Perl''' in '''Quick Filter'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Once loaded, type '''Perl''' in '''Quick Filter'''.<br />
<br />
<br />
You will see a list of packages.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to the green color solid-filled checkboxes<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The green color solid-filled checkbox before a '''PERL package '''indicates that it is already installed. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to checkboxes with star marks<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Whereas checkboxes with star marks, indicate that you may need these packages, as well.<br />
<br />
<br />
These will help you to create a documentation or debug a '''PERL '''script. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Install any of the missing packages, depending on your future use of '''PERL.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| <nowiki><<PAUSE>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us now go through the steps to install PERL on '''Windows''' Operating System.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
Installing PERL on Windows Operating System<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * At the time of '''recording''' the '''Perl tutorials''', version '''5.14.2''' was available in '''Windows.'''<br/> <br />
<br />
* Now, there is a new '''PERL''' version available.<br/> <br />
<br />
* I will be demonstrating the installation using the new '''PERL''' version''' 5.16.3'''<br/> <br />
<br />
* All the '''PERL commands''' shown in the tutorials will work perfectly, on the new version as well.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| On the browser>> in the '''address bar''', type the '''URL'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the any web browser in your Windows machine and in the '''address bar''', type the '''URL '''as shown.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| New webpage opens.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will be directed to the download page of '''PERL.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to '''32 bit '''version of '''PERL.'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Select the '''download version,''' as per your '''system specifications.'''<br />
<br />
<br />
In my case, I will select '''32 bit '''version of '''PERL.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to '''Save '''button.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save the '''Perl msi file''' on your computer in a location of your choice.<br />
<br />
<br />
I have already saved it on my machine.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Go to the saved folder>> doubleclick on the '''msi '''file.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''folder''' where you have downloaded '''PERL msi''' file and double-click on it.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Run'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then click on '''Run''' in the '''pop-up''' window.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Next '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Click on '''Next i'''n the '''setup wizard '''window.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Agree'''>> click on '''Next'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Accept the '''License Aggrement when prompted '''and then click on '''Next'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, '''Custom Setup''' window will appear.<br />
<br />
<br />
This window lists all the '''PERL '''features that get installed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to each one as per narration<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| These are;<br />
<br />
* '''Perl,'''<br />
* '''PPM utilty,''' used to download and install '''Perl Modules''' on''' Windows'''<br />
* Documentation which provides the documentation for''' Perl Modules'''<br />
* And examples of '''Perl'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Next'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Keep all these '''default features''' and click on '''Next'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Point to the checkbox.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| A popup window for setting '''environmental variable''' and '''file extension''' will appear.<br />
<br />
<br />
Keep the '''check box checked,''' as shown here.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on '''Next >> Install'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Click on '''Next '''and then''' Install'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will start the installation of '''PERL. '''<br />
<br />
<br />
This may take some time depending on your '''internet '''speed. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Uncheck the '''Display Release Note checkbox >> '''click on '''Finish'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Once done, uncheck the '''Display Release Note checkbox '''and then click on '''Finish'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This completes the installation of '''PERL''' on '''Windows.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| <nowiki>.<PAUSE></nowiki><br />
<br />
<br />
Now let us verify the installation.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Click on Start menu >> Type cmd<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Go to '''Start''' '''menu '''and type '''cmd''' to open the '''command prompt.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Type '''perl space hyphen v''' <br />
<br />
<br />
>> press '''ENTER'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| On the '''command prompt,'''<br />
<br />
type '''perl space hyphen v''' <br />
<br />
<br />
and press '''ENTER'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will see the installed version of '''PERL.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| If it doesn't show the version, repeat the above installation steps once again.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| <nowiki><PAUSE></nowiki><br />
<br />
<br />
Now let us execute a simple '''Hello Perl''' program.<br />
<br />
<br />
This file has been provided to you along with this tutorial, in the Code Files link, below the player.<br />
<br />
<br />
Pls download and use this file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| In the command prompt window >> type cd users\Amol<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I had saved the file in '''users\Amol '''directory on my system. So, let's go there by typing.<br />
<br />
<br />
cd user\Amol<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Type '''perl sampleProgram.pl''' <br />
<br />
<br />
>> press''' Enter'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then type '''perl sampleProgram.pl''' <br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Hello Perl''' will get printed on the '''command prompt, '''as shown.<br />
<br />
<br />
<nowiki><PAUSE></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us summarize.<br />
<br />
In this tutorial, we have learrnt:<br />
<br />
* '''Overview''' of PERL and<br />
* Installation instructions of '''PERL''' for '''Ubuntu 12.04''' and '''Windows 7.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| About the Project<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Blocks-in-Perl/EnglishPERL/C2/Blocks-in-Perl/English2014-04-10T06:07:59Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''BLOCKS in PERL '''Author:''' Amol Brahmankar '''Keywords:''' BLOCKS in perl video tutorial. {| style="border-spacing:0;" | style="border-top:0.0007in …'</p>
<hr />
<div>'''Title Of Script: '''BLOCKS in PERL<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' BLOCKS in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual Clue'''</center><br />
| style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on BLOCKS in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In this tutorial, we will learn about the various '''BLOCKS '''available in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I am using '''Ubuntu Linux 12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| As a pre-requisite,<br />
<br />
you should have basic knowledge of '''Variables, Comments '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' data structures in PERL '''will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Perl provides 5 special '''blocks'''.<br />
<br />
<br />
These '''blocks '''get executed at various stages of a '''Perl '''program.<br />
<br />
<br />
These blocks are:<br />
<br />
# BEGIN<br />
# END<br />
# UNITCHECK<br />
# CHECK<br />
# INIT<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us start with understanding the '''BEGIN''' block<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''BEGIN''' block get executed at the time of compilation. <br />
* So, any code written inside this block gets executed first during compilation.<br />
* We can have several '''BEGIN''' blocks inside a '''Perl '''script.<br />
* These blocks will get executed in the order of declaration.<br />
* That is in the '''First define First execute '''pattern<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
'''BEGIN {'''<br />
<br />
'''<nowiki># Piece of code to be executed at the time of compilation</nowiki>'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''BEGIN''' block''' '''is as follows <br />
<br />
<br />
'''BEGIN''' in capital letters space open curly bracket<br />
<br />
'''Press Enter'''<br />
<br />
<br />
Piece of code to be executed at the time of compilation<br />
<br />
'''Press Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us look at an example of '''BEGIN '''blocks.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit beginBlock.pl '''and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit beginBlock dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
'''print "First Line in PERL script\n"; '''<br />
<br />
'''BEGIN { '''<br />
<br />
'''print "Inside First BEGIN block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''BEGIN { '''<br />
<br />
'''print "Inside Second BEGIN block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''BEGIN { '''<br />
<br />
'''print "Inside Third BEGIN block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''print "Last line in PERL script\n";'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''beginBlock dot pl''' file in '''gedit'''.<br />
<br />
<br />
Type the following piece of code as displayed on the screen.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''Gedit'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us look at what I have written inside the script.<br />
<br />
<br />
* Here we have printed some text before and after the '''BEGIN''' blocks.<br/> <br />
<br />
* Similarly, I have written one '''print statement''' in each '''BEGIN''' block.<br/> <br />
<br />
* Please note, I have '''not''' given the '''semicolon''' after the '''BEGIN''' blocks.<br/> <br />
<br />
* Putting a semicolon, will result in a '''syntax error''' on execution of the program.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to terminal<br />
<br />
<br />
'''perl beginBlock.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and execute the script by typing,<br />
<br />
<br />
'''perl beginBlock dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal<br />
<br />
<br />
Inside First BEGIN block <br />
<br />
Inside Second BEGIN block <br />
<br />
Inside Third BEGIN block <br />
<br />
First Line in PERL script <br />
<br />
Last line in PERL script <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the '''terminal'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Notice that;'''<br />
<br />
* The line written inside the first '''BEGIN''' '''block '''gets''' '''printed first and <br />
* The first '''print statement''' in the script actually gets printed after the '''BEGIN block statements.'''<br />
* '''BEGIN blocks''' gets executed in the order of their declaration.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From this example, it is evident that:<br />
<br />
* The code written inside the '''BEGIN''' blocks get executed '''first'''.<br />
* This is irrespective of the location of the '''BEGIN''' block inside '''PERL''' script.<br />
* '''BEGIN blocks''' always get executed in the '''First In First Out''' manner<br />
<br />
So one of the use of this '''block''' is to include files inside a '''Perl '''script, before actual execution starts.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us understand the '''END''' block<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''END''' block get executed at the end of the '''PERL''' program<br />
* Code written inside this '''block''' gets executed after '''PERL''' has finished executing the program<br />
* We can have several '''END''' blocks inside a '''Perl''' script.<br />
* These '''blocks''' will get executed in reverse order of definition.<br />
* That is in '''Last define First execute''' pattern.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
'''END {'''<br />
<br />
'''<nowiki># Piece of code to be executed at the end of the PERL script</nowiki>'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''END''' block''' '''is as follows <br />
<br />
<br />
'''END''' in capital letters space open curly bracket<br />
<br />
'''Press Enter'''<br />
<br />
<br />
Piece of code to be executed at the end of the PERL script<br />
<br />
<br />
'''Press Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us look at an example of '''END '''blocks.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit endBlock.pl '''and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit endBlock dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
'''print "First Line in PERL script\n"; '''<br />
<br />
'''END { '''<br />
<br />
'''print "Inside First END block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''END { '''<br />
<br />
'''print "Inside Second END block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''END { '''<br />
<br />
'''print "Inside Third END block\n"; '''<br />
<br />
'''} '''<br />
<br />
'''print "Last line in PERL script\n";'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This will open the '''endBlock dot pl''' file in '''gedit'''.<br />
<br />
<br />
Type the following piece of code as displayed on the screen.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us look at what I have written inside this script.<br />
<br />
<br />
* Here we have printed some text before and after the '''END''' blocks.<br/> <br />
<br />
* Similarly, we have written one print statement in each '''END''' block.<br/> <br />
<br />
* Please note, I have not given the semicolon after the '''END''' block.<br/> <br />
<br />
* If we give the semicolon, there will be a syntax error on compilation.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to terminal<br />
<br />
<br />
'''perl endBlock.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and execute the script by typing,<br />
<br />
<br />
'''perl endBlock dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal<br />
<br />
<br />
First Line in PERL script <br />
<br />
Last line in PERL script <br />
<br />
Inside Third END block <br />
<br />
Inside Second END block <br />
<br />
Inside First END block <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You will get the output as displayed on the terminal<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Notice that;<br />
<br />
* The line written inside the '''END''' block is printed last.<br />
* The last '''print statement '''in the script actually gets printed before the '''END''' block statements and<br />
* '''END blocks''' gets executed in the reverse order of their declaration<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| From the example, it is evident that;<br />
<br />
* The code written inside the '''END''' blocks get executed at the end<br />
* This is irrespective of the location of the '''END''' block inside the '''PERL '''script and <br />
* '''END blocks''' gets executed in the '''Last In First Out '''manner<br />
<br />
So, one use of''' END block''' is to destroy objects created in the program, before exiting.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Similarly '''PERL''' has '''UNITCHECK, CHECK''' and '''INIT''' blocks.<br />
<br />
<br />
These blocks are used rarely by developers and are a bit difficult to understand.<br />
<br />
<br />
So, I will be just brief you about these blocks.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''UNITCHECK, CHECK '''and '''INIT''' blocks are useful;<br />
<br />
* to catch the '''transition''' between '''compilation''' and '''execution''' '''phase''' of the main program and <br />
* to perform some '''checks''' or '''initialisation''' after compilation and before execution <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''UNITCHECK''' and '''CHECK''' blocks runs in '''Last in First out''' manner<br />
<br />
<br />
whereas<br />
<br />
<br />
'''INIT''' block runs in '''First In First Out''' manner.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
'''UNITCHECK {'''<br />
<br />
'''<nowiki># Piece of code to be executed</nowiki>'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''UNITCHECK''' block''' '''is as follows <br />
<br />
<br />
'''UNITCHECK''' in capital letters space open curly bracket<br />
<br />
Press''' Enter'''<br />
<br />
<br />
Piece of code to be executed<br />
<br />
<br />
Press''' Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
'''CHECK {'''<br />
<br />
'''<nowiki># Piece of code to be executed</nowiki>'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''CHECK''' block''' '''is as follows <br />
<br />
<br />
'''CHECK''' in capital letters space open curly bracket<br />
<br />
Press''' Enter'''<br />
<br />
<br />
Piece of code to be executed<br />
<br />
<br />
Press''' Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
'''INIT {'''<br />
<br />
'''<nowiki># Piece of code to be initialised</nowiki>'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for '''INIT''' block''' '''is as follows <br />
<br />
<br />
'''INIT''' in capital letters space open curly bracket<br />
<br />
Press''' Enter'''<br />
<br />
<br />
Piece of code to be initialised<br />
<br />
<br />
Press''' Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For better understanding, I recommend that you experiment with these blocks in your Perl scripts. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
* '''BEGIN '''and''' END '''blocks''' '''in''' details '''and<br/> <br />
<br />
* '''Introduction '''to''' UNITCHECK, CHECK '''and''' INIT '''blocks''' '''<br/> <br />
<br />
* using sample program<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Here is assignment for you -<br />
<br />
<br />
Type the below code inside a PERL script;<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
'''END {'''<br />
<br />
'''@array = ();'''<br />
<br />
'''print "Length of an array inside END block: ", $#array + 1, "\n";'''<br />
<br />
'''}'''<br />
<br />
<br />
'''print "Length of an array: ", $#array + 1, "\n";'''<br />
<br />
<br />
'''BEGIN {'''<br />
<br />
'''@array = (1, 2, 3);'''<br />
<br />
'''}'''<br />
<br />
<br />
execute the script and observe the output.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| About the Project<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Watch the video available at the following link.<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team <br />
<br />
<br />
* Conducts workshops using spoken tutorials<br />
* Gives certificates to those who pass an online test<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hyphen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Hash-in-Perl/EnglishPERL/C2/Hash-in-Perl/English2014-03-07T06:48:24Z<p>AmolBrahmankar: Created page with ''''Title Of Script: Hash in Perl''' '''Author:''' Amol Brahmankar '''Keywords: '''Hash in perl, Associative Array in Perl video tutorial. {| style="border-spacing:0;" | styl…'</p>
<hr />
<div>'''Title Of Script: Hash in Perl'''<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords: '''Hash in perl, Associative Array in Perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual Cue'''</center><br />
| style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on '''Hash''' in '''Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In this tutorial, we will learn about;<br />
<br />
* '''Hash''' in '''Perl '''and<br />
* Accessing element of a '''hash'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For this tutorial, I am using <br />
<br />
* '''Ubuntu Linux12.04''' operating system <br />
* '''Perl 5.14.2 '''and <br />
* '''gedit '''Text Editor<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| To practise this tutorial, you should have knowledge of '''Variables & Data Structures '''in''' Perl.'''<br />
<br />
<br />
Knowledge of''' comments, loops, conditional statements '''and '''Arrays''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * '''Hash''' is an unordered collection of data<br />
* It's a '''key/value pair data structure'''<br />
* '''Hash keys''' are unique<br />
* However, '''Hash''' can have duplicate values<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax of '''hash '''is as shown.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us see how to get the value of a '''key '''from '''hash'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
$hashName{'KeyName'}<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for accessing the value of a '''key '''is<br />
<br />
<br />
'''dollar hashName open curly bracket single quote keyName single quote close curly bracket'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand '''hash''' using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to gedit with '''perlHash.pl '''opened<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I have already typed the code in '''perlHash dot pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''Gedit'''<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''%hash = ( '''<br />
<br />
''''Name' => 'John', '''<br />
<br />
''''Department' => 'Finance' '''<br />
<br />
'''); '''<br />
<br />
<br />
'''print "Value of Key Name is: ", $hash{'Name'}, "\n";'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the code as shown in your '''perlHash dot pl '''file.<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''%hash'''<br />
<br />
<br />
'''highlight keys 'Name' & 'Department''''<br />
<br />
<br />
'''highlight values 'John' & 'Finance''''<br />
<br />
<br />
'''$hash{'Name'}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Hash''' in '''Perl''' is declared with '''percentage''' sign.<br />
<br />
<br />
These are the '''keys''' of '''hash.'''<br />
<br />
<br />
And these are the '''values''' of '''hash.'''<br />
<br />
<br />
'''Note:''' To access '''key''' of '''hash''', one has to use '''dollar '''sign.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to terminal<br />
<br />
<br />
'''perl perlHash.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script '''as''' '''<br />
<br />
<br />
'''perl perlHash dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal<br />
<br />
<br />
Value of Key Name is: John<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output is as shown on '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us see add and delete of '''keys''' from a '''hash'''. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax for<br />
<br />
1. adding key is<br />
<br />
'''dollar hashName open curly bracket'''<br />
<br />
'''single quote KeyName single quote close '''<br />
<br />
'''curly bracket equal to value semicolon'''<br />
<br />
2. deleting key is<br />
<br />
'''delete dollar hashName open curly bracket'''<br />
<br />
'''single quote KeyName single quote close '''<br />
<br />
'''curly bracket semicolon'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us understand this using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal hashKeyOperations dot pl<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| I have already typed the code in '''hashKeyOperations dot pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the code as shown in your '''hashKeyOperations dot pl '''file.<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <nowiki>#!/usr/bin/perl </nowiki><br />
<br />
use Data::Dumper; <br />
<br />
<br />
<nowiki>#Hash of marks obtained in a subject </nowiki><br />
<br />
%hash = ( <br />
<br />
'Subject' => 'Math', <br />
<br />
'Marks' => 98 <br />
<br />
); <br />
<br />
<br />
print "Hash before adding keys\n"; <br />
<br />
print Dumper \%hash; <br />
<br />
<br />
<nowiki>#Add Name and roll number of a student </nowiki><br />
<br />
$hash{'Name'} = 'Amol'; <br />
<br />
$hash{'RollNumber'} = 12; <br />
<br />
<br />
print "Hash after addition of Name & RollNumber keys\n"; <br />
<br />
print Dumper \%hash; <br />
<br />
<br />
delete $hash{'RollNumber'}; <br />
<br />
<br />
print "Hash after deletion of RollNumber key\n"; <br />
<br />
print Dumper \%hash;<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This is the declaration of '''hash.'''<br />
<br />
<br />
We'll be adding, deleting the '''keys '''from this '''hash.'''<br />
<br />
<br />
Here we are adding a '''key '''to an already created '''hash.'''<br />
<br />
It is like assigning a '''value '''to a '''variable.'''<br />
<br />
<br />
'''delete''' keyword is used to delete the '''key'''. <br />
<br />
We need to pass the '''key''' to delete it.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Press '''Ctrl+S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
'''perl hashKeyOperations.pl '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Switch to the '''terminal '''and execute the '''Perl script '''as<br />
<br />
'''perl hashKeyOperations dot pl '''and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Hash before adding keys <br />
<br />
$VAR1 = { <br />
<br />
'Subject' => 'Math', <br />
<br />
'Marks' => 98 <br />
<br />
}; <br />
<br />
Hash after addition of Name & RollNumber keys <br />
<br />
$VAR1 = { <br />
<br />
'Subject' => 'Math', <br />
<br />
'RollNumber' => 12, <br />
<br />
'Marks' => 98, <br />
<br />
'Name' => 'Amol' <br />
<br />
}; <br />
<br />
Hash after deletion of RollNumber key <br />
<br />
$VAR1 = { <br />
<br />
'Subject' => 'Math', <br />
<br />
'Marks' => 98, <br />
<br />
'Name' => 'Amol' <br />
<br />
};<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Output will be as shown on the '''terminal.'''<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us look at '''sorting''' of a '''hash keys '''and''' values.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Syntax to '''sort keys''' is<br />
<br />
'''sort open bracket keys percentage hashName close bracket semicolon'''<br />
<br />
<br />
Similarly, we can '''sort hash values '''as<br />
<br />
'''sort open bracket values percentage hashName close bracket semicolon'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand '''sorting '''functionality using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let me switch to '''sortHash dot pl '''on '''gedit.'''<br />
<br />
<br />
Type the code as displayed on the screen in''' '''your '''sortHash dot pl '''file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <nowiki>#!//usr/bin/perl </nowiki><br />
<br />
<br />
%addressHash = ( <br />
<br />
'City' <nowiki>=> 'Pune', </nowiki><br />
<br />
'State' <nowiki>=> 'Maharashtra', </nowiki><br />
<br />
'Country' => 'India' <br />
<br />
); <br />
<br />
<br />
foreach $key (sort(keys %addressHash)) { <br />
<br />
print "Key: ", $key, " ==> ", $addressHash{$key}, "\n"; <br />
<br />
} <br />
<br />
<br />
foreach $value (sort(values %addressHash)) { <br />
<br />
print "Value: $value\n"; <br />
<br />
}<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Here we have declared '''hash''' of '''address'''.<br />
<br />
<br />
Here, to sort the '''keys''' we have used the '''sort''' inbuilt function, along with the '''keys '''function.<br />
<br />
<br />
This will sort the '''hash keys '''in alphabetical order.<br />
<br />
<br />
Similarly, we can use the '''sort '''function on '''values '''of '''hash'''.<br />
<br />
<br />
Sorting can also be done on '''numeric keys''' and/or '''values'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save the file and switch to '''terminal.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
'''perl sortHash.pl'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Execute the script by typing<br />
<br />
'''perl sortHash dot pl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Key: City ==> Pune <br />
<br />
Key: Country ==> India <br />
<br />
Key: State ==> Maharashtra <br />
<br />
Value: India <br />
<br />
Value: Maharashtra <br />
<br />
Value: Pune <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The output will be as shown on the '''terminal.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us '''see '''how to get all '''keys''' and '''values''' of '''hash.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Perl''' provides inbuilt function to fetch all the '''hash''' '''keys''' and '''values'''.<br />
<br />
# '''keys''' function is used to retrive all the '''keys''' of '''hash'''<br />
# '''values''' function returns '''values '''of all the keys whereas<br />
# '''each''' function iterates over '''hash '''and returns '''key/value '''pair from '''hash'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand these functions using a sample program.<br />
<br />
<br />
For this, we'll use '''perlHash dot pl''' script, which we created earlier in this tutorial.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''%hash = ( '''<br />
<br />
''''Name' => 'John', '''<br />
<br />
''''Department' => 'Finance' '''<br />
<br />
'''); '''<br />
<br />
<br />
'''print "Value of Key Name is: ", $hash{'Name'}, "\n"; '''<br />
<br />
<br />
'''@keysOfHash = keys(%hash); '''<br />
<br />
'''print "Keys of a Hash are: @keysOfHash\n"; '''<br />
<br />
<br />
'''@valuesOfHash = values(%hash); '''<br />
<br />
'''print "Values of a Hash are: @valuesOfHash\n"; '''<br />
<br />
<br />
'''while (($key, $value) = each(%hash)) { '''<br />
<br />
'''print "key: $key and value: $value\n"; '''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the following piece of code as shown on the screen;<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight comma in print statement<br />
<br />
<br />
'''@keysOfHash = keys(%hash); '''<br />
<br />
<br />
'''@valuesOfHash = values(%hash); '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand the code now.<br />
<br />
<br />
'''keys''' function on '''hash,''' returns an '''array''' which contains all '''keys''' of '''hash.'''<br />
<br />
<br />
'''values''' function on '''hash''' returns an''' array''' of '''values''' for all '''keys''' of '''hash.'''<br />
<br />
<br />
'''each '''function''' '''returns the '''key/value''' pair. <br />
<br />
<br />
Here, we have used the '''while loop.'''<br />
<br />
<br />
It will iterate over each '''key/value''' pair of '''hash, '''that is returned by '''each function.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Press '''Ctrl+S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
'''perl perlHash.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us execute the script on the '''terminal''' by typing<br />
<br />
'''perl perlHash dot pl'''<br />
<br />
Press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''Value of Key Name is: John '''<br />
<br />
'''Keys of a Hash are: Department Name '''<br />
<br />
'''Values of a Hash are: Finance John '''<br />
<br />
'''key: Department and value: Finance '''<br />
<br />
'''key: Name and value: John '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output will be seen on the '''terminal.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us see few other ways of looping over '''Hash.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
<br />
<br />
foreach $variable (keys(%hashName)) {<br />
<br />
<nowiki># Action to be performed</nowiki><br />
<br />
}<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We can use '''foreach loop '''to iterate over each key of '''Hash'''.<br />
<br />
<br />
Then perform a set of actions on the value of a '''key.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| On the Slide, point to each of the words as per the narration.<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The syntax is as displayed on the screen.<br />
<br />
<br />
Here, each iteration of '''foreach loop '''will assign''' key '''from '''hash '''to '''$variable.'''<br />
<br />
<br />
Then it will use that '''$variable''' to fetch the '''value '''or to perform a set of actions.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| foreach $variable (values(%hashName)) {<br />
<br />
<nowiki># Action to be performed</nowiki><br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Similarly, we can loop over '''hash values '''as shown on the screen.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We will look at a sample program. So, let me switch to '''loopingOverHash dot pl''' in '''gedit.''' <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''%hash = ( '''<br />
<br />
''''Name' => 'John', '''<br />
<br />
''''Department' => 'Finance' '''<br />
<br />
'''); '''<br />
<br />
<br />
'''foreach $key (keys(%hash)) { '''<br />
<br />
'''print "Key is $key and it's value is $hash{$key}\n"; '''<br />
<br />
'''} '''<br />
<br />
<br />
'''print "======================================\n"; '''<br />
<br />
<br />
'''foreach $value (values(%hash)) { '''<br />
<br />
'''print "Value is $value\n"; '''<br />
<br />
'''} '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the following piece of code as shown in your '''loopingOverHash dot pl '''file.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight<br />
<br />
<br />
'''foreach $key (keys(%hash))'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| This piece of code returns single '''key '''of '''hash.'''<br />
<br />
<br />
Here in our case, <br />
<br />
1st time '''dollar key ($key) '''contains the '''Department '''as '''key.'''<br />
<br />
<br />
In the next iteration of '''foreach, Name key '''is''' '''returned.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch back to the slides<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Note: '''Hash '''is an unordered collection of data.<br />
<br />
So, '''keys '''returned will not be in the sequence defined at the time of creating '''hash.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| '''Back to gedit'''<br />
<br />
'''foreach $value (values(%hash))'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The loop on '''values '''works in a similar way.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to terminal<br />
<br />
<br />
'''perl loopingOverHash.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then, switch to the '''terminal '''and execute the '''Perl script '''as''' '''<br />
<br />
<br />
'''perl loopingOverHash dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal<br />
<br />
<br />
'''Key is Department and it's value is Finance '''<br />
<br />
'''Key is Name and it's value is John '''<br />
<br />
'''<nowiki>====================================== </nowiki>'''<br />
<br />
'''Value is Finance '''<br />
<br />
'''Value is John '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The following output is displayed on the '''terminal.''' <br />
<br />
<br />
<nowiki><pause></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us summarize.<br />
<br />
In this tutorial, we learnt -<br />
<br />
* '''Hash '''in '''Perl '''and <br />
* Accessing '''elements '''of a '''hash'''<br />
* using sample programs.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Here is assignment for you -<br />
<br />
# Declare '''hash''' having student name as '''key''' <br />
# And his/her percentage as the '''value'''.<br />
# Loop over '''hash''' using '''keys, values '''and '''each '''function<br />
# Then print the '''percentage''' of each student.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| About the Project<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Functions-in-Perl/EnglishPERL/C2/Functions-in-Perl/English2013-11-18T12:14:15Z<p>AmolBrahmankar: Created page with ''''Title Of Script: Functions in Perl''' '''Author: Amol Brahmankar''' '''Keywords: Functions in Perl, video tutorial, sub, subroutine, function without arguments or return val…'</p>
<hr />
<div>'''Title Of Script: Functions in Perl'''<br />
<br />
'''Author: Amol Brahmankar'''<br />
<br />
'''Keywords: Functions in Perl, video tutorial, sub, subroutine, function without arguments or return value, function with arguments, function with a single return value, function with a multiple return values''' <br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.0007in solid #000000;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <center>'''Visual Cue'''</center><br />
| style="border:0.0007in solid #000000;padding:0.0382in;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Welcome to the spoken tutorial on '''Functions''' in''' Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In this tutorial, we will learn about -<br />
<br />
* '''Perl functions'''<br />
* '''functions''' with '''arguments'''<br />
* '''function '''with '''return values'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| For this tutorial, I am using <br />
<br />
* '''Ubuntu Linux12.04''' operating system<br />
* '''Perl 5.14.2'''<br />
* and the '''gedit '''Text Editor<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| You should have basic knowledge of '''variables, comments, loops, conditional statements '''and''' Data Structures '''in''' Perl.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| We will first see some simple''' Perl functions.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''sub simple_func {'''<br />
<br />
'''<nowiki># piece of code</nowiki>'''<br />
<br />
'''} '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| * In '''Perl''', '''functions,''' also called as '''subroutines,''' are declared with '''sub''' '''keyword.'''<br />
* The definition of a declared '''function''' is written between curly braces.<br />
* This '''function''' does not take any '''arguments.'''<br />
* And, it does not return anything.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Note: '''<br />
<br />
* '''function '''definition can be written anywhere in the script or in another '''module.'''<br />
* This '''module''', must''' '''then be included in the script, to use this '''function.'''<br />
* To include the '''module '''file in the script, one has to use the following syntax-<br />
<br />
'''use ModuleFileName;''' <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand this using asample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Directly switch to the file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open a file in your text editor and name it as '''simpleFunction dot pl'''<br />
<br />
<br />
Here is my '''simpleFunction dot pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| In '''gedit'''<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''<nowiki># Call the function</nowiki>'''<br />
<br />
'''printText();'''<br />
<br />
<br />
'''sub printText {'''<br />
<br />
'''print “This is illustration of simple Perl Function\n”;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the code as displayed on the screen.<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are just calling a '''function, '''which we have defined. <br />
<br />
Then, the execution control is passed to that '''function.'''<br />
<br />
<br />
This is the declaration & definition of the '''function'''. This '''function''' will print out the given text.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save your file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Switch to terminal<br />
<br />
<br />
'''perl simpleFunction.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Then switch to the '''terminal '''and '''execute '''the '''Perl''' script by typing<br />
<br />
<br />
'''perl simpleFunction dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Highlight the output on the terminal<br />
<br />
<br />
'''This is illustration of simple Perl Function'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| <nowiki>The output will be as shown <<pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us '''see '''a '''function '''with '''arguments.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''sub func_with_parameters {'''<br />
<br />
'''($variable1, $variable2,...$variableN) = @_;'''<br />
<br />
'''<nowiki># Action to be perform on the arguments pass to this function</nowiki>'''<br />
<br />
'''}'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand this '''function''' using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Directly switch to the file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open a file in your text editor and name it as '''functionWithArgs''' '''dot pl'''<br />
<br />
<br />
Here is my '''functionWithArgs''' script in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''<nowiki># Call the function</nowiki>'''<br />
<br />
'''addVariables(10, 20);'''<br />
<br />
<br />
'''sub addVariables {'''<br />
<br />
'''($var1, $var2) = @_;'''<br />
<br />
'''$addition = $var1 + $var2;'''<br />
<br />
'''print “Addition is: $addition\n”;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Type the following piece of code as shown on the screen.<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are calling a '''function''' with two '''arguments, 10''' and''' 20'''.<br />
<br />
<br />
* The passed arguments are caught in '''$var1 '''& '''$var2.'''<br />
* '''@_''' is a special '''Perl variable. '''We will''' '''cover its details in future tutorials. <br />
* This '''function''' performs the addition of 2 '''variables''' and prints the answer.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save your file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Note:<br />
<br />
* '''@_ '''is a''' special Perl array'''. <br />
* This '''array''' is used to store the passed '''arguments'''.<br />
* Similarly, we can catch the passed '''arguments''' in '''variables''' as<br />
<br />
'''$var1 = shift @_;'''<br />
<br />
'''$var2 = shift @_;'''<br />
<br />
* '''shift @_ '''removes the '''element '''at '''first position '''from '''@_ array''' and assigns it to a '''variable'''.<br />
* Another way is;<br/> '''<nowiki>$var1 = $_[0];</nowiki>'''<br/> '''<nowiki>$var2 = $_[1];</nowiki>'''<br />
* The above mentioned way is similar to fetching '''elements''' of '''@_ array''' using '''index.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
<br />
'''Addition is: 30'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, switch to '''terminal '''and execute the script<br />
<br />
by typing -<br />
<br />
<br />
'''perl functionWithArgs dot pl'''<br />
<br />
and press''' Enter'''<br />
<br />
<br />
The output is as displayed on the '''terminal.'''<br />
<br />
<nowiki><pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us look at a '''functions''' which returns a single value.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''sub return_single_value {'''<br />
<br />
'''<nowiki>#piece of code </nowiki>'''<br />
<br />
<br />
'''return $variable;'''<br />
<br />
'''}'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand the same using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Directly switch to the file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let me switch to '''funcWithSingleRtrnVal dot pl''' script in '''gedit.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''<nowiki># Call the function</nowiki>'''<br />
<br />
'''$addition = addVariables(10, 20);'''<br />
<br />
'''print “Addition is: $addition\n”;'''<br />
<br />
<br />
'''sub addVariables {'''<br />
<br />
'''($var1, $var2) = @_;'''<br />
<br />
'''$add = $var1 + $var2;'''<br />
<br />
'''return $add;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Open a file in your text editor and type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are calling '''addVariables function '''with '''parameters 10 '''and '''20. '''<br />
<br />
<br />
The return value of the '''function '''is caught in '''$addition variable.'''<br />
<br />
<br />
This '''function''' does the addition of the passed '''parameters''' and returns the answer.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
'''perl funcWithSingleRtrnVal.pl'''<br />
<br />
<br />
'''Addition is: 30'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us execute the script. So, switch to '''terminal '''and type-<br />
<br />
<br />
'''perl funcWithSingleRtrnVal dot pl'''<br />
<br />
and press '''Enter'''<br />
<br />
<br />
The output is as displayed on the '''terminal'''<br />
<br />
<nowiki><pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, let us see a '''function''' which returns multiple values.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''sub return_multiple_values {'''<br />
<br />
'''<nowiki>#piece of code </nowiki>'''<br />
<br />
<br />
'''return ($variable1, $variable2,...$variableN);'''<br />
<br />
'''}'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us understand the same, using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Directly switch to the file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| In '''gedit, '''I have opened a file and named it as '''funcWithMultipleRtrnVals dot pl'''<br />
<br />
<br />
Pls do likewise in your text editor.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
'''use Data::Dumper; '''<br />
<br />
<br />
'''<nowiki># Call the function </nowiki>'''<br />
<br />
'''($var1, $var2, $addition) = addVariables(10, 20); '''<br />
<br />
'''print "Addition of $var1 and $var2 is: $addition\n"; '''<br />
<br />
<br />
'''sub addVariables { '''<br />
<br />
'''($var1, $var2) = @_; '''<br />
<br />
'''$add = $var1 + $var2; '''<br />
<br />
'''return ($var1, $var2, $add); '''<br />
<br />
'''} '''<br />
<br />
<br />
'''@marksArray = getMarksArray(); '''<br />
<br />
'''print "Array\n"; '''<br />
<br />
'''print Dumper \@marksArray; '''<br />
<br />
<br />
'''%marksHash = getMarksHash(); '''<br />
<br />
'''print "Hash\n"; '''<br />
<br />
'''print Dumper \%marksHash; '''<br />
<br />
<br />
'''sub getMarksArray { '''<br />
<br />
'''@mArray = (98, 96, 95); '''<br />
<br />
'''return @mArray; '''<br />
<br />
'''} '''<br />
<br />
<br />
'''sub getMarksHash { '''<br />
<br />
'''%mHash = ( '''<br />
<br />
''''English' => 98, '''<br />
<br />
''''Physics' => 96, '''<br />
<br />
''''Chemistry' => 95 '''<br />
<br />
'''); '''<br />
<br />
'''return %mHash; '''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now, type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are calling '''addVariables function''' with '''parameters''' '''10''' and '''20'''. <br />
<br />
<br />
The return values of the '''function''' are caught in '''variables''' '''$var1, $var2''', '''$addition'''.<br />
<br />
<br />
This '''function''' does addition and returns the passed parameters and the resultant answer.<br />
<br />
<br />
This illustration demonstrates how we can return an '''array '''from a '''function.'''<br />
<br />
<br />
Similarly this demonstrates how '''hash''' can be returned from a '''function.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Save your file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Terminal<br />
<br />
<br />
'''Addition of 10 and 20 is: 30 '''<br />
<br />
'''Array '''<br />
<br />
'''<nowiki>$VAR1 = [ </nowiki>'''<br />
<br />
'''98, '''<br />
<br />
'''96, '''<br />
<br />
'''95 '''<br />
<br />
''']; '''<br />
<br />
'''Hash '''<br />
<br />
'''$VAR1 = { '''<br />
<br />
''''Chemistry' => 95, '''<br />
<br />
''''English' => 98, '''<br />
<br />
''''Physics' => 96 '''<br />
<br />
'''}; '''<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Now let us execute the '''Perl '''script on the '''terminal '''by typing -<br />
<br />
<br />
'''perl funcWithMultipleRtrnVals dot pl'''<br />
<br />
and press''' Enter.'''<br />
<br />
<br />
The output is as displayed on the '''terminal.'''<br />
<br />
<nowiki><pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| '''Perl''' provides several '''inbuilt functions.''' <br />
<br />
We learnt some of them in earlier tutorials. For eg-''' Arrays, Hash, sort, scalar, each, keys '''etc..<br />
<br />
<br />
Calling '''inbuilt''' '''functions''', similar to calling any other '''function, '''which we define.<br />
<br />
<br />
E.g '''sort(@arrayName);'''<br />
<br />
<br />
Try some incorporating some''' inbuilt functions''' in the sample programs we used.<br />
<br />
<br />
And observe their outputs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
* '''Functions '''in''' Perl'''<br />
* '''functions''' with '''arguments '''and<br />
* '''functions '''which''' return values'''<br />
<br />
using sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Here is assignment for you -<br />
<br />
# Write a '''function''' which takes 3 '''arguments'''.<br />
# Perform some action on these '''arguments'''<br />
# Return the result of the action performed on the '''arguments '''and print the same.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| About the Project<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:none;padding:0.0382in;"| <br />
| style="border-top:none;border-bottom:0.0007in solid #000000;border-left:0.0007in solid #000000;border-right:0.0007in solid #000000;padding:0.0382in;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Arrays/EnglishPERL/C2/Arrays/English2013-10-05T11:59:45Z<p>AmolBrahmankar: </p>
<hr />
<div>'''Title Of Script: Arrays in Perl'''<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords: '''Array in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arrays''' in '''Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about;<br />
<br />
* '''Index''' of an '''array'''<br />
* Length of an''' array'''<br />
* Accessing '''elements''' of an '''array'''<br />
* Looping over an '''array'''<br />
* '''Sequential Array'''<br />
* '''Array Slicing'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables & Data Structures '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''.<br />
* '''Array index''' starts from zero always.<br />
* In '''Perl''', it is not necessary to declare the length of an '''array'''.<br />
* '''Array '''length expands/shrinks as and when '''elements''' are added/removed from it.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax for declaring an array is -<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The last '''index''' of an '''array''' can be found with this command -<br />
<br />
'''$#myArray '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample '''program'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
gedit arrayIndex.pl & >> Enter<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the terminal and type <br />
<br />
'''gedit arrayIndex dot pl space ampersand '''<br />
<br />
and press '''Enter.'''<br />
<br />
<br />
This will open '''arrayIndex dot pl '''file in '''geditor'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
'''print “Last index of myArray is: $#myArray\n”;'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the piece of code that is displayed on screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
<br />
'''$#myArray'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here we have declared & defined an '''array''' which contains '''5 elements'''. <br />
<br />
<br />
As '''array index '''starts from zero, the last '''index''' value will be '''4''' <br />
<br />
<br />
i.e number of elements, which is 5, minus 1.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl+S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
Type<br />
<br />
'''perl arrayIndex.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to the '''terminal '''and '''execute '''the '''Perl''' script. Type -''' '''<br />
<br />
<br />
'''perl arrayIndex dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
Last index of myArray is: 4<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| <nowiki>The output shown on terminal is <<pause>></nowiki><br />
<br />
<br />
'''Last index of myArray is: 4'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us '''see how to get length of an array '''in perl.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are many ways by which we can find the length of an '''array. '''<br />
<br />
<br />
These are as follows- <br />
<br />
# Index of an '''array''' + 1; '''$#array + 1'''<br />
# Using '''PERL '''inbuilt '''scalar function; scalar(@array)'''<br />
# Assign '''array '''to a '''scalar variable;''' '''$arrayLength = @array'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
gedit arrayLength.pl & >> Enter<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an illustration of '''array''' length using a sample program.<br />
<br />
<br />
Switch to the '''terminal''' and type -<br />
<br />
'''gedit arrayLength dot pl space ampersand'''<br />
<br />
<br />
Press '''Enter.'''<br />
<br />
<br />
This will open '''arrayLength Perl '''script in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3); '''<br />
<br />
'''$length = @myArray; '''<br />
<br />
<br />
'''print "Length of an array using index: ", $#myArray + 1, "\n"; '''<br />
<br />
<br />
'''print "Length of an array using scalar variable: $length\n"; '''<br />
<br />
<br />
'''print "Length of an array using scalar function: ", scalar(@myArray), "\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code, as shown on the screen-<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
Here we have declared & defined an '''array''' which contains '''5 elements'''. <br />
<br />
<br />
So, output will display '''5'''.<br />
<br />
<br />
Highlighted here are various ways to find the length of an '''array '''in '''Perl.'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight comma in print statement<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Please note,'''<br />
<br />
We have '''concatenated''' the output in the '''print''' '''statement''' using '''comma.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us execute the script. <br />
<br />
Switch to '''terminal''' and type-<br />
<br />
<br />
'''perl arrayLength dot pl '''and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Length of an array using index: 5 <br />
<br />
Length of an array using scalar variable: 5 <br />
<br />
Length of an array using scalar function: 5<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output is as shown on the '''terminal.'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, let us understand''' '''how to access''' '''individual '''elements '''in an '''array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Indexing '''is used to access '''elements '''of an '''array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example for accessing '''elements '''of an '''array '''at -<br />
<br />
# First Position<br />
# Last Position<br />
# Any position<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the''' terminal '''and type -<br />
<br />
<br />
'''gedit perlArray dot pl space ampersand '''<br />
<br />
and press '''Enter.'''<br />
<br />
<br />
This will open '''perlArray dot pl''' script in '''gedit'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3); '''<br />
<br />
<br />
'''<nowiki># Access the first element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki># Access 3rd element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "3rd Element of myArray is: ", $myArray[2], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki># Access last element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
<br />
'''<nowiki>$myArray[0]</nowiki>'''<br />
<br />
<br />
'''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Please note- <br />
<br />
# '''myArray''' is declared with '''@ (at the rate)''' sign.<br />
# But, to access an '''array element '''we need to use '''$ (dollar) '''sign.<br />
# To access the '''element '''at 3rd position, we need to pass '''index '''as 2<br />
<br />
i.e position of '''element''' '''minus '''1.<br />
<br />
<br />
Here, to access the first''' element '''of '''myArray '''<br />
<br />
zero is passed as '''index.'''<br />
<br />
<br />
To access the last element of '''myArray '''we have passed the last '''index '''to '''myArray.'''<br />
<br />
<br />
Recall, we had learnt about this earlier. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and execute''' '''the '''Perl script''' as''' -'''<br />
<br />
<br />
'''perl perlArray dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output will be as shown on the terminal <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us understand, how to '''loop over''' each''' element '''of an '''array.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are two ways of '''looping over''' an '''array-'''<br />
<br />
# Using '''for loop'''<br />
# Using '''foreach loop'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let's learn how to use these '''loops '''to iterate over an '''array '''using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| For this, switch to the '''terminal''' and type<br />
<br />
'''gedit loopingOverArray dot pl space ampersand'''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3); '''<br />
<br />
<br />
'''<nowiki># Iterate over an array using for loop </nowiki>'''<br />
<br />
'''<nowiki>for ($i = 0; $i <= $#myArray; $i++)</nowiki> { '''<br />
<br />
'''<nowiki># Do the operation on individual element </nowiki>'''<br />
<br />
'''<nowiki>print "Printing element using for loop: ", $myArray[$i], "\n"; </nowiki>'''<br />
<br />
'''} '''<br />
<br />
<br />
'''<nowiki># Used to separate the output on terminal </nowiki>'''<br />
<br />
'''print "============================================\n"; '''<br />
<br />
<br />
'''<nowiki># Iterate over an array using foreach loop </nowiki>'''<br />
<br />
'''foreach $element (@myArray) { '''<br />
<br />
'''<nowiki># Do the operation on individual element </nowiki>'''<br />
<br />
'''print "Printing element using foreach loop: $element\n"; '''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open '''loopingOverArray Perl script '''in '''gedit.'''<br />
<br />
<br />
Type the piece of code as shown on the screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are printing each '''element''' of the '''array''' by iterating the '''index'''.<br />
<br />
<br />
The '''for loop '''will execute till the value of '''i''' variable reaches the last '''index '''of the''' array'''. <br />
<br />
<br />
Here, '''foreach loop '''will be executed for each '''element '''of the''' array.'''<br />
<br />
<br />
Once the '''array '''reaches its last '''element''', it will exit the '''foreach loop'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Please Note:'''<br />
<br />
* If you are not aware of '''for''' and''' foreach loops''',<br />
* please go through the relevant tutorials<br />
* on the spoken tutorial website. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl scalars.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and execute''' '''the '''Perl script''' as''' -'''<br />
<br />
<br />
'''perl loopingOverArray dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Printing element using for loop: 1 '''<br />
<br />
'''Printing element using for loop: 2 '''<br />
<br />
'''Printing element using for loop: 3 '''<br />
<br />
'''<nowiki>==============================</nowiki>'''<br />
<br />
'''Printing element using foreach loop: 1 '''<br />
<br />
'''Printing element using foreach loop: 2 '''<br />
<br />
'''Printing element using foreach loop: 3 '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In '''Perl''', we can declare a '''sequential array''' as-<br />
<br />
* '''@alphaArray = (a..d); '''<br />
* i.e '''alphaArray''' will contain '''elements''' 'a', 'b', 'c' and 'd'<br />
* Similarly <br />
* '''@numericArray = (1..5);''' is same as '''@numericArray = (1, 2, ,3, 4, 5);'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Perl''' also provides '''array slicing. '''<br />
<br />
<br />
This is nothing but extracting part of an '''array''' and dumping it into a new '''array'''.<br />
<br />
<br />
'''@array = (19, 23, 56, 45, 87, 89); '''<br />
<br />
'''<nowiki>@newArray = @myarray[1, 4];</nowiki>'''<br />
<br />
<br />
After '''slicing''', '''newArray''' will look like this -<br />
<br />
'''@newArray = (23, 87);'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
<br />
In this tutorial, we have learnt to-<br />
<br />
* Find''' index '''of an '''array'''<br />
* Find '''length '''of an '''array'''<br />
* Access '''elements '''of an '''array'''<br />
* '''Loop over '''an '''array'''<br />
* '''Sequenial Array'''<br />
* '''Array Slicing'''<br />
<br />
using sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
# Declare an '''array''' of rainbow colors<br />
# Print 4th '''element''' of this''' array '''<br />
# Print '''Length '''and last '''index '''of this '''array'''<br />
# '''Loop over''' each '''element''' of the '''array''' using '''for''' & '''foreach loops'''<br />
# Declare array as <br/> @myArray = (1..9); and then<br/> Create an array of odd numbers from above array using array slicing. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Data-Structures/EnglishPERL/C2/Data-Structures/English2013-09-05T04:15:29Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''Data structures in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Data structures in perl video tutorial. {| style="border-spacing:0;" | style="bo…'</p>
<hr />
<div>'''Title Of Script: '''Data structures in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Data structures in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Data Structures''' in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about;<br />
<br />
'''Data Structures''' available in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Perl has 3 types of data structure -<br />
<br />
1. '''Scalar'''<br />
<br />
2. '''Array'''<br />
<br />
3. '''Hash'''. Also, called as '''Associative Array'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Scalar:'''<br />
<br />
# This type of '''data structure''' holds a value of any '''data type'''. <br />
# The '''data type''' can be '''string, number, double''' etc.<br />
# It can also hold the reference to an array or reference to a '''hash.''' <br />
<br />
'''Note:''' reference in '''Perl''' will be covered in subsequent tutorial.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''Declaring scalar data structure;'''<br />
<br />
'''$count = 12;'''<br />
<br />
'''$string = 'I am scalar of type string';'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Scalar''' type of''' data structure''' is as simple as declaring the '''variable'''. <br />
<br />
<br />
'''$count = 12; '''<br />
<br />
<br />
'''$string = 'I am scalar of type string';'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We can perform the following operations on '''scalar;'''<br />
<br />
# Assign a value to it<br />
# Assign one '''scalar''' to another<br />
# '''Arithmetic operations''' on '''number''' type of '''scalars''' like add, subtract etc<br />
# '''string operations''' on '''string''' '''scalar''' like '''concatenation,''' '''substr''' etc<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of s'''calar data structure'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal >> type gedit scalars dot pl & >> Enter.<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''terminal''' and type <br />
<br />
'''gedit scalars dot pl &'''<br />
<br />
and Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
<nowiki>#!/usr/bin/perl </nowiki><br />
<br />
<br />
<nowiki># Following are the various ways of declaring sclar </nowiki><br />
<br />
$count = 12; <br />
<br />
$string = 'I am scalar of type string'; <br />
<br />
<br />
<nowiki>=head </nowiki><br />
<br />
Few of the arithmatic operations that <br />
<br />
can be performed on the sclars <br />
<br />
<nowiki>=cut </nowiki><br />
<br />
<br />
$newCount = $count; <br />
<br />
print "Original Count: $newCount\n"; <br />
<br />
<br />
$newCount = $newCount + 12; #Can be written as $newCount += 12; <br />
<br />
print "New Count After Addition: $newCount\n"; <br />
<br />
<br />
$newCount = $newCount - 12; #Can be written as $newCount -= 12; <br />
<br />
print "New Count After Substraction: $newCount\n"; <br />
<br />
<br />
<nowiki>=head </nowiki><br />
<br />
Few of the string operations that <br />
<br />
can be performed on the sclars <br />
<br />
<nowiki>=cut </nowiki><br />
<br />
<br />
$string = $string. ' in Perl'; #can be written as $string .= ' in Perl'; <br />
<br />
print "Original String: $string\n"; <br />
<br />
<br />
$newString = substr($string, 0, 11); <br />
<br />
print "New String: $newString\n";<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''scalars dot pl file in gedit'''.<br />
<br />
Type the code as displayed on the screen.<br />
<br />
<br />
This is the '''declaration''' and '''assignment''' to the '''scalar.'''<br />
<br />
<br />
These are few '''arithmetic operations''' that can be performed on '''number''' type of '''scalar''' <br />
<br />
<br />
<nowiki><pause></nowiki><br />
<br />
<br />
These are '''string operations '''that can be performed on '''String''' type of '''scalar'''.<br />
<br />
<br />
<nowiki><pause></nowiki><br />
<br />
<br />
'''substr''' is the '''PERL function''' which provides part of the '''string''' as output. <br />
<br />
<br />
Here '''index 0 '''specifies start of a string, i.e. from where we want to start extraction of the string.<br />
<br />
<br />
And 11 is the '''offset '''upto where we want the string to be in the output.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl + s''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl scalars.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' as''' '''<br />
<br />
<br />
'''perl scalars dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Original Count: 12 '''<br />
<br />
'''New Count After Addition: 24 '''<br />
<br />
'''New Count After Substraction: 12 '''<br />
<br />
'''String: I am scalar of type string in Perl '''<br />
<br />
'''New String: I am scalar '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output shown on '''terminal'''<nowiki> is as highlighted. <pause></nowiki><br />
<br />
<br />
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
'''Original Count: 12 '''<br />
<br />
'''New Count After Addition: 24 '''<br />
<br />
'''New Count After Substraction: 12 '''<br />
<br />
'''String: I am scalar of type string in Perl '''<br />
<br />
'''New String: I am scalar '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at '''array data structure''' in perl.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Array''':<br />
<br />
# It is a list of '''elements'''.<br />
# '''Elements''' can be '''string, number '''etc.<br />
# It has an '''index''', which is used for performing various operations on the '''array'''.<br />
# '''Index''' starts with zero.<br />
# Unlike other programming languages, there is no need to declare an '''array''' or its length before using it in '''Perl'''.<br />
# '''Perl array''', stretches or shrinks as per the '''elements''' added or removed from it<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
Highlight @variableName = (element 1, element 2, ..., element N);<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax to write an '''array''' is;<br />
<br />
<br />
'''at the rate variable name space equal to space open bracket list of elements separated with comma close bracket semicolon'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''array data structure'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal >> type <br />
<br />
gedit perlArray.pl & >> Enter.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''terminal''' and type <br />
<br />
'''gedit perlArray dot pl &'''<br />
<br />
and Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
<nowiki>#!/usr/bin/perl</nowiki><br />
<br />
<br />
<nowiki>#Number array</nowiki><br />
<br />
@numArray = (98, 99, 92, 97);<br />
<br />
<br />
<nowiki>#String array</nowiki><br />
<br />
@strArray = ('Perl', 'String', 'Array');<br />
<br />
<br />
<nowiki>#Mix array</nowiki><br />
<br />
@array = (98, 'StringValue', 92);<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''perlArray dot pl file in gedit'''.<br />
<br />
Type the following code as displayed on the screen<br />
<br />
<br />
This is the number array which has elements of number type<br />
<br />
<br />
This is the string array which has elements of string type<br />
<br />
<br />
This array has elements of both number and string type.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
Highlight all 3 types of array<br />
<br />
<br />
print “Number Array: @numArray\n”;<br />
<br />
<br />
print “String Array: @strArray\n”;<br />
<br />
<br />
print “Array: @array\n”;<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This example shows the various types of '''array in Perl'''. <br />
<br />
<br />
Now let us see how to print the '''array'''.<br />
<br />
<br />
Type<br />
<br />
'''print space double quote Number Array colon space at the rate numArray slash n double quote semicolon'''<br />
<br />
'''Press Enter'''<br />
<br />
Similarly print other 2 arrays as<br />
<br />
<br />
'''print “String Array: @strArray\n”;'''<br />
<br />
<br />
'''print “Array: @array\n”;'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl + s''' to save the file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' as''' '''<br />
<br />
<br />
'''perl perlArray dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Number Array: 98 99 92 97 '''<br />
<br />
'''String Array: Perl String Array '''<br />
<br />
'''Array: 98 StringValue 92 '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki> <pause></nowiki><br />
<br />
<br />
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
'''Number Array: 98 99 92 97 '''<br />
<br />
'''String Array: Perl String Array '''<br />
<br />
'''Array: 98 StringValue 92 '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at '''Hash data structure''' in '''Perl'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Hash''':<br />
<br />
# '''Hash''' is alternatively called as''' Associative array. '''<br />
# It is a '''Key Value pair data structure'''.<br />
# '''Key in hash''' is unique. <br />
# If the same '''key''' is added again, then the '''value''' of that '''key''' will be overridden by the latest '''value''' assigned to the '''key.'''<br />
# '''Value''' can be duplicate.<br />
# It also holds '''value''' of any '''data type'''.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
Highlight <br />
<br />
%variableName = ( <br />
<br />
'Key1' => 'Value1' ,<br />
<br />
'Key2' => 'Value2'<br />
<br />
);<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''hash''' is;<br />
<br />
<br />
'''percentage variable name space equal to space open bracket '''<br />
<br />
'''Press Enter'''<br />
<br />
'''single quote key Name single quote space equal to greater than sign space Value comma'''<br />
<br />
'''Press Enter'''<br />
<br />
'''single quote key Name single quote space equal to greater than sign space Value'''<br />
<br />
'''Press Enter'''<br />
<br />
'''close bracket semicolon'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''hash data structure'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''terminal''' and type <br />
<br />
'''gedit perlHash dot pl &'''<br />
<br />
and Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
<nowiki>#!/usr/bin/perl</nowiki><br />
<br />
<br />
use Data::Dumper;<br />
<br />
<br />
<nowiki>#Hash of marks obtain in a subject</nowiki><br />
<br />
%hash = ( <br />
<br />
'Subject' => 'Math', <br />
<br />
'Marks' => 98 <br />
<br />
);<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''perlHash dot pl file in gedit'''.<br />
<br />
Type the following code as displayed on the screen;<br />
<br />
<br />
This '''hash''' indicates the marks obtained in a subject.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
Highlight <br />
<br />
print "Hash: ", Dumper \%hash;<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This example, shows the use of '''hash'''. <br />
<br />
<br />
Now let us see how to print the '''hash'''.<br />
<br />
<br />
For now, just note the way I have printed the '''hash'''. <br />
<br />
<br />
Detailed explanation will be given in a subsequent tutorial. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl + s '''to save the file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlHash.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and execute''' '''the '''Perl script''' as''' '''<br />
<br />
<br />
'''perl perlHash dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Hash: '''<br />
<br />
'''$VAR1 = { '''<br />
<br />
''''Subject' => 'Math', '''<br />
<br />
''''Marks' => 98 '''<br />
<br />
'''};'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
''<nowiki><< I Wiil just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
'''Hash: '''<br />
<br />
'''$VAR1 = { '''<br />
<br />
''''Subject' => 'Math', '''<br />
<br />
''''Marks' => 98 '''<br />
<br />
'''}; '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
* '''scalar'''<br />
* '''Array and'''<br />
* '''Hash Data Structure''' in '''Perl''' <br />
* using sample programs.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
# Declare '''scalar''' '''variable''' <br />
# Assign '''value''' of '''type''' '''float''' to it and then print it.<br />
# Declare and print an '''array''' of colors 'Red', 'Yellow' and 'Green'.<br />
# Declare and print a '''hash''' of Employee Name and their department. <br />
<br />
'''Hint:''' <br />
<br />
'Employee' => 'John',<br />
<br />
'Department' => 'Engineering' <br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hyphen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Array-functions/EnglishPERL/C2/Array-functions/English2013-08-29T07:11:41Z<p>AmolBrahmankar: </p>
<hr />
<div>'''Title Of Script: '''Array functions in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Array functions in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Array Functions''' in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about '''Array functions''' in '''Perl''', like<br />
<br />
# push<br />
# pop<br />
# shift <br />
# unshift<br />
# split<br />
# splice<br />
# join<br />
# sort<br />
# qw<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables, Data Structures & Arrays '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Perl provides certain inbuilt '''functions'''.<br />
<br />
<br />
These '''functions''' can perform various operations on an '''Array'''. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us first learn how to add and remove''' elements''' from last position of an '''Array'''. <br />
<br />
<br />
This can be done by using;<br />
<br />
# '''push''' '''function''' which adds an '''element''' at the last position of an '''Array''' <br />
# '''pop''' '''function''' which removes an '''element''' from the last position of an '''Array'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand '''push''' and '''pop''' '''functions''' by using a sample program.<br />
<br />
<br />
Open the '''terminal''' and type '''gedit perlArray dot pl space ampersand '''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3); '''<br />
<br />
<br />
'''print "Original array: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Insert an element at the last position of an array </nowiki>'''<br />
<br />
'''push (@myArray, 4); '''<br />
<br />
<br />
'''print "New array after pushing 4 into an array: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Remove an element from the last position of an array </nowiki>'''<br />
<br />
'''pop (@myArray); '''<br />
<br />
'''print "New array after popping element: @myArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open '''perlArray dot pl''' file in '''gedit.'''<br />
<br />
<br />
Type the code as shown on the screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we have defined an '''Array '''of length 3.<br />
<br />
<br />
'''push''' '''function''' will insert an '''element''' at the last position of this '''Array''' i.e after 3<nowiki>;</nowiki><br />
<br />
<br />
wheras, '''pop function '''will remove an '''element''' from the last position of the '''Array'''.<br />
<br />
<br />
In our case, '''4''' will be removed from the '''Array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''push function''' takes 2 arguments-<br />
<br />
<br />
# '''1st argument '''to the '''push function, '''is the '''Array''' in which to add an''' element.'''<br />
# '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''pop function '''is as follows -<br />
<br />
* '''pop function''' takes only one''' argument'''<br />
* It is the '''Array '''from which an '''element '''needs to be removed.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:'''<br />
<br />
Both these functions work at '''last position''' of an '''Array.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''element''' removed by '''pop''' '''function''' can be collected into another '''variable.''' <br />
<br />
<br />
The syntax for this is -<br />
<br />
'''$variable = pop(@myArray)'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now switch to the '''terminal '''and '''execute '''the '''Perl script'''.''' '''<br />
<br />
<br />
Type '''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
Original array: 1 2 3 <br />
<br />
New array after pushing 4 into an array: 1 2 3 4 <br />
<br />
New array after popping element: 1 2 3<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output is shown on the '''terminal.'''<br />
<br />
<br />
'''Original array '''<br />
<br />
'''New array after pushing 4 into an array'''<br />
<br />
'''New array after popping element'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us see how to add/remove an '''element '''from the '''1st position''' of an '''Array.'''<br />
<br />
<br />
This can be achieved using-<br />
<br />
# '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position<br />
# '''shift function''' - which removes the<br/> first''' element '''from an '''Array.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample program.<br />
<br />
<br />
I will open '''perlArray dot pl '''file which I have already created.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki># Insert an element at the first position of an array </nowiki>'''<br />
<br />
'''unshift (@myArray, 0); '''<br />
<br />
<br />
'''print "Array after adding 0 into an array at 1st position: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Remove an element from the first position of an array </nowiki>'''<br />
<br />
'''shift (@myArray); '''<br />
<br />
<br />
'''print "Array after removing element from 1st position: @myArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown on the screen.<br />
<br />
<br />
'''unshift '''function will insert''' '''an '''element '''at the''' '''first''' '''position <br />
<br />
i.e before 1<br />
<br />
<br />
'''shift '''function will remove''' '''an''' element '''from the''' '''first position. <br />
<br />
<br />
In our case,''' '''zero''' '''will be removed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''unshift function''' takes 2 '''arguments''' -<br />
<br />
# '''1st argument '''is the''' Array''' in which to add an '''element'''<br />
# '''2nd argument '''is the '''element '''to be added into the '''Array'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''shift function''' takes only one '''argument - '''<br />
<br />
* This is the '''Array '''from which the '''element '''needs to be removed.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:'''<br />
<br />
Both these functions works at '''first position '''of an array.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We can collect the element removed by '''shift function''' into some variable.<br />
<br />
<br />
This syntax for this is - <br />
<br />
'''$variable = shift(@myArray)'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl''' '''script.''' <br />
<br />
<br />
Type '''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output on the terminal'''<br />
<br />
<br />
'''Array after adding 0 into an array at 1st position: 0 1 2 3 '''<br />
<br />
'''Array after removing element from 1st position: 1 2 3 '''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output displayed on the '''terminal''' is as highlighted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us see how to remove an '''element''' from a specified position of an '''Array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''splice function '''removes an '''element''' from a specified position of an '''Array'''.<br />
* The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample program.<br />
<br />
<br />
Go to the '''perlArray dot pl''' file which we created earlier.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki># Removes element(s) from the specified position </nowiki>'''<br />
<br />
'''@myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9); '''<br />
<br />
'''@splicedArray = splice(@myNewArray, 4, 2); '''<br />
<br />
<br />
'''print "Spliced Array: @splicedArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the piece of code shown on the screen.<br />
<br />
<br />
We need to provide<br />
<br />
* the '''index''' from where we want to remove the '''elements''' and<br />
* the '''offset''' upto which we want to remove the '''elements'''<br />
<br />
In our case, the '''elements '''5 and 6 will be removed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing -''' '''<br />
<br />
<br />
'''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output on the terminal'''<br />
<br />
<br />
'''Spliced Array: 5 6'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output displayed on the '''terminal''' is as highlighted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at few other '''inbuilt functions '''of '''Arrays.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''split function '''is used to divide a '''string '''at a specified '''delimiter'''. <br />
* The return value of this '''function '''is an '''Array'''.<br />
* The '''elements '''of this '''Array '''are the divided portions of the '''string'''.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''.<br />
* It returns a string of joined '''elements.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''sort function '''sorts an '''Array '''in alphabetical/numerical order.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''qw function '''returns an '''Array '''of words, separated by a '''white space.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us understand all these functions using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
'''gedit arrayFunctions.pl &'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''terminal''' and type <br />
<br />
<br />
'''gedit arrayFunctions dot pl space ampersand'''<br />
<br />
<br />
This will open '''arrayFunctions dot pl''' file in '''gedit'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
'''use Data::Dumper; '''<br />
<br />
<br />
'''$string = "Illustration Of Array Functions"; '''<br />
<br />
'''@newArray = split (/ /, $string); '''<br />
<br />
<br />
'''print "Declared String: $string\n\n"; '''<br />
<br />
'''print "Array after split: \n"; '''<br />
<br />
'''print Dumper \@newArray; '''<br />
<br />
'''print "\n"; '''<br />
<br />
<br />
'''$joinedString = join(", ", @newArray); '''<br />
<br />
'''print "String After Join: $joinedString\n\n"; '''<br />
<br />
<br />
'''@sortedArray = sort(@newArray); '''<br />
<br />
'''print "Array after sorting: @sortedArray\n\n";'''<br />
<br />
<br />
'''@qwArray = qw (Hello Perl Array); '''<br />
<br />
'''print "Array after qw: \n"; '''<br />
<br />
'''print Dumper \@qwArray; '''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
In this case, each word of the variable string will become an '''element '''of an '''Array.'''<br />
<br />
<br />
Here, each '''element''' of '''newArray '''will be joined by '''comma.'''<br />
<br />
<br />
'''sort function''' will sort the '''elements''' of '''newArray''' in alphabetical order.<br />
<br />
<br />
'''qw function''' creates an '''Array''' of words separated by '''space.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand each function.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''split function '''takes two '''arguments.'''<br />
<br />
* '''1st '''is the '''delimiter '''by which the '''string '''needs to be split<br />
* '''2nd '''is the '''string '''which needs to be split.<br />
<br />
'''Delimiters''' can be specified in f'''orward slash, single or double quotes.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''join function '''takes 2 '''arguments.'''<br />
<br />
# '''1st '''is the '''delimiter '''by which the '''Array elements '''needs to be joined'''.'''<br />
# '''2nd '''is the '''Array.'''<br />
<br />
'''Delimiters''' can be specified in '''single or double'''<br />
<br />
'''quotes.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''sort function '''takes a single''' argument,''' which is the '''Array '''that needs to be sorted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''qw function '''returns an '''Array '''of words, separated by '''space.'''<br />
<br />
It is not necessary to specify the word in''' quotes, '''if written using '''qw. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl arrayFunctions.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing - <br />
<br />
<br />
'''perl arrayFunctions dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Declared String: Illustration Of Array Functions '''<br />
<br />
<br />
'''Array after split: '''<br />
<br />
'''<nowiki>$VAR1 = [ </nowiki>'''<br />
<br />
''''Illustration', '''<br />
<br />
''''Of', '''<br />
<br />
''''Array', '''<br />
<br />
''''Functions' '''<br />
<br />
''']; '''<br />
<br />
<br />
'''String After Join: Illustration, Of, Array, Functions '''<br />
<br />
<br />
'''Array after sorting: Array Functions Illustration Of '''<br />
<br />
<br />
'''Array after qw: '''<br />
<br />
'''<nowiki>$VAR1 = [ </nowiki>'''<br />
<br />
''''Hello', '''<br />
<br />
''''Perl', '''<br />
<br />
''''Array' '''<br />
<br />
'''];'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt to -<br />
<br />
* add/remove '''elements '''from an '''Array'''<br />
* basic functions which can be performed on '''Array'''<br />
* using sample programs<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
# '[http://script.spoken-tutorial.org/index.php/Perl script.spoken-tutorial.org/index.php/Perl]' split the above '''string''' at '/' '''delimiter'''<br />
# Add '''https:// '''at the start of an newly created '''Array'''<br />
# Remove '''element “Perl” '''from an '''Array'''<br />
# Declare number '''Array '''and sort it<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Arrays/EnglishPERL/C2/Arrays/English2013-08-27T11:07:25Z<p>AmolBrahmankar: Created page with ''''Title Of Script: Arrays in Perl''' '''Author:''' Amol Brahmankar '''Keywords: '''Array in perl video tutorial. {| style="border-spacing:0;" | style="border-top:0.05pt sol…'</p>
<hr />
<div>'''Title Of Script: Arrays in Perl'''<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords: '''Array in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arrays''' in '''Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about;<br />
<br />
* '''Index''' of an '''array'''<br />
* Length of an''' array'''<br />
* Accessing '''elements''' of an '''array'''<br />
* Looping over an '''array'''<br />
* '''Sequential Array'''<br />
* '''Array Slicing'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables & Data Structures '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''Array''' is a simple '''data structure''' which contains '''elements''' of any '''data type'''.<br />
* '''Array index''' starts from zero always.<br />
* In '''Perl''', it is not necessary to declare the length of an '''array'''.<br />
* '''Array '''length expands/shrinks as and when '''elements''' are added/removed from it.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax for declaring an array is -<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The last '''index''' of an '''array''' can be found with this command -<br />
<br />
'''$#myArray '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample '''program'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
gedit arrayIndex.pl & >> Enter<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the terminal and type <br />
<br />
'''gedit arrayIndex dot pl space ampersand '''<br />
<br />
and press '''Enter.'''<br />
<br />
<br />
This will open '''arrayIndex dot pl '''file in '''geditor'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
'''print “Last index of myArray is: $#myArray\n”;'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the piece of code that is displayed on screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''@myArray = (1, 2, 3, 'abc', 10.3);'''<br />
<br />
<br />
'''$#myArray'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here we have declared & defined an '''array''' which contains '''5 elements'''. <br />
<br />
<br />
As '''array index '''starts from zero, the last '''index''' value will be '''4''' <br />
<br />
<br />
i.e number of elements, which is 5, minus 1.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl+S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
Type<br />
<br />
'''perl arrayIndex.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to the '''terminal '''and '''execute '''the '''Perl''' script. Type -''' '''<br />
<br />
<br />
'''perl arrayIndex dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
Last index of myArray is: 4<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| <nowiki>The output shown on terminal is <<pause>></nowiki><br />
<br />
<br />
'''Last index of myArray is: 4'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us '''see how to get length of an array '''in perl.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are many ways by which we can find the length of an '''array. '''<br />
<br />
<br />
These are as follows- <br />
<br />
# Index of an '''array''' + 1; '''$#array + 1'''<br />
# Using '''PERL '''inbuilt '''scalar function; scalar(@array)'''<br />
# Assign '''array '''to a '''scalar variable;''' '''$arrayLength = @array'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
gedit arrayLength.pl & >> Enter<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an illustration of '''array''' length using a sample program.<br />
<br />
<br />
Switch to the '''terminal''' and type -<br />
<br />
'''gedit arrayLength dot pl space ampersand'''<br />
<br />
<br />
Press '''Enter.'''<br />
<br />
<br />
This will open '''arrayLength Perl '''script in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3); '''<br />
<br />
'''$length = @myArray; '''<br />
<br />
<br />
'''print "Length of an array using index: ", $#myArray + 1, "\n"; '''<br />
<br />
<br />
'''print "Length of an array using scalar variable: $length\n"; '''<br />
<br />
<br />
'''print "Length of an array using scalar function: ", scalar(@myArray), "\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code, as shown on the screen-<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
Here we have declared & defined an '''array''' which contains '''5 elements'''. <br />
<br />
<br />
So, output will display '''5'''.<br />
<br />
<br />
Highlighted here are various ways to find the length of an '''array '''in '''Perl.'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight comma in print statement<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Please note,'''<br />
<br />
We have '''concatenated''' the output in the '''print''' '''statement''' using '''comma.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us execute the script. <br />
<br />
Switch to '''terminal''' and type-<br />
<br />
<br />
'''perl arrayLength dot pl '''and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Length of an array using index: 5 <br />
<br />
Length of an array using scalar variable: 5 <br />
<br />
Length of an array using scalar function: 5<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output is as shown on the '''terminal.'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, let us understand''' '''how to access''' '''individual '''elements '''in an '''array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Indexing '''is used to access '''elements '''of an '''array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us look at an example for accessing '''elements '''of an '''array '''at -<br />
<br />
# First Position<br />
# Last Position<br />
# Any position<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the''' terminal '''and type -<br />
<br />
<br />
'''gedit perlArray dot pl space ampersand '''<br />
<br />
and press '''Enter.'''<br />
<br />
<br />
This will open '''perlArray dot pl''' script in '''gedit'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3, 'abc', 10.3); '''<br />
<br />
<br />
'''<nowiki># Access the first element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki># Access 3rd element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "3rd Element of myArray is: ", $myArray[2], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki># Access last element of an array </nowiki>'''<br />
<br />
'''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
<br />
'''<nowiki>$myArray[0]</nowiki>'''<br />
<br />
<br />
'''<nowiki>print "First element of myArray is: ", $myArray[0], "\n"; </nowiki>'''<br />
<br />
<br />
'''<nowiki>print "Last element of myArray is: ", $myArray[$#myArray], "\n";</nowiki>'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Please note- <br />
<br />
# '''myArray''' is declared with '''@ (at the rate)''' sign.<br />
# But, to access an '''array element '''we need to use '''$ (dollar) '''sign.<br />
# To access the '''element '''at 3rd position, we need to pass '''index '''as 2<br />
<br />
i.e position of '''element''' '''minus '''1.<br />
<br />
<br />
Here, to access the first''' element '''of '''myArray '''<br />
<br />
zero is passed as '''index.'''<br />
<br />
<br />
To access the last element of '''myArray '''we have passed the last '''index '''to '''myArray.'''<br />
<br />
<br />
Recall, we had learnt about this earlier. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us understand, how to '''loop over''' each''' element '''of an '''array.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are two ways of '''looping over''' an '''array-'''<br />
<br />
# Using '''for loop'''<br />
# Using '''foreach loop'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let's learn how to use these '''loops '''to iterate over an '''array '''using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| For this, switch to the '''terminal''' and type<br />
<br />
'''gedit loopingOverArray dot pl space ampersand'''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3); '''<br />
<br />
<br />
'''<nowiki># Iterate over an array using for loop </nowiki>'''<br />
<br />
'''<nowiki>for ($i = 0; $i <= $#myArray; $i++)</nowiki> { '''<br />
<br />
'''<nowiki># Do the operation on individual element </nowiki>'''<br />
<br />
'''<nowiki>print "Printing element using for loop: ", $myArray[$i], "\n"; </nowiki>'''<br />
<br />
'''} '''<br />
<br />
<br />
'''<nowiki># Used to separate the output on terminal </nowiki>'''<br />
<br />
'''print "=================================================\n"; '''<br />
<br />
<br />
'''<nowiki># Iterate over an array using foreach loop </nowiki>'''<br />
<br />
'''foreach $element (@myArray) { '''<br />
<br />
'''<nowiki># Do the operation on individual element </nowiki>'''<br />
<br />
'''print "Printing element using foreach loop: $element\n"; '''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open '''loopingOverArray Perl script '''in '''gedit.'''<br />
<br />
<br />
Type the piece of code as shown on the screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we are printing each '''element''' of the '''array''' I have made this modification. Pls verify that this is correct.by iterating the '''index'''.<br />
<br />
<br />
The '''for loop '''will execute till the value of '''i''' variable reaches the last '''index '''of the''' array'''. <br />
<br />
<br />
Here, '''foreach loop '''will be executed for each '''element '''of the''' array.'''<br />
<br />
<br />
Once the '''array '''reaches its last '''element''', it will exit the '''foreach loop'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Please Note:'''<br />
<br />
* If you are not aware of '''for''' and''' foreach loops''',<br />
* please go through the relevant tutorials<br />
* on the spoken tutorial website. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl scalars.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and execute''' '''the '''Perl script''' as''' -'''<br />
<br />
<br />
'''perl loopingOverArray dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Printing element using for loop: 1 '''<br />
<br />
'''Printing element using for loop: 2 '''<br />
<br />
'''Printing element using for loop: 3 '''<br />
<br />
'''<nowiki>==============================</nowiki>'''<br />
<br />
'''Printing element using foreach loop: 1 '''<br />
<br />
'''Printing element using foreach loop: 2 '''<br />
<br />
'''Printing element using foreach loop: 3 '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In '''Perl''', we can declare a '''sequential array''' as-<br />
<br />
* '''@alphaArray = (a..d); '''<br />
* i.e '''alphaArray''' will contain '''elements''' 'a', 'b', 'c' and 'd'<br />
* Similarly <br />
* '''@numericArray = (1..5);''' is same as '''@numericArray = (1, 2, ,3, 4, 5);'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Perl''' also provides '''array slicing. '''<br />
<br />
<br />
This is nothing but extracting part of an '''array''' and dumping it into a new '''array'''.<br />
<br />
<br />
'''@myarray = (19, 23, 56, 45, 87, 89); '''<br />
<br />
'''<nowiki>@mynewArray = @myarray[1, 4];</nowiki>'''<br />
<br />
<br />
After '''slicing''', '''mynewArray''' will look like this -<br />
<br />
'''@mynewArray = (23, 87);'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
<br />
In this tutorial, we have learnt to-<br />
<br />
* Find''' index '''of an '''array'''<br />
* Find '''length '''of an '''array'''<br />
* Access '''elements '''of an '''array'''<br />
* '''Loop over '''an '''array'''<br />
* '''Sequenial Array'''<br />
* '''Array Slicing'''<br />
<br />
using sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
# Declare an '''array''' of rainbow colors<br />
# Print 4th '''element''' of this''' array '''<br />
# Print '''Length '''and last '''index '''of this '''array'''<br />
# '''Loop over''' each '''element''' of the '''array''' using '''for''' & '''foreach loops'''<br />
# Give one point about slicing, too.xxxxxxxxxx<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Array-functions/EnglishPERL/C2/Array-functions/English2013-08-27T11:03:46Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''Array functions in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Array functions in perl video tutorial. {| style="border-spacing:0;" | style="bo…'</p>
<hr />
<div>'''Title Of Script: '''Array functions in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Array functions in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Array Functions''' in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about '''Array functions''' in '''Perl''', like<br />
<br />
# push<br />
# pop<br />
# shift <br />
# unshift<br />
# split<br />
# splice<br />
# join<br />
# sort<br />
# qw<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables, Data Structures & Arrays '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' Comments, loops and conditional statements''' will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Perl provides certain inbuilt '''functions'''.<br />
<br />
<br />
These '''functions''' can perform various operations on an '''Array'''. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us first learn how to add and remove''' elements''' from last position of an '''Array'''. <br />
<br />
<br />
This can be done by using;<br />
<br />
# '''pop''' '''function''' which removes an '''element''' from the last position of an '''Array'''<br />
# '''push''' '''function''' which adds an '''element''' at the last position of an '''Array''' <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand '''push''' and '''pop''' '''functions''' by using a sample program.<br />
<br />
<br />
Open the '''terminal''' and type '''gedit perlArray dot pl space ampersand '''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
<br />
'''@myArray = (1, 2, 3); '''<br />
<br />
<br />
'''print "Original array: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Insert an element at the last position of an array </nowiki>'''<br />
<br />
'''push (@myArray, 4); '''<br />
<br />
<br />
'''print "New array after pushing 4 into an array: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Remove an element from the last position of an array </nowiki>'''<br />
<br />
'''pop (@myArray); '''<br />
<br />
'''print "New array after popping element: @myArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open '''perlArray dot pl''' file in '''gedit.'''<br />
<br />
<br />
Type the code as shown on the screen<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
Here, we have defined an '''Array '''of length 3.<br />
<br />
<br />
'''push''' '''function''' will insert an '''element''' at the last position of this '''Array''' i.e after 3<nowiki>;</nowiki><br />
<br />
<br />
wheras, '''pop function '''will remove an '''element''' from the last position of the '''Array'''.<br />
<br />
<br />
In our case, '''4''' will be removed from the '''Array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let me explain the syntax of '''push function-'''<br />
<br />
<br />
# '''1st argument '''to the '''push function '''is the '''Array''' in which to add an''' element.'''<br />
# '''2nd argument '''is the''' element '''which is''' '''to be '''pushed''' into the '''Array.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''pop function '''is as follows -<br />
<br />
* '''pop function''' takes only one''' argument'''<br />
* It is the '''Arrayname '''from which an '''element '''needs to be removed.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:'''<br />
<br />
Both these functions works at '''last position''' of an '''Array.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''element''' removed by '''pop''' '''function''' can be collected into another '''variable.''' <br />
<br />
<br />
The syntax for this is -<br />
<br />
'''$variable = pop(@myArray)'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now switch to the '''terminal '''and '''execute '''the '''Perl script'''.''' '''<br />
<br />
<br />
Type '''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
Original array: 1 2 3 <br />
<br />
New array after pushing 4 into an array: 1 2 3 4 <br />
<br />
New array after popping element: 1 2 3<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output is shown on the '''terminal.'''<br />
<br />
<br />
'''Original array '''<br />
<br />
'''New array after pushing 4 into an array'''<br />
<br />
'''New array after popping element'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us see how to add/remove an '''element '''from the '''1st position''' of an '''Array.'''<br />
<br />
<br />
This can be achieved using-<br />
<br />
# '''unshift function - '''which adds an''' element '''to an '''Array''' at the 1st position<br />
# '''shift function''' - which removes the<br/> first''' element '''from an '''Array.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample program.<br />
<br />
<br />
I will open '''perlArray dot pl '''file which I have already created.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki># Insert an element at the first position of an array </nowiki>'''<br />
<br />
'''unshift (@myArray, 0); '''<br />
<br />
<br />
'''print "Array after adding 0 into an array at 1st position: @myArray\n"; '''<br />
<br />
<br />
'''<nowiki># Remove an element from the first position of an array </nowiki>'''<br />
<br />
'''shift (@myArray); '''<br />
<br />
<br />
'''print "Array after removing element from 1st position: @myArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown on the screen.<br />
<br />
<br />
'''unshift '''function will insert''' '''an '''element '''at the''' '''first''' '''position <br />
<br />
i.e before 1<br />
<br />
<br />
'''shift '''function will remove''' '''an''' element '''from the''' '''first position. <br />
<br />
<br />
In our case,''' '''zero''' '''will be removed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''unshift function''' takes 2 '''arguments''' -<br />
<br />
# '''1st argument '''is the''' Array''' in which to add an '''element'''<br />
# '''2nd argument '''is the '''element '''to be added into the '''Array'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''shift function''' takes only one '''argument - '''<br />
<br />
* This is the '''Array '''from which the '''element '''needs to be removed.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:'''<br />
<br />
Both these functions works at '''first position '''of an array.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We can collect the element removed by '''shift function''' into some variable.<br />
<br />
<br />
This syntax for this is - <br />
<br />
'''$variable = shift(@myArray)'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl''' '''script.''' <br />
<br />
<br />
Type '''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output on the terminal'''<br />
<br />
<br />
'''Array after adding 0 into an array at 1st position: 0 1 2 3 '''<br />
<br />
'''Array after removing element from 1st position: 1 2 3 '''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output displayed on the '''terminal''' is as highlighted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us see how to remove an '''element''' from a specified position of an '''Array'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''splice function '''removes an '''element''' from a specified position of an '''Array'''.<br />
* The '''return value '''of this '''function '''is an '''Array '''of removed '''elements'''.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand this using a sample program.<br />
<br />
<br />
Go to the '''perlArray dot pl''' file which we created earlier.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki># Removes element(s) from the specified position </nowiki>'''<br />
<br />
'''@myNewArray = (1, 2, 3, 4, 5, 6, 7, 8, 9); '''<br />
<br />
'''@splicedArray = splice(@myNewArray, 4, 2); '''<br />
<br />
<br />
'''print "Spliced Array: @splicedArray\n";'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the piece of code shown on the screen.<br />
<br />
<br />
We need to provide<br />
<br />
* the '''index''' from where we want to remove the '''elements''' and<br />
* the '''offset''' upto which we want to remove the '''elements'''<br />
<br />
In our case, the '''elements '''5 and 6 will be removed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl perlArray.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing -''' '''<br />
<br />
<br />
'''perl perlArray dot pl '''and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight the output on the terminal'''<br />
<br />
<br />
'''Spliced Array: 5 6'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output displayed on the '''terminal''' is as highlighted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at few other '''inbuilt functions '''of '''Arrays.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''split function '''is used to divide a '''string '''at a specified '''delimiter'''. <br />
* The return value of this '''function '''is an '''Array'''.<br />
* The '''elements '''of this '''Array '''are the divided portions of the '''string'''.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| * '''join function''' joins the '''elements '''of an '''Array, '''using the specified '''delimiter'''.<br />
* It returns a string of joined '''elements.'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''sort function '''sorts an '''Array '''in alphabetical/numerical order.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''qw function '''returns an '''Array '''of words, separated by a '''white space.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us understand all these functions using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
<br />
'''gedit arrayFunctions.pl &'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''terminal''' and type <br />
<br />
<br />
'''gedit arrayFunctions dot pl space ampersand'''<br />
<br />
<br />
This will open '''arrayFunctions dot pl''' file in '''gedit'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl </nowiki>'''<br />
<br />
'''use Data::Dumper; '''<br />
<br />
<br />
'''$string = "Illustration Of Array Functions"; '''<br />
<br />
'''@newArray = split (/ /, $string); '''<br />
<br />
<br />
'''print "Declared String: $string\n\n"; '''<br />
<br />
'''print "Array after split: \n"; '''<br />
<br />
'''print Dumper \@newArray; '''<br />
<br />
'''print "\n"; '''<br />
<br />
<br />
'''$joinedString = join(", ", @newArray); '''<br />
<br />
'''print "String After Join: $joinedString\n\n"; '''<br />
<br />
<br />
'''@sortedArray = sort(@newArray); '''<br />
<br />
'''print "Array after sorting: @sortedArray\n\n";'''<br />
<br />
<br />
'''@qwArray = qw (Hello Perl Array); '''<br />
<br />
'''print "Array after qw: \n"; '''<br />
<br />
'''print Dumper \@qwArray; '''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code as shown.<br />
<br />
<br />
<nowiki><<pause>></nowiki><br />
<br />
<br />
In this case, each word of the variable string will become an '''element '''of an '''Array.'''<br />
<br />
<br />
Here, each '''element''' of '''newArray '''will be joined by '''comma.'''<br />
<br />
<br />
'''sort function''' will sort the '''elements''' of '''newArray''' in alphabetical order.<br />
<br />
<br />
'''qw function''' creates an '''Array''' of words separated by '''space.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand each function.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''split function '''takes two '''arguments.'''<br />
<br />
* '''1st '''is the '''delimiter '''by which the '''string '''needs to be split<br />
* '''2nd '''is the '''string '''which needs to be split.<br />
<br />
'''Delimiters''' can be specified in f'''orward slash, single or double quotes.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''join function '''takes 2 '''arguments.'''<br />
<br />
# '''1st '''is the '''delimiter '''by which the '''Array elements '''needs to be joined'''.'''<br />
# '''2nd '''is the '''Array.'''<br />
<br />
'''Delimiters''' can be specified in '''single or double'''<br />
<br />
'''quotes.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''sort function '''takes a single''' argument,''' which is the '''Array '''that needs to be sorted.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''qw function '''returns an '''Array '''of words, separated by '''space.'''<br />
<br />
It is not necessary to specify the word in''' quotes, '''if written using '''qw. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press ctrl + s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl + S '''to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl arrayFunctions.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and '''execute '''the '''Perl script''' by typing - <br />
<br />
<br />
'''perl arrayFunctions dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Declared String: Illustration Of Array Functions '''<br />
<br />
<br />
'''Array after split: '''<br />
<br />
'''<nowiki>$VAR1 = [ </nowiki>'''<br />
<br />
''''Illustration', '''<br />
<br />
''''Of', '''<br />
<br />
''''Array', '''<br />
<br />
''''Functions' '''<br />
<br />
''']; '''<br />
<br />
<br />
'''String After Join: Illustration, Of, Array, Functions '''<br />
<br />
<br />
'''Array after sorting: Array Functions Illustration Of '''<br />
<br />
<br />
'''Array after qw: '''<br />
<br />
'''<nowiki>$VAR1 = [ </nowiki>'''<br />
<br />
''''Hello', '''<br />
<br />
''''Perl', '''<br />
<br />
''''Array' '''<br />
<br />
'''];'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is displayed on the '''terminal'''<nowiki>. <pause></nowiki><br />
<br />
<br />
''<nowiki><< Will just highlight the output and will not read it while recording >></nowiki>''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt to -<br />
<br />
* add/remove '''elements '''from an '''Array'''<br />
* basic functions which can be performed on '''Array'''<br />
* using sample programs<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
# '[http://script.spoken-tutorial.org/index.php/Perl script.spoken-tutorial.org/index.php/Perl]' split the above '''string''' at '/' '''delimiter'''<br />
# Add '''https:// '''at the start of an newly created '''Array'''<br />
# Remove '''element “Perl” '''from an '''Array'''<br />
# Declare number '''Array '''and sort it<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/More-Conditional-statements/EnglishPERL/C2/More-Conditional-statements/English2013-08-25T07:27:08Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''if-elsif-else, switch statement '''Author:''' Amol Brahmankar '''Keywords:''' Conditional Statements in Perl, if-elsif-else, switch statement in perl vid…'</p>
<hr />
<div>'''Title Of Script: '''if-elsif-else, switch statement<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Conditional Statements in Perl, if-elsif-else, switch statement in perl video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''if-elsif-else '''and''' switch''' conditional statements in '''Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about the<br />
<br />
* '''if-elsif-else '''statement and<br />
* '''switch '''statement in '''Perl'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl'''<br />
<br />
<br />
and knowledge of <br />
<br />
<br />
* '''for, foreach, while '''and '''do-while '''loops and<br />
* '''if '''and '''if-else''' statements <br />
<br />
will be an added advantage'''.'''<br />
<br />
<br />
Please go through relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''If-elsif-else '''statement in '''Perl '''can be used<br />
<br />
* to test multiple conditions and<br />
* When all conditions fail then it executes the default '''else '''block<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''if (condition) {'''<br />
<br />
'''piece of code;'''<br />
<br />
'''} elsif (condition) {'''<br />
<br />
'''another piece of code;'''<br />
<br />
'''} else {'''<br />
<br />
'''code to be executed if both the above '''<br />
<br />
'''conditions are false;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''if-elsif-else '''statement''' '''is as follows <br />
<br />
<br />
'''if space '''open bracket''' condition''' close bracket '''space '''Open curly bracket <br />
<br />
Press '''Enter'''<br />
<br />
piece of code '''semicolon''' <br />
<br />
to be executed when the condition is '''true'''<br />
<br />
Press '''Enter'''<br />
<br />
<br />
Close curly bracket space '''elsif space '''open bracket '''condition '''close bracket '''space''' open curly bracket<br />
<br />
Press '''Enter'''<br />
<br />
another piece of code '''semicolon''' <br />
<br />
to be executed when '''elsif condition''' is '''true'''<br />
<br />
Press '''Enter'''<br />
<br />
close curly bracket '''space else''' '''space '''open curly bracket<br />
<br />
'''Enter'''<br />
<br />
code to be executed when both the above conditions are '''false semicolon'''<br />
<br />
Press '''Enter'''<br />
<br />
close curly bracket''' '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
Highlight '''if (condition)'''<br />
<br />
<br />
Highlight '''elsif (condition)'''<br />
<br />
<br />
Highlight''' else'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| First, the '''if '''condition is checked and executed if the condition is '''true'''.<br />
<br />
<br />
If not, then the '''else if''' condition is checked and executed if''' '''it is '''true'''.<br />
<br />
<br />
Otherwise, the code within '''else '''block is executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''if-elsif-else '''statement.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit conditionalBlocks.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit conditionalBlocks dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''conditionalBlocks'''.'''pl''' in the Titlebar of gedit.<br />
<br />
<br />
Type the piece of code.<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
$language = 'Perl';<br />
<br />
<br />
if ($language eq 'Perl') {<br />
<br />
print “Hi, I am Perl\n”;<br />
<br />
} elsif ($ language eq 'Java') {<br />
<br />
print “Hi, I am Java\n”;<br />
<br />
} else {<br />
<br />
print “I am not a computer language\n”;<br />
<br />
}<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''conditionalBlocks'''.'''pl''' file in '''gedit'''.<br />
<br />
<br />
Type the following piece of code as displayed on the screen.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight variables declaration<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We have assigned the value ''''Perl'''' to variable '''language.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' eq'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Note that, '''eq''' is the '''string''' '''comparison''' operator.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the '''if-elsif-else '''code<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| And then we have the various conditons that we want to check here.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, press '''ctrl+s''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal '''and execute''' '''file directly.''' '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Type <br />
<br />
'''perl conditionalBlocks.pl'''<br />
<br />
'''Don't press Enter.'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type, <br />
<br />
'''perl conditionalBlocks dot pl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note: '''<br />
<br />
# I am skipping the compilation step. It is not a mandatory step for executing '''Perl''' scripts.<br />
# If there is any compilation error, <br />
<br />
* execution will throw an error and <br />
* will stop execution of script<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press''' Enter.'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
Hi, I am Perl<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output shown on the terminal is<br />
<br />
<br />
'''Hi, I am Perl '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at our next case.<br />
<br />
Switch to '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Change '''$language = 'Java';'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Assign ''''Java'''' to variable '''language '''as shown. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl+s''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl conditionalBlocks.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and execute''' '''the''' '''file.<br />
<br />
Type<br />
<br />
'''perl conditionalBlocks dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''Hi, I am Java'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output shown on terminal is<br />
<br />
<br />
'''Hi, I am Java '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Again let us switch back to '''gedit'''. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Change $language = 'English';'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us assign ''''English'''' to the '''language''' variable.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Again, press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl conditionalBlocks.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to the '''terminal '''and execute''' '''the''' '''file.<br />
<br />
<br />
Type<br />
<br />
'''perl conditionalBlocks dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''I am not a computer language'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The output shown on terminal is<br />
<br />
<br />
'''I am not a computer language '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The 3 cases imply that;<br />
<br />
* Only one '''if block''' that satisfies the condition will be executed<br />
* otherwise the default''' else block''' will be executed.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We can have multiple '''elsif''' conditions as per our requirement, like this.<br />
<br />
<nowiki><<Don't read out the remaining part just display>></nowiki><br />
<br />
'''if (condition) { code1; }'''<br />
<br />
'''elsif (condition) { code2; }'''<br />
<br />
'''elsif (condition) { code3; }'''<br />
<br />
'''else { default_code;}'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is an assignment for you -<br />
<br />
<br />
Write an '''if-elsif-else''' statement to print <br />
<br />
* “I am a Science graduate” if '''stream''' is '''science'''<br />
* “I am a Commerce graduate” if '''stream''' is '''commerce'''<br />
* “I am an Arts graduate” if '''stream''' is neither '''science '''or '''commerce'''. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| <nowiki><<Pause>></nowiki><br />
<br />
Now let us learn about the '''switch '''statement.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Till Perl 5.8, there was no '''switch''' statement in Perl. <br />
<br />
<br />
After that, '''Switch module''' was introduced,<br />
<br />
<br />
which provided the functionality of '''switch''' statement.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Remain on previous slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:'''<br />
<br />
'''Modules''' in '''Perl''' will be covered in subsequent tutorials.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| use Switch; <br />
<br />
<br />
switch ($value) {<br />
<br />
case 1 { executes when <br />
<br />
'''$value = 1'''}<br />
<br />
case 'a' { executes when <br />
<br />
'''$value = 'a''''}<br />
<br />
else { executes when '''$value does '''<br />
<br />
'''not match any of the '''<br />
<br />
'''cases'''}<br />
<br />
}<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''switch '''is as follows:<br />
<br />
<br />
'''use Switch semicolon'''<br />
<br />
Press '''Enter''' <br />
<br />
'''switch''' space open bracket '''dollar value''' close bracket space open curly bracket<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''case '''space''' 1 '''space open curly bracket executes when '''dollar''' '''value '''equal to''' 1''' close curly bracket.<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''case '''space single quote''' a '''single quote space open curly bracket executes when '''dollar''' '''value '''equal to single quote '''a '''single quote''' '''close curly bracket<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''else''' space open curly bracket executes when''' dollar value '''does not match any of the cases<br />
<br />
close curly bracket<br />
<br />
Press '''Enter'''<br />
<br />
close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand '''switch''' using a sample program.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit sampleSwitch.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit sampleSwitch dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| use Switch;<br />
<br />
$var = 'Perl'<br />
<br />
<br />
switch ($var) {<br />
<br />
case 'Perl' {print “I am Perl\n”;}<br />
<br />
case 'Java' {print “I am Java\n”;}<br />
<br />
case 'Linux' {prin “I am Linux\n”;}<br />
<br />
else {print “I am not a computer language\n”;}<br />
<br />
}<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, type the sample program as shown on the screen.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Gedit<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us understand how the '''switch''' statement works.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''use Switch'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''use Switch '''statement includes the '''Switch''' module inside the '''Perl '''code.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Note:''' <br />
<br />
We will learn about '''use keyword '''in detail''' '''in subsequent tutorials. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now we'll test the different cases.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''$var = 'Perl''''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We have assigned ''''Perl'''' to the variable '''$var'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''switch ($var) '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The value in variable '''$var '''is checked in the '''switch '''statement. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''case 'Perl' '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In the first case, it matches with the case ''''Perl''''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''{print “I am Perl\n”;}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| So the code written against this '''case''' will be executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press''' ctrl+s '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''<br />
<br />
'''perl sampleSwitch.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to '''terminal''' and execute the script<br />
<br />
<br />
'''perl sampleSwitch.pl'''<br />
<br />
press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Terminal'''<br />
<br />
Highlight the output on '''terminal'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is shown on the '''terminal'''<br />
<br />
'''I am Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch back to '''sampleSwitch.pl''' in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Change''' $var = 'Linux''''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us assign''' 'Linux' '''to the variable''' $var'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight switch ($var) '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Again, the value of variable''' $var '''will be checked in '''switch. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''case 'Linux' '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| It matches with the case ''''Linux''''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''{print “I am Linux\n”;}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| So code written against this case will get executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
'''perl sampleSwitch.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to terminal and execute the script<br />
<br />
<br />
'''perl sampleSwitch.pl'''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
Highlight the output on terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is shown on the terminal<br />
<br />
'''I am Linux'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''gedit'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch to '''sampleSwitch.pl '''in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Change '''$var = '''''''Java'''' >> <br />
<br />
Highlight '''case 'Java' '''<br />
<br />
<br />
<nowiki><Don't execute this part></nowiki><br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Similarly, if variable '''$var '''has value ''''Java'''', then second case will be checked.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Change '''$var = 'English''''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us assign ''''English'''' to the variable '''$var'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight switch ($var) '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Again, the value of variable''' $var '''will be checked in '''switch. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the 3 '''case '''statements >> then point to the '''else '''statement<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| It does not match any of the '''case''' statements.<br />
<br />
<br />
So the '''else''' statement will be executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to Highlight<br />
<br />
'''perl sampleSwitch.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to terminal and execute the script<br />
<br />
<br />
'''perl sampleSwitch.pl'''<br />
<br />
<br />
Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Terminal<br />
<br />
Highlight the output on Highlight<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output is shown on the terminal -<br />
<br />
'''I am not a computer language'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The 3 '''cases''' imply that:<br />
<br />
* The value of the expression decides the '''case '''to be executed<br />
* Only the valid '''case '''will be executed and <br />
* When there is no valid '''case, '''then the default '''else case '''will be executed<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
Highlight '''else case '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| It is not mandatory to write the '''else case'''. <br />
<br />
<br />
In such a scenario, <br />
<br />
* if none of the '''cases''' match <br />
* then there will be no output from the '''switch''' statement. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is another assignment for you -<br />
<br />
* Re-write the previous assignment <br />
* given earlier in this tutorial<br />
* using '''switch''' statement. <br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
* '''if'''-'''elsif-else''' statement and <br />
* '''switch''' statement in '''Perl'''<br />
* using sample programs.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Conditional-statements/EnglishPERL/C2/Conditional-statements/English2013-08-25T07:25:16Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''if, if-else statements in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Conditional Statements in Perl, if statement in perl, if-else statement in p…'</p>
<hr />
<div>'''Title Of Script: '''if, if-else statements in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Conditional Statements in Perl, if statement in perl, if-else statement in perl, video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''if '''and''' if-else''' conditional statements in '''Perl.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about;<br />
<br />
'''if '''statement '''and '''<br />
<br />
'''if-else '''statement in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using <br />
<br />
<br />
'''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl'''<br />
<br />
<br />
Knowledge of''' for, foreach, while '''and '''do-while '''loops in '''Perl '''will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Perl provides the following conditional statements -<br />
<br />
1. if<br />
<br />
2. if-else<br />
<br />
3. if-elsif-else and<br />
<br />
4. switch<br />
<br />
<br />
In this tutorial, we'll cover '''if''' and '''If-else''' statements<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''if '''statement in '''Perl '''can be used<br />
<br />
* to execute a piece of code <br />
* only when a specified condition is satisfied.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''if (condition) {'''<br />
<br />
'''piece of code;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''if '''conditional''' '''statement''' '''is as follows <br />
<br />
<br />
'''if space '''open bracket''' condition''' close bracket '''space '''Open curly bracket <br />
<br />
'''Enter'''<br />
<br />
Piece of code to be executed when the condition is '''true semicolon'''<br />
<br />
'''Enter'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''if (condition)'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The code inside the '''if''' statement will be executed only when the condition is '''true'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''if '''statement.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit conditionalBlocks.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit conditionalBlocks dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''conditionalBlocks'''.'''pl''' in the Titlebar of gedit.<br />
<br />
<br />
Type the piece of code.<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
$count = 5;<br />
<br />
<br />
if ($count == 5) {<br />
<br />
print “I am inside if statement\n”;<br />
<br />
}<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''conditionalBlocks'''.'''pl''' file in '''gedit'''.<br />
<br />
<br />
Type the following piece of code as displayed on the screen.<br />
<br />
<br />
Here we have specified a condition for '''if''' which checks the value of variable '''count'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight =='''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Note the '''equal to equal to''' sign here. This is the '''comparison''' operator.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight if ($count == 5)'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The condition '''$count equal to equal to 5''' is checked against the value of variable '''count'''. <br />
<br />
<br />
When it is equal to '''5''', the code within the '''if''' block will get executed. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then switch to the '''terminal'''.<br />
<br />
<br />
Make sure that you are in the directory in which you have saved your file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''perl -c conditionalBlocks.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following to check for any compilation or syntax error -<br />
<br />
<br />
'''perl hyphen c conditionalBlocks dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal-<br />
<br />
<br />
'''conditionalBlocks.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be displayed on the terminal window<br />
<br />
'''conditionalBlocks.pl syntax OK'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute Perl script<br />
<br />
<br />
'''perl conditionalBlocks.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As there is no compilation or syntax error, we will execute the '''Perl''' script by typing -<br />
<br />
<br />
'''perl conditionalBlocks dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output on the terminal<br />
<br />
<br />
'''I am inside if statement'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be shown on terminal.<br />
<br />
<br />
'''I am inside if statement'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch back to '''gedit'''<br />
<br />
<br />
print “I am inside if statement\n” if ($count == 5);<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Switch back to '''gedit'''<br />
<br />
<br />
Alternately, we can write the above '''if '''statement as-<br />
<br />
<br />
print '''space''' '''double quote '''I am inside if statement''' slash n double quote complete space if open bracket''' dollar count equal to equal to 5 '''close bracket semicolon.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at '''if-else '''statement.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This statement is used when user wants to execute <br />
<br />
* one piece of code when the condition is '''true''' and <br />
* another piece of code when the condition is '''false'''<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
if-else statement syntax is<br />
<br />
<br />
'''if (condition) {'''<br />
<br />
'''piece of code; '''<br />
<br />
'''<nowiki># executes when if condition is true</nowiki>'''<br />
<br />
'''} else {'''<br />
<br />
'''another piece of code;'''<br />
<br />
'''<nowiki># executes when if condition is false</nowiki>'''<br />
<br />
'''} '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax for '''if-else '''condition is as follows -<br />
<br />
<br />
'''if space '''open bracket''' condition''' close bracket space open curly bracket Press '''Enter'''<br />
<br />
<br />
piece of code '''semicolon '''<br />
<br />
to be executed when '''if condition''' is '''true, '''<br />
<br />
Press '''Enter'''<br />
<br />
<br />
close curly bracket space '''else '''space open curly bracket '''Enter'''<br />
<br />
<br />
another piece of code '''semicolon '''<br />
<br />
to be executed when '''if''' '''condition''' is '''false'''<br />
<br />
Press '''Enter '''close curly bracket <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to gedit<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
'''$count = 4;'''<br />
<br />
'''if ($count == 5) {'''<br />
<br />
'''print “I am inside if statement\n”;'''<br />
<br />
'''} else {'''<br />
<br />
'''print “I am inside else statement\n”;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now again, go to the '''conditionalBlocks.pl''' file which we have already created in '''gedit'''.<br />
<br />
<br />
at the end of the''' if '''block type''' space '''<br />
<br />
'''else '''<br />
<br />
'''space open curly bracket '''press '''Enter '''<br />
<br />
<br />
'''print space double quote I am inside else statement slash n close double quote semicolon '''<br />
<br />
<br />
Press '''Enter '''and close the '''curly bracket. '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''$count = 4;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here, '''4''' is assigned to variable count.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight''' if ($count == 5) '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As the value of '''count''' variable does not match '''5''',<br />
<br />
* the code within the '''if''' block will not get evaluated<br />
* instead the code within the '''else '''block will get evaluated.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Press '''Ctrl+s<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now press''' Ctrl+S''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now switch to '''terminal'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''perl -c conditionalBlocks.pl'''<br />
<br />
<br />
and press '''Enter'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then compile the file for any syntax or compilation errors by typing<br />
<br />
<br />
'''perl hyphen c conditionalBlocks dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the below line on '''terminal''' -<br />
<br />
<br />
'''conditionalBlocks.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be displayed on the '''terminal'''<br />
<br />
<br />
'''conditionalBlocks.pl syntax OK'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute Perl script<br />
<br />
<br />
'''perl conditionalBlocks.pl'''<br />
<br />
<br />
and press '''Enter'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As there is no compilation or syntax error, we will now execute the '''Perl''' script.<br />
<br />
<br />
Type<br />
<br />
'''perl conditionalBlocks dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight output on the '''terminal'''<br />
<br />
<br />
'''I am inside else statement '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be shown on '''terminal'''.<br />
<br />
<br />
'''I am inside else statement'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
'''if''' and<br />
<br />
'''if-else''' conditional statements in '''Perl'''<br />
<br />
using sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
<br />
Print “It is an open source language” <br />
<br />
* when the variable declared has value ''''Perl'''' <br />
* otherwise print “It's a proprietary language”<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-05-08T18:16:16Z<p>AmolBrahmankar: /* Basic Level */</p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
<br />
The Spoken Tutorial Effort for '''Perl''' has being contributed by '''Amol Brahmankar''' from Pune supported with domain reviews by '''Namrata Gaikwad''' from Pune.<br />
<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if & if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 5;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>}</nowiki><br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>#executes this piece of code if the above if condition is evaluates to false.</nowiki><br><nowiki>Another piece of code;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 4;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am inside else\n";</nowiki><br><nowiki>}</nowiki><br />
# if-elsif-else & switch Statement<br />
#* if-elsif-else statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$language = 'Perl';</nowiki><br><nowiki>if ($language eq 'Perl') {</nowiki><br><nowiki>print "Hi, I am Perl\n";</nowiki><br><nowiki>} elsif ($language eq 'Java') {</nowiki><br><nowiki>print "Hi, I am Java\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am not a computer language\n";</nowiki><br><nowiki>}</nowiki><br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-05-05T10:07:09Z<p>AmolBrahmankar: /* Basic Level */</p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
<br />
The Spoken Tutorial Effort for '''Perl''' has being contributed by '''Amol Brahmankar''' from Pune supported with domain reviews by '''Namrata Gaikwad''' from Pune.<br />
<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if & if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 5;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>}</nowiki><br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>#executes this piece of code if the above if condition is evaluates to false.</nowiki><br><nowiki>Another piece of code;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 4;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am inside else\n";</nowiki><br><nowiki>}</nowiki><br />
# if-elsif-else Statement<br />
#* if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#* Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#* eg:<br><nowiki>$language = 'Perl';</nowiki><br><nowiki>if ($language eq 'Perl') {</nowiki><br><nowiki>print "Hi, I am Perl\n";</nowiki><br><nowiki>} elsif ($language eq 'Java') {</nowiki><br><nowiki>print "Hi, I am Java\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am not a computer language\n";</nowiki><br><nowiki>}</nowiki><br />
# switch Statement<br />
#* switch statement is used to select one of many blocks of code to be executed.<br />
#* There were no Switch/Case in perl prior to 5.8 version. <br />
#* After 5.8 PERL provided Switch module.<br />
#* Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#* eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-05-05T09:59:40Z<p>AmolBrahmankar: /* Basic Level */</p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
<br />
The Spoken Tutorial Effort for '''Perl''' has being contributed by '''Amol Brahmankar''' from Pune supported with domain reviews by '''Namrata Gaikwad''' from Pune.<br />
<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if & if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 5;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>}</nowiki><br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>#executes this piece of code if the above if condition is evaluates to false.</nowiki><br><nowiki>Another piece of code;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 4;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am inside else\n";</nowiki><br><nowiki>}</nowiki><br />
# if-elsif-else & switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$language = 'Perl';</nowiki><br><nowiki>if ($language eq 'Perl') {</nowiki><br><nowiki>print "Hi, I am Perl\n";</nowiki><br><nowiki>} elsif ($language eq 'Java') {</nowiki><br><nowiki>print "Hi, I am Java\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am not a computer language\n";</nowiki><br><nowiki>}</nowiki><br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-03-30T10:19:19Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
<br />
The Spoken Tutorial Effort for '''Perl''' has being contributed by '''Amol Brahmankar''' from Pune supported with domain reviews by '''Namrata Gaikwad''' from Pune.<br />
<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if & if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 5;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>}</nowiki><br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>#executes this piece of code if the above if condition is evaluates to false.</nowiki><br><nowiki>Another piece of code;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$count = 4;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am inside else\n";</nowiki><br><nowiki>}</nowiki><br />
# if-elsif-else & switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/while-do-while-loops/EnglishPERL/C2/while-do-while-loops/English2013-03-23T11:11:47Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''while and do-while loops in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Loops in Perl, while loop in perl, do-while loop in perl, video tutorial. …'</p>
<hr />
<div>'''Title Of Script: '''while and do-while loops in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Loops in Perl, while loop in perl, do-while loop in perl, video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''while and do-while '''loops in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about;<br />
<br />
'''while '''loop in '''Perl'''<br />
<br />
'''do-while '''loop in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl'''<br />
<br />
<br />
Knowledge of '''for '''and '''foreach '''loops in '''Perl '''will be an added advantage'''.'''<br />
<br />
<br />
Please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''while '''loop in '''Perl'''<br />
<br />
<br />
The '''while''' loop executes a block of code while a condition is true.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''while (condition) {'''<br />
<br />
'''Piece of code to be executed while the condition is true'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''while''' loop is as follows -<br />
<br />
<br />
'''while space '''open bracket''' condition''' close bracket<br />
<br />
<br />
Open curly bracket <br />
<br />
<br />
Piece of code to be executed while the condition is true<br />
<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''while (condition)'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| So, what happens if the condition is not satisfied? Then, the '''while''' loop will exit, without executing the code within, even once.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''while''' loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit whileLoop.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit whileLoop dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''whileLoop'''.'''pl''' in the Titlebar of gedit.<br />
<br />
<br />
Type the piece of code.<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''$i = 0;'''<br />
<br />
'''<nowiki>while ($i<=4) {</nowiki>'''<br />
<br />
'''print “Value of i: $i\n”;'''<br />
<br />
'''$i++;'''<br />
<br />
'''}'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''whileLoop'''.'''pl''' file in '''gedit'''.<br />
<br />
<br />
Now Type the following code<br />
<br />
<br />
'''hash exclamation mark slash u s r slash bin slash perl'''<br />
<br />
<br />
Press '''Enter.'''<br />
<br />
<br />
'''dollar i''' is equal to zero '''semicolon'''<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''while''' open bracket dollar i less than or equal to four close bracket space<br />
<br />
<br />
Open curly bracket press enter and type<br />
<br />
<br />
'''print''' space double quote Value of i colon space dollar i '''slash n doule '''quote complete '''semicolon'''<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''dollar i plus plus semicolon'''<br />
<br />
press enter and<br />
<br />
Close the curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight $i=0;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let me explain the '''while''' loop in detail.<br />
<br />
<br />
We have initialized the variable''' i''' to 0. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now we have specified the condition for '''while''' loop as '''$i '''less than or equal to 4. <br />
<br />
If the '''condition''' is true, the code within the '''while''' loop will get executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight print'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This means, first time our '''while''' loop will print ''''Value of i: 0' '''on the '''terminal'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight $i++'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then the '''$i++''' increments the value of variable '''i '''by one.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now again, the loop condition '''<nowiki>$i<=4</nowiki>''' will be evaluated.<br />
<br />
<br />
And the loop will exit once the value of''' i''' becomes 5.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this case, '''while''' loop will get executed for''' i''' equal to 0, 1, 2, 3, 4.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to the '''terminal'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''perl -c whileLoop.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following to check for any compilation or syntax error -<br />
<br />
<br />
'''perl hyphen c whileLoop dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal-<br />
<br />
'''whileLoop.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be displayed on the terminal <br />
<br />
'''whileLoop.pl syntax OK'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute Perl script<br />
<br />
<br />
'''perl whileLoop.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As there are no compilation or syntax error, we will execute the '''Perl''' script by typing -<br />
<br />
<br />
'''perl whileLoop dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output of the perl script on terminal<br />
<br />
'''Value of i: 0'''<br />
<br />
'''Value of i: 1'''<br />
<br />
'''Value of i: 2'''<br />
<br />
'''Value of i: 3'''<br />
<br />
'''Value of i: 4'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be displayed on the '''terminal'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at '''do-while loop'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''do...while''' statement will always execute the piece of code at-least once.<br />
<br />
<br />
It will then check the '''condition''' and repeat the loop while the '''condition''' is true.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''do {'''<br />
<br />
'''Piece of code to be executed while the condition is true'''<br />
<br />
'''} while (condition);'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax for '''do-while '''loop is as follows -<br />
<br />
<br />
'''do '''space<br />
<br />
open curly bracket<br />
<br />
Piece of code to be executed while the '''condition''' is true<br />
<br />
close curly bracket then space<br />
<br />
'''while '''space within brackets '''condition''' and then '''semicolon'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit doWhileLoop.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type;<br />
<br />
<br />
'''gedit doWhileLoop dot pl space ampersand'''<br />
<br />
<br />
and then press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''doWhileLoop'''.'''pl''' in the Titlebar of gedit.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''doWhileLoop'''.'''pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Type the piece of code and '''save''' the file<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''$i = 0;'''<br />
<br />
<br />
'''do {'''<br />
<br />
'''print “Value of i: $i\n”;'''<br />
<br />
'''$i++;'''<br />
<br />
'''<nowiki>} while ($i<=4);</nowiki>'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code -<br />
<br />
<br />
'''hash exclamation mark slash u s r slash bin slash perl'''<br />
<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''dollar i equals to zero semicolon press enter'''<br />
<br />
'''do space'''<br />
<br />
open curly bracket enter type<br />
<br />
'''print space double''' quote''' Value of i colon space dollar i slash n''' double quote complete '''semicolon'''<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''dollar i plus plus semicolon'''<br />
<br />
'''press enter'''<br />
<br />
close curly bracket <br />
<br />
space '''while space '''open bracket '''dollar i less than or equal to four '''<br />
<br />
close bracket '''semicolon'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight $i=0;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is the detail explanation of a '''do-while''' loop.<br />
<br />
We have initialized the variable''' i''' to 0.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight print'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| First time, the '''do-while''' loop will print the output as ''''Value of i colon 0'''' <br />
<br />
<br />
without checking for '''condition'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''Highlight $i++;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''$i++''' will increment the value of variable''' i '''by one each time loop gets executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Second time, the condition''' $i''' less than or equal to 4 will be checked. <br />
<br />
<br />
If the '''condition''' is true, the loop will get executed again.<br />
<br />
<br />
In our case, second time the output displayed on '''terminal''' will be ''''Value of I colon 1'.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The loop will get executed till the '''condition''' becomes false that is when variable''' i '''becomes 5.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl -c doWhileLoop.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to '''terminal''' and type the following to check for any compilation or syntax error.<br />
<br />
<br />
'''perl hyphen c doWhileLoop dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal -<br />
<br />
'''doWhileLoop.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be displayed on the '''terminal''' <br />
<br />
'''doWhileLoop.pl syntax OK'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute Perl script<br />
<br />
<br />
'''perl doWhileLoop.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As there are no compilation or syntax errors, we will now execute the '''Perl''' script.<br />
<br />
<br />
Type<br />
<br />
<br />
'''perl doWhileLoop dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output of the perl script on terminal<br />
<br />
'''Value of i: 0'''<br />
<br />
'''Value of i: 1'''<br />
<br />
'''Value of i: 2'''<br />
<br />
'''Value of i: 3'''<br />
<br />
'''Value of i: 4'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be displayed on the '''terminal'''.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us see the actual difference between '''while''' and '''do-while''' loops.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit loop.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type -<br />
<br />
<br />
'''gedit loop dot pl space ampersand'''<br />
<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''$count = 0;'''<br />
<br />
'''<nowiki># while loop</nowiki>'''<br />
<br />
'''while ($count > 0) {'''<br />
<br />
'''print “I am in while loop\n”;'''<br />
<br />
'''}'''<br />
<br />
<br />
'''<nowiki># do-while loop</nowiki>'''<br />
<br />
'''do {'''<br />
<br />
'''print “I am in do-while loop\n”;'''<br />
<br />
'''} while ($count > 0);'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open '''loop dot pl''' file in '''gedit'''.<br />
<br />
<br />
Now type the piece of code shown.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''$count = 0;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| We have declared a variable '''count''' and initialized it to zero<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''while ($count > 0)'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In the '''while''' loop '''condition,''' we are checking if the variable '''count''' is greater than zero.<br />
<br />
<br />
The '''condition''' is not true. So, the '''while''' loop code will not be executed even once.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''do {'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In the '''do...while''' loop''',''' we are first executing the code and then checking the condition.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''print “I am in do-while loop\n”;'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| So, the code will be executed at least once.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''} while ($count > 0);'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Then the '''condition''' whether the variable '''count''' is greater than zero, is checked.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The '''condition''' is not true. So the loop will exit.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, press '''ctrl+s''' to save the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
type '''perl -c loop.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to terminal and type the following to check for any compilation or syntax errors<br />
<br />
<br />
'''perl hyphen c loop dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal;<br />
<br />
'''loop.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be displayed on the terminal <br />
<br />
<br />
'''loop dot pl syntax OK'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute perl script<br />
<br />
<br />
'''perl loop.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| As there are no compilation or syntax errors, let us execute the '''Perl''' script.<br />
<br />
By Typing<br />
<br />
<br />
'''perl loop dot pl'''<br />
<br />
<br />
and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output of the perl script on terminal<br />
<br />
<br />
'''I am in do-while loop'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be displayed on the terminal.<br />
<br />
<br />
'''I am in do-while loop'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here we can see, there is no output message displaying ''''I am in while loop'''' <br />
<br />
<br />
This message was what we printed inside the '''while''' loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This implies that, <br />
<br />
<br />
'''do-while''' loop executes at-least once before evaluating the '''condition''' <br />
<br />
<br />
whereas '''while''' loop does not get executed even once when the '''condition''' specified is false.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I hope the difference is clear to you now. <br />
<br />
<br />
That's all there is to '''while''' and '''do-while''' loops.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
'''while''' loop and<br />
<br />
'''do-while''' loop in '''Perl'''<br />
<br />
using sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is assignment for you -<br />
<br />
<br />
Print ''''Hello Perl'''' <br />
<br />
till the count of variable reaches 10 <br />
<br />
by using '''while''' loop and '''do-while''' loops<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/for-for-each-loops/EnglishPERL/C2/for-for-each-loops/English2013-03-22T12:19:04Z<p>AmolBrahmankar: Created page with ''''Title Of Script: '''for & foreach Loops in Perl '''Author:''' Amol Brahmankar '''Keywords:''' Loops in Perl, for loop in perl, foreach in perl, video tutorial. {| style="…'</p>
<hr />
<div>'''Title Of Script: '''for & foreach Loops in Perl<br />
<br />
'''Author:''' Amol Brahmankar<br />
<br />
'''Keywords:''' Loops in Perl, for loop in perl, foreach in perl, video tutorial.<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border-top:0.05pt solid #000000;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <center>'''Visual Clue'''</center><br />
| style="border:0.05pt solid #000000;padding:0.097cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''for '''and''' foreach Loops''' in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Learning Objectives<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn about:<br />
<br />
'''for''' loop in '''Perl '''and<br />
<br />
'''foreach''' loop in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: System Requirements<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| I am using '''Ubuntu Linux12.04''' operating system and '''Perl 5.14.2'''<br />
<br />
<br />
I will also be using the '''gedit '''Text Editor.<br />
<br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide: Prerequisites<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| You should have basic knowledge of '''Variables '''and''' Comments '''in''' Perl.'''<br />
<br />
<br />
If not, please go through the relevant spoken tutorials on the '''spoken tutorial '''website.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Perl''' provides a mechanism by which we can check a condition repeatedly for various values. This is done using '''loops'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are various types of loops in Perl;<br />
<br />
'''for loop'''<br />
<br />
'''foreach loop'''<br />
<br />
'''while loop &'''<br />
<br />
'''do-while loop'''<br />
<br />
<br />
In this tutorial, we'll cover '''for''' and '''foreach loop'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''for '''loop in '''Perl''' can be used to execute a piece of code for a certain number of times.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''for (variable initialization; condition; increment/decrement) '''<br />
<br />
'''{'''<br />
<br />
'''Piece of code to be executed multiple times'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax of '''for loop''' is as follows:<br />
<br />
<br />
'''for space open bracket variable initialization semicolon condition semicolon increment close bracket'''<br />
<br />
<br />
Press '''Enter'''<br />
<br />
<br />
Open curly brackets<br />
<br />
<br />
Piece of code to be executed multiple times<br />
<br />
<br />
Close curly brackets<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of a '''for '''loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
'''gedit forLoop.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type;<br />
<br />
'''gedit forLoop.pl space & (ampersand)'''<br />
<br />
and press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''forLoop'''.'''pl''' in the Titlebar of gedit.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''forLoop'''.'''pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Type the piece of code and '''save''' the file<br />
<br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''<nowiki>for ($i=0; $i<=4; $i++) {</nowiki>'''<br />
<br />
'''print “Value of i: $i\n”;'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code;<br />
<br />
<br />
'''hash exclamation mark slash u s r slash bin slash perl'''<br />
<br />
<br />
Press '''Enter'''<br />
<br />
<br />
'''for space open bracket dollar i equals to zero semicolon space dollar i less than or equal to four semicolon space dollar i plus plus close bracket'''<br />
<br />
space<br />
<br />
Open curly bracket <br />
<br />
press enter<br />
<br />
'''print space double quote Value of i colon space dollar I slash n double quote conmplete semicolon '''<br />
<br />
<br />
Press '''Enter'''<br />
<br />
now<br />
<br />
Close curly bracket<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''ctrl+s'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''Ctrl+S''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''$i=0'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let me explain what the '''for''' loop does.<br />
<br />
<br />
The variable '''i''' is initialized to zero.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>$i<=4</nowiki>'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Next, the condition is checked. <br />
<br />
In this case, the condition is '''i is less than or equal to 4'''.<br />
<br />
<br />
If this condition is true, the code within the curly bracket will be executed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''print'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This means the first '''print''' statement <br />
<br />
'''“Value of i: 0”'''<br />
<br />
will be displayed on the '''terminal'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''$i++'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| After this, the variable '''i''' is incremented by '''1.''' <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''<nowiki>$i<=4</nowiki>'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| And the '''for''' loop condition is checked once again.<br />
<br />
<br />
This loop will exit when the value of''' i''' becomes greater than '''4'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In this case, the '''for''' loop will be executed for '''i = 0, 1, 2, 3, 4'''<br />
<br />
<br />
which is a total of '''5 '''times.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to '''terminal'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to the '''terminal'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''perl -c forLoop.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following to check for any compilation or syntax error:<br />
<br />
<br />
'''perl hyphen c forLoop dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal;<br />
<br />
'''forLoop.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here it displays a message <br />
<br />
'''forLoop.pl syntax OK'''<br />
<br />
<br />
So, we have no errors.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute perl script<br />
<br />
<br />
'''perl forLoop.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let's execute the '''Perl''' script by typing<br />
<br />
<br />
'''perl forLoop dot pl'''<br />
<br />
<br />
and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight/point to the output of the perl script on terminal<br />
<br />
'''Value of i: 0'''<br />
<br />
'''Value of i: 1'''<br />
<br />
'''Value of i: 2'''<br />
<br />
'''Value of i: 3'''<br />
<br />
'''Value of i: 4'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be shown on terminal.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, let us look at the '''foreach '''loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| If we want to iterate a condition for an '''array''', we can make use of '''foreach '''loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide<br />
<br />
<br />
'''foreach $variable (@array) {'''<br />
<br />
'''perform action on each element of an array'''<br />
<br />
'''}'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The syntax is:<br />
<br />
<br />
'''foreach space dollar variable space within brackets at the rate array '''<br />
<br />
<br />
'''space '''<br />
<br />
<br />
open curly bracket <br />
<br />
<br />
'''perform action on each element of an array '''<br />
<br />
<br />
Press '''Enter'''<br />
<br />
Close the curly bracket.<br />
<br />
<br />
'''Please note: '''We'll cover array, array initialization and defining an array in subsequent tutorials.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now let us look at an example of '''foreach''' loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to the Terminal and type<br />
<br />
<br />
'''gedit foreachLoop.pl &'''<br />
<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Open the '''Terminal''' and type<br />
<br />
<br />
'''gedit foreachLoop dot pl space ampersand'''<br />
<br />
<br />
and Press''' Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Point to the filename '''foreachLoop'''.'''pl''' in the Titlebar of gedit.<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This will open the '''foreachLoop'''.'''pl''' file in '''gedit'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| '''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
'''@myarray = (10, 20, 30);'''<br />
<br />
<br />
'''foreach $var (@myarray) {'''<br />
<br />
'''print “Element of array is: $var \n”;'''<br />
<br />
'''}'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Type the following piece of code<br />
<br />
<br />
'''hash exclamation mark slash u s r slash bin slash perl'''<br />
<br />
<br />
and Press '''Enter'''<br />
<br />
<br />
'''at the rate myarray is equal to open bracket ten comma space twenty comma space thirty close bracket semicolon'''<br />
<br />
press enter <br />
<br />
'''foreach space dollar var space open bracket at the rate myarray close bracket space'''<br />
<br />
<br />
Open curly bracket<br />
<br />
press enter<br />
<br />
<br />
'''print space double quote Element of array is colon dollar var space slash n double quote complete semicolon '''<br />
<br />
<br />
Press '''Enter and'''<br />
<br />
Close curly bracket<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+S'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Press '''ctrl+s''' to '''save''' the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight<br />
<br />
'''@myarray = (10, 20, 30);'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let me explain what this code does.<br />
<br />
<br />
An array '''myarray '''is declared. <br />
<br />
It has 3 elements 10, 20 and 30.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''$var '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| In each iteration of foreach loop dollar var will contain the single element of an array<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''foreach $var (@myarray)'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''foreach '''keyword will repeat this loop for each element of an array.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''print “Element of array is:$var \n”;'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| That is, the code within the curly bracket will be executed for each '''myarray '''element.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''\n'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| '''Back-slash n''' will place the prompt on a new line.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This means the first '''element''' ''''10'''' will be displayed on the terminal.<br />
<br />
<br />
Then 20 and so on, till all the elements are printed.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight '''foreach '''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| This loop will exit after printing all the elements in '''myarray'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Switch to terminal<br />
<br />
<br />
'''perl -c foreachLoop.pl'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Now, switch to terminal and type the following to check for any compilation or syntax error.<br />
<br />
type<br />
<br />
'''perl hyphen c foreachLoop dot pl'''<br />
<br />
<br />
and press''' Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the below line on terminal;<br />
<br />
'''foreachLoop.pl syntax OK'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following line will be shown on terminal<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Execute perl script<br />
<br />
<br />
'''perl foreachLoop.pl'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| There are no compilation or syntax errors.<br />
<br />
So let us execute the '''Perl''' script.<br />
<br />
<br />
Type <br />
<br />
<br />
'''perl foreachLoop dot pl'''<br />
<br />
<br />
and press '''Enter'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Highlight the output of the perl script on terminal <br />
<br />
'''Element of an array is: 10'''<br />
<br />
'''Element of an array is: 20'''<br />
<br />
'''Element of an array is: 30'''<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The following output will be shown on terminal.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| So, that's it about '''for''' loop and '''foreach''' loop.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Slide : Summary<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Let us summarize.<br />
<br />
In this tutorial, we have learnt -<br />
<br />
'''for''' loop and<br />
<br />
'''foreach''' loop in '''Perl'''<br />
<br />
using some sample programs.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Assignment<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Here is an assignment for you -<br />
<br />
# Declare a string as 'Spoken Tutorial' and <br />
# Print it 5 times<br />
# Declare an array of colours as @colorArray = ('red', 'white', 'blue') and <br />
# Print the element of an array using '''foreach''' loop<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Watch the video available at the following link<br />
<br />
<br />
It summaries the Spoken Tutorial project<br />
<br />
<br />
If you do not have good bandwidth, you can<br />
<br />
download and watch it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Spoken Tutorial Workshops<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team <br />
<br />
<br />
Conducts workshops using spoken tutorials<br />
<br />
<br />
Gives certificates to those who pass an online <br />
<br />
test<br />
<br />
<br />
For more details, please write to<br />
<br />
contact at spoken hyphen tutorial dot org<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| Acknowledgment<br />
<br />
<br />
http://spoken-tutorial.org\NMEICT-Intro<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project<br />
<br />
<br />
It is supported by the National Mission on<br />
<br />
Education through ICT, MHRD, Government of India.<br />
<br />
<br />
More information on this Mission is available<br />
<br />
<br />
spoken hypen tutorial dot org slash NMEICT hyphen Intro<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:none;padding:0.097cm;"| <br />
| style="border-top:none;border-bottom:0.05pt solid #000000;border-left:0.05pt solid #000000;border-right:0.05pt solid #000000;padding:0.097cm;"| Hope you enjoyed this Perl tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
<br />
Thanks for joining.<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Comments-in-Perl/EnglishPERL/C2/Comments-in-Perl/English2013-03-21T06:12:08Z<p>AmolBrahmankar: Created page with ''''Title of script''': Comments in Perl '''Author: Amol''' '''Keywords: Comments in Perl, Video Tutorials, Scalars in Perl''' {| style="border-spacing:0;" | style="border:0.…'</p>
<hr />
<div>'''Title of script''': Comments in Perl<br />
<br />
'''Author: Amol'''<br />
<br />
'''Keywords: Comments in Perl, Video Tutorials, Scalars in Perl'''<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Visual Cue'''</center><br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on Comments in '''Perl'''. <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide:Learning Objectives<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| In this tutorial, we will learn about<br />
<br />
Comments in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide:System Requirements<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| I am using '''Ubuntu Linux12.04''' operating system and <br />
<br />
'''Perl''' '''5.14.2 '''<br />
<br />
that is, '''Perl''' revision 5 version 14 and subversion 2<br />
<br />
<br />
I will also be using the '''gedit Text Editor'''. <br />
<br />
You can use any text editor of your choice.<br />
<br />
<br />
You should have Basic knowledge of Compiling, Executing and Variables in Perl <br />
<br />
If not, please go through the relevant spoken tutorials on the spoken tutorial website <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Commenting a piece of code in '''Perl''' can be done in two ways:<br />
<br />
a. Single Line<br />
<br />
b. Multi Line<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''Single Line'''<br />
<br />
This type of comment is used <br />
<br />
* when user wants to comment a single line of code or <br />
* to add one liner text to explain the functionality of a piece of code<br />
<br />
This type of comment starts with the symbol '''<nowiki># (hash)</nowiki>'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Here is a demo.<br />
<br />
Let us open a new file in the Text Editor.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''' and type <br />
<br />
'''gedit singlemulti.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Open the '''Terminal''' and type -<br />
<br />
'''gedit comments dot pl &'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''&'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Once again, reminding you that the '''ampersand''' is used to free the '''command prompt''' in the '''terminal'''. And press enter<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''<nowiki># Declaring count variable</nowiki>'''<br />
<br />
'''$count = 1; '''<br />
<br />
'''print “Count is $count \n”; # prints Count is 1'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now type the following commands.<br />
<br />
'''hash Declaring count variable'''<br />
<br />
'''press enter'''<br />
<br />
'''dollar count space equal to space 1 semicolon'''<br />
<br />
'''press enter'''<br />
<br />
'''print space double quotes Count is dollar count slash n double quote complete semicolon space hash prints Count is 1'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save the file.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Save this file by pressing ctlr s file and execute the '''Perl''' script.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c singlemulti.pl'''<br />
<br />
and press '''Enter'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''', and type<br />
<br />
'''perl hyphen c comments dot pl'''<br />
<br />
and press '''Enter.'''<br />
<br />
This tells us that there is no syntax error<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Then type<br />
<br />
'''perl singlemulti.pl'''<br />
<br />
and press '''Enter'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now type<br />
<br />
'''perl comments dot pl'''<br />
<br />
and press '''Enter.'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the output<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| It will show the following output -<br />
<br />
'''Count is 1'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Go to '''gedit''' and type <br />
<br />
'''<nowiki>#!/usr/bin/perl</nowiki>'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Let us switch back to '''gedit'''.<br />
<br />
in '''gedit''', go to the first line and press enter.<br />
<br />
Go back to the first line and type the following code.<br />
<br />
'''Hash exclamation mark slash usr slash bin slash perl'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This line in '''Perl''' is called as a '''shebang''' '''line''' and is the first line in a '''Perl '''program.<br />
<br />
It tells where to find the '''Perl Interpreter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Note:''' Though this line starts with hash''' '''symbol, it will not be considered as a single line comment by '''Perl'''.<br />
<br />
Now let us look at multiline comments<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''Multi Line'''<br />
<br />
This type of comment is used <br />
<br />
* when user wants to comment a piece of code or <br />
* to add description/usage of piece of code<br />
<br />
This type of comment starts with the symbol '''equal to''' '''''head''''' and ends with '''equal to ''cut'''''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''<nowiki>=head</nowiki>'''<br />
<br />
'''print “ count variable will be used for counting purpose”'''<br />
<br />
'''<nowiki>=cut</nowiki>'''<br />
<br />
above<br />
<br />
'''$count = 1; '''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Lets switch back to '''gedit''' and type the following -<br />
<br />
in the '''comments dot pl '''file<br />
<br />
at the end of file type<br />
<br />
'''equal to head'''<br />
<br />
'''press enter'''<br />
<br />
'''print space double quote count variable will be used for counting purpose double quote complete'''<br />
<br />
'''press enter'''<br />
<br />
'''equal to cut'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save the file and close it.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save the file, close it and execute the '''Perl''' script.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c singlemulti.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| On the '''Terminal''', type<br />
<br />
'''perl hyphen c comments dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
No syntax error<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Then type<br />
<br />
'''perl singlemulti.pl'''<br />
<br />
and press '''Enter'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| so let us execute it<br />
<br />
'''perl comments dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the output<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| It will show the same output as before.<br />
<br />
'''Count is 1'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to gedit<br />
<br />
Point to the sentence<br />
<br />
'''“count variable will be used for counting purpose”'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| It does not print the sentence '''“count variable will be used for counting purpose”'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to '''<nowiki>=head</nowiki>''' and '''<nowiki>=cut </nowiki>'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This is because we commented the portion using '''equal to head''' and '''equal to cut'''<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| You can either use '''<nowiki>=head =cut</nowiki>''' or '''<nowiki>=begin =end.</nowiki>'''<br />
<br />
These are not the special keywords used by '''Perl'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Please note''' there should not be any leading or trailing space(s) before '''<nowiki>= to</nowiki>''' sign and after the '''head''', '''cut''', '''begin''' or '''end''' word.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''' <br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Open the '''Terminal''' once again.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type <br />
<br />
'''gedit comments.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| And Type -<br />
<br />
'''gedit commentsExample dot pl &'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''<nowiki># Here I am going to explain commenting in Perl</nowiki>'''<br />
<br />
'''<nowiki># Following is the scalar variable declaration</nowiki>'''<br />
<br />
'''$firstNum = 10;'''<br />
<br />
'''$secondNum = 20;'''<br />
<br />
<br />
'''<nowiki>=head</nowiki>'''<br />
<br />
'''following piece of code adds 2 numbers'''<br />
<br />
'''puts the result into a 3<sup>rd</sup> variable'''<br />
<br />
'''and prints this 3<sup>rd</sup> variable'''<br />
<br />
'''<nowiki>=cut</nowiki>'''<br />
<br />
'''$addition = $firstNum + $secondNum;'''<br />
<br />
'''print “Addition is $addition”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following commands as shown on the screen.<br />
<br />
Here I am declaring two variables '''firstNum '''and '''secondNum '''and I am assigning some values to them.<br />
<br />
Then I have commented this portion here.<br />
<br />
Now I added these two numbers and assign the value to a third variable named addition.<br />
<br />
Next I want to print the value using print command.<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save the file and switch to the '''Terminal'''.<br />
<br />
Type<br />
<br />
'''perl -c comments.pl'''<br />
<br />
'''perl comments.pl'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save the file and execute the '''Perl''' script on the '''Terminal'''.<br />
<br />
<br />
On terminal type<br />
<br />
'''perl hyphen c commentsExample dot pl'''<br />
<br />
press enter<br />
<br />
There is no syntax error<br />
<br />
so execute the script by typing<br />
<br />
<br />
'''perl commentsExample dot pl'''<br />
<br />
press enter<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the output<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| It will show the following output.<br />
<br />
'''Addition is 30'''<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Summary<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| This brings us to the end of this tutorial.<br />
<br />
Here we learnt, <br />
<br />
To Add Comments in '''Perl'''<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Assignment<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''Assignment'''<br />
<br />
# Write a perl script to find square of a number. <br />
# Explain the functionality of the code written using: <br />
<br />
* Single Line Comment. <br />
* Mutli Line Comment. <br />
<br />
<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| About the Project<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Watch the video available at the following link <br />
<br />
It summarises the Spoken Tutorial project <br />
<br />
If you do not have good bandwidth, you can <br />
<br />
download and watch it <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Workshops <br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| The Spoken Tutorial Project Team <br />
<br />
Conducts workshops using spoken tutorials <br />
<br />
Gives certificates to those who pass an online <br />
<br />
test <br />
<br />
For more details, please write to <br />
<br />
contact at spoken hyphen tutorial dot org <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Acknowledgment<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project <br />
<br />
It is supported by the National Mission on <br />
<br />
Education through ICT, MHRD, Government <br />
<br />
of India <br />
<br />
More information on this Mission is available <br />
<br />
at the following link.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Hope you enjoyed this '''Perl''' tutorial. <br />
<br />
This is Amol Brahmankar signing off.<br />
<br />
Thanks for joining.<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Variables-in-Perl/EnglishPERL/C2/Variables-in-Perl/English2013-03-18T13:14:55Z<p>AmolBrahmankar: </p>
<hr />
<div>'''Title of script''': Variables in Perl<br />
<br />
'''Author: Amol'''<br />
<br />
'''Keywords: Variables in Perl, Video Tutorial, Scalars in Perl'''<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Visual Cue'''</center><br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on Variables in '''Perl'''. <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide:Learning Objectives<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| In this tutorial, we will learn about<br />
<br />
Variables in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide:System Requirements<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| I am using '''Ubuntu Linux12.04''' operating system and <br />
<br />
'''Perl''' '''5.14.2 '''<br />
<br />
that is, '''Perl''' revision 5 version 14 and subversion 2<br />
<br />
<br />
I will also be using the '''gedit Text Editor'''. <br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Variables in '''Perl''':<br />
<br />
Variables are used for storing values, like text strings, numbers or arrays.<br />
<br />
Once a variable is declared, it can be used over and over again in the script.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Scalar represents a single value and can store scalars only.<br />
<br />
Scalar variables are declared using $ (dollar) symbol.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Let us look at Variable Declaration:<br />
<br />
A variable can be declared as follows:<br />
<br />
'''dollar priority semicolon'''<br />
<br />
'''$priority;'''<br />
<br />
<br />
Variable names in '''Perl''' can have several formats. Variables must begin with a letter or underscore <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| And may contains letters, digits, underscores or a combination of above 3.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Variables declared with CAPITAL letters have special meaning in '''Perl.''' <br />
<br />
So avoid declaring variables using Capital letters.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''gedit variables.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Open the '''Terminal''' and type <br />
<br />
'''gedit variables.pl & '''<br />
<br />
<br />
'''The ampersand will unlock the command prompt on the terminal)'''<br />
<br />
Now press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the filename '''variables.pl''' in the Titlebar of '''gedit'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will open the '''variables.pl '''file in gedit text editor.<br />
<br />
'''dot pl '''is the default extension of a '''Perl''' file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following<br />
<br />
'''$priority;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following in the file;<br />
<br />
'''dollar priority semicolon'''<br />
<br />
'''$priority;'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''$priority; '''with the cursor<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| So we have declared the variable '''priority'''.<br />
<br />
You do not need to declare a variable before using it;<br />
<br />
you can just use it into your code. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$priority = 1;'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us assign a numerical value to the variable '''priority''' <br />
<br />
<br />
for this Type<br />
<br />
'''dollar priority space equal to space one semicolon'''<br />
<br />
'''$priority = 1;'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “Value of variable is: $priority”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Next, type<br />
<br />
'''print space double quote Value of variable is: dollar priority slash n double quote complete semicolon'''<br />
<br />
'''slash n is the new line character'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save this file as '''variables.pl '''at any location of your choice.<br />
<br />
I am storing it in my home directory''' /home/amol.'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Save this file as '''variables.pl''' at any location.<br />
<br />
In my case, it will get saved in '''/home/amol '''directory. Now save this file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type <br />
<br />
'''chmod 755 variables.pl '''<br />
<br />
explain the need of above line and then press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now, let us change the permissions of '''variables.pl''' '''Perl '''file which we just created.<br />
<br />
To do so, on the '''Terminal''' type,<br />
<br />
'''chmod 755 variables.pl'''<br />
<br />
This will provide read, write & execute rights to the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
but don't press '''Enter''' yet.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this '''Perl''' script, on the '''Terminal'''.<br />
<br />
Type the following command;<br />
<br />
'''perl hyphen c variables dot pl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''-c''' using the cursor.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Hyphen c '''switch compiles the '''Perl''' script for any compilation/syntax error.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now press '''Enter''' <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now press enter<br />
<br />
This tells us that there is no syntax error in our script<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''variables.pl syntax OK'''<br />
<br />
<br />
Type<br />
<br />
'''perl variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let's execute the '''Perl''' script by typing<br />
<br />
'''perl variables dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to<br />
<br />
'''Value of variable is: 1'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output displayed is as highlighted<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| We can also assign a string value to the variable we declared.<br />
<br />
Switch back to the Text editor window.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''1'''<nowiki>; with the cursor</nowiki><br />
<br />
Instead of '''1''' type<br />
<br />
''''high''''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Instead of '''dollar priority equal to one;''' <br />
<br />
type<br />
<br />
'''dollar priority equal to in single quote high '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point from right of '''<nowiki>= </nowiki>'''to left.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Please note that assignments are evaluated from right to left.<br />
<br />
A '''scalar''' can hold data of any type, be it a string, a number <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Save this file and compile the script once again by typing<br />
<br />
<br />
'''perl hyphen c variables dot pl'''<br />
<br />
now press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''perl variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This tells us that there is no syntax error.<br />
<br />
<br />
Execute the script by typing<br />
<br />
'''perl variables dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to<br />
<br />
'''Value of variable is: high'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output is as shown<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.<br />
<br />
Go to the last line and press '''Enter.'''<br />
<br />
Type <br />
<br />
'''$priority = “String”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Switch back to the Text Editor window.<br />
<br />
<br />
You can also use scalars within a double-quoted strings as<br />
<br />
'''dollar priority in double quote String'''<br />
<br />
<br />
Save this file and close it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us learn how to declare multiple variables.<br />
<br />
To do so open the new file in Text Editor.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''' and type <br />
<br />
'''gedit multivar.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| on'''Terminal''' type -<br />
<br />
'''gedit multivar dot pl space &'''<br />
<br />
and press '''Enter'''.<br />
<br />
<br />
This will open multivar dot pl file in text editor<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$firstVar, $secondVar;'''<br />
<br />
and press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Now will type -<br />
<br />
'''dollar firstVar comma dollar secondVar semicolon'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$firstVar = $secondVar;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To copy the value of variable '''dollar firstVar''' to '''dollar secondVar''', type -<br />
<br />
'''dollar firstVar space equal to space dollar secondVar semicolon'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to slide<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables <br />
<br />
Let us see how we can achieve this using '''Perl'''.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''$firstVar = $secondVar =10;'''<br />
<br />
Press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to text editor and now Let us assign the value '''10''' to both these variables by typing '''dollar firstVar equal to dollar secondVar equal to ten semicolon'''.<br />
<br />
And Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “firstVar: $firstVar and secondVar: $secondVar”;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now To print the values, type<br />
<br />
'''print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon''' press '''Enter'''.<br />
<br />
Now save this file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$addition = $firstVar + $secondVar;'''<br />
<br />
Press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us add the values in the two variables.<br />
<br />
For this type<br />
<br />
'''dollar addition space equal to space dollar firstVar plus space dollar secondVar semicolon'''<br />
<br />
and Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight the variable '''addition '''with the cursor <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Notice, we haven't declared the variable '''addition'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “Addition: $addition”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Once again to print the value of the addition, type<br />
<br />
'''print double quote Addition is dollar addition slash n double quote complete semicolon'''<br />
<br />
Save this file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this file again on terminal type<br />
<br />
'''perl hyphen c multivar dot pl'''<br />
<br />
there is no syntax error so<br />
<br />
we can execute the script by typing<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''perl multivar.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''perl multivar dot pl'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to the output<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will produce an output as highlighted<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Pause the video recording here<br />
<br />
<br />
Type out the code for subtraction, multiplication and division in the '''multivar.pl''' file.<br />
<br />
<br />
Resume the video recording now<br />
<br />
<br />
Also, compile the code so that the output is displayed on the '''Terminal'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to '''gedit'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Similarly, try subtraction, multiplication and division.<br />
<br />
<br />
I have written the code here.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Close this file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let us save this file and close it.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Show the output in the '''terminal'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now compile the file by typing<br />
<br />
perl hyphen c multivar dot pl<br />
<br />
There is no syntax error so we can execute the script as \<br />
<br />
perl multivar dot pl<br />
<br />
<br />
On execution the output will look like this<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Summary<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This brings us to the end of this tutorial.<br />
<br />
In this tutorial we have learnt, <br />
<br />
To Declare and Use scalar variables in '''Perl'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Assignment<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Assignment'''<br />
<br />
# Declare a number variable. <br />
# Assign the value 10 to it. <br />
# Print the variable declared.<br />
# Declare 2 string variables.<br />
# Assign these values to them - “Namaste ” and “India”. <br />
# Print those string variables one after the other.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Watch the video available at the following link <br />
<br />
It summarises the Spoken Tutorial project <br />
<br />
If you do not have good bandwidth, you can <br />
<br />
download and watch it <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Workshops <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The Spoken Tutorial Project Team <br />
<br />
Conducts workshops using spoken tutorials <br />
<br />
Gives certificates to those who pass an online <br />
<br />
test <br />
<br />
For more details, please write to <br />
<br />
contact at spoken tutorial dot org <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Acknowledgment<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project <br />
<br />
It is supported by the National Mission on <br />
<br />
Education through ICT, MHRD, Government <br />
<br />
of India <br />
<br />
More information on this Mission is available <br />
<br />
at the following link.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Hope you enjoyed this '''Perl''' tutorial. This is Amol Brahmankar signing off.<br />
<br />
Thanks for joining.<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERL/C2/Variables-in-Perl/EnglishPERL/C2/Variables-in-Perl/English2013-03-18T12:34:08Z<p>AmolBrahmankar: Created page with ''''Title of script''': Variables in Perl '''Author: Amol''' '''Keywords: Variables in Perl, Video Tutorial, Scalars in Perl''' {| style="border-spacing:0;" | style="border:0…'</p>
<hr />
<div>'''Title of script''': Variables in Perl<br />
<br />
'''Author: Amol'''<br />
<br />
'''Keywords: Variables in Perl, Video Tutorial, Scalars in Perl'''<br />
<br />
<br />
<br />
{| style="border-spacing:0;"<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Visual Cue'''</center><br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| <center>'''Narration'''</center><br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Welcome to the spoken tutorial on Variables in '''Perl'''. <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide:Learning Objectives<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| In this tutorial, we will learn about<br />
<br />
Variables in '''Perl'''<br />
<br />
Comments in '''Perl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide:System Requirements<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| I am using '''Ubuntu Linux12.04''' operating system and <br />
<br />
'''Perl''' '''5.14.2 '''<br />
<br />
that is, '''Perl''' revision 5 version 14 and subversion 2<br />
<br />
<br />
I will also be using the '''gedit Text Editor'''. <br />
<br />
You can use any text editor of your choice.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Variables in '''Perl''':<br />
<br />
Variables are used for storing values, like text strings, numbers or arrays.<br />
<br />
Once a variable is declared, it can be used over and over again in the script.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Scalar represents a single value and can store scalars only.<br />
<br />
Scalar variables are declared using $ (dollar) symbol.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Let us look at Variable Declaration:<br />
<br />
A variable can be declared as follows:<br />
<br />
'''dollar priority semicolon'''<br />
<br />
'''$priority;'''<br />
<br />
<br />
Variable names in '''Perl''' can have several formats. Variables must begin with a letter or underscore <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| And may contains letters, digits, underscores or a combination of above 3.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Slide<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Variables declared with CAPITAL letters have special meaning in '''Perl.''' <br />
<br />
So avoid declaring variables using Capital letters.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''gedit variables.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Open the '''Terminal''' and type <br />
<br />
'''gedit variables.pl & '''<br />
<br />
<br />
'''The ampersand will unlock the command prompt on the terminal)'''<br />
<br />
Now press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point to the filename '''variables.pl''' in the Titlebar of '''gedit'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will open the '''variables.pl '''file in gedit text editor.<br />
<br />
'''dot pl '''is the default extension of a '''Perl''' file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following<br />
<br />
'''$priority;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type the following in the file;<br />
<br />
'''dollar priority semicolon'''<br />
<br />
'''$priority;'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''$priority; '''with the cursor<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| So we have declared the variable '''priority'''.<br />
<br />
You do not need to declare a variable before using it;<br />
<br />
you can just use it into your code. <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$priority = 1;'''<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us assign a numerical value to the variable '''priority''' <br />
<br />
<br />
for this Type<br />
<br />
'''dollar priority space equal to space one semicolon'''<br />
<br />
'''$priority = 1;'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “Value of variable is: $priority”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Next, type<br />
<br />
'''print space double quote Value of variable is: dollar priority slash n double quote complete semicolon'''<br />
<br />
'''slash n is the new line character'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Save this file as '''variables.pl '''at any location of your choice.<br />
<br />
I am storing it in my home directory''' /home/amol.'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Save this file as '''variables.pl''' at any location.<br />
<br />
In my case, it will get saved in '''/home/amol '''directory. Now save this file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type <br />
<br />
'''chmod 755 variables.pl '''<br />
<br />
explain the need of above line and then press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now, let us change the permissions of '''variables.pl''' '''Perl '''file which we just created.<br />
<br />
To do so, on the '''Terminal''' type,<br />
<br />
'''chmod 755 variables.pl'''<br />
<br />
This will provide read, write & execute rights to the file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
but don't press '''Enter''' yet.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this '''Perl''' script, on the '''Terminal'''.<br />
<br />
Type the following command;<br />
<br />
'''perl hyphen c variables dot pl'''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''-c''' using the cursor.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Hyphen c '''switch compiles the '''Perl''' script for any compilation/syntax error.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now press '''Enter''' <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now press enter<br />
<br />
This tells us that there is no syntax error in our script<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''variables.pl syntax OK'''<br />
<br />
<br />
Type<br />
<br />
'''perl variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let's execute the '''Perl''' script by typing<br />
<br />
'''perl variables dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to<br />
<br />
'''Value of variable is: 1'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output displayed is as highlighted<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| We can also assign a string value to the variable we declared.<br />
<br />
Switch back to the Text editor window.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight '''1'''<nowiki>; with the cursor</nowiki><br />
<br />
Instead of '''1''' type<br />
<br />
''''high''''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Instead of '''dollar priority equal to one;''' <br />
<br />
type<br />
<br />
'''dollar priority equal to in single quote high '''<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point from right of '''<nowiki>= </nowiki>'''to left.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Please note that assignments are evaluated from right to left.<br />
<br />
A '''scalar''' can hold data of any type, be it a string, a number <br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Save this file and compile the script once again by typing<br />
<br />
<br />
'''perl hyphen c variables dot pl'''<br />
<br />
now press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''perl variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This tells us that there is no syntax error.<br />
<br />
<br />
Execute the script by typing<br />
<br />
'''perl variables dot pl'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to<br />
<br />
'''Value of variable is: high'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The output is as shown<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch back to the Text editor window.<br />
<br />
Go to the last line and press '''Enter.'''<br />
<br />
Type <br />
<br />
'''$priority = “String”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Switch back to the Text Editor window.<br />
<br />
<br />
You can also use scalars within a double-quoted strings as<br />
<br />
'''dollar priority in double quote String'''<br />
<br />
<br />
Save this file and close it<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us learn how to declare multiple variables.<br />
<br />
To do so open the new file in Text Editor.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the '''Terminal''' and type <br />
<br />
'''gedit multivar.pl &'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| on'''Terminal''' type -<br />
<br />
'''gedit multivar dot pl space &'''<br />
<br />
and press '''Enter'''.<br />
<br />
<br />
This will open multivar dot pl file in text editor<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$firstVar, $secondVar;'''<br />
<br />
and press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Now will type -<br />
<br />
'''dollar firstVar comma dollar secondVar semicolon'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$firstVar = $secondVar;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To copy the value of variable '''dollar firstVar''' to '''dollar secondVar''', type -<br />
<br />
'''dollar firstVar space equal to space dollar secondVar semicolon'''<br />
<br />
and press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to slide<br />
<br />
<br />
<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| All mathematical operations like addition, subtraction, multiplication, division, can be done on these variables <br />
<br />
Let us see how we can achieve this using '''Perl'''.<br />
<br />
|-<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| '''$firstVar = $secondVar =10;'''<br />
<br />
Press '''Enter'''.<br />
| style="border:0.5pt solid #000000;padding:0.106cm;"| Switch to text editor and now Let us assign the value '''10''' to both these variables by typing '''dollar firstVar equal to dollar secondVar equal to ten semicolon'''.<br />
<br />
And Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “firstVar: $firstVar and secondVar: $secondVar”;'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now To print the values, type<br />
<br />
'''print double quote firstVar: dollar firstVar and secondVar: dollar secondVar slash n double quote complete semicolon''' press '''Enter'''.<br />
<br />
Now save this file.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''$addition = $firstVar + $secondVar;'''<br />
<br />
Press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now let us add the values in the two variables.<br />
<br />
For this type<br />
<br />
'''dollar addition space equal to space dollar firstVar plus space dollar secondVar semicolon'''<br />
<br />
and Press '''Enter'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Highlight the variable '''addition '''with the cursor <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Notice, we haven't declared the variable '''addition'''.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''print “Addition: $addition”;'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Once again to print the value of the addition, type<br />
<br />
'''print double quote Addition is dollar addition slash n double quote complete semicolon'''<br />
<br />
Save this file<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to the Terminal and type<br />
<br />
'''perl -c variables.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| To compile this file again on terminal type<br />
<br />
'''perl hyphen c multivar dot pl'''<br />
<br />
there is no syntax error so<br />
<br />
we can execute the script by typing<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Type<br />
<br />
'''perl multivar.pl'''<br />
<br />
and press '''Enter'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''perl multivar dot pl'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Point with the cursor to the output<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This will produce an output as highlighted<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Pause the video recording here<br />
<br />
<br />
Type out the code for subtraction, multiplication and division in the '''multivar.pl''' file.<br />
<br />
<br />
Resume the video recording now<br />
<br />
<br />
Also, compile the code so that the output is displayed on the '''Terminal'''.<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Switch to '''gedit'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Similarly, try subtraction, multiplication and division.<br />
<br />
<br />
I have written the code here.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Close this file in '''gedit'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now Let us save this file and close it.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Show the output in the '''terminal'''<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Now compile the file by typing<br />
<br />
perl hyphen c multivar dot pl<br />
<br />
There is no syntax error so we can execute the script as \<br />
<br />
perl multivar dot pl<br />
<br />
<br />
On execution the output will look like this<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Summary<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| This brings us to the end of this tutorial.<br />
<br />
In this tutorial we have learnt, <br />
<br />
To Declare and Use scalar variables in '''Perl'''<br />
<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Assignment<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| '''Assignment'''<br />
<br />
# Declare a number variable. <br />
# Assign the value 10 to it. <br />
# Print the variable declared.<br />
# Declare 2 string variables.<br />
# Assign these values to them - “Namaste ” and “India”. <br />
# Print those string variables one after the other.<br />
<br />
<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| About the Project<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Watch the video available at the following link <br />
<br />
It summarises the Spoken Tutorial project <br />
<br />
If you do not have good bandwidth, you can <br />
<br />
download and watch it <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Workshops <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| The Spoken Tutorial Project Team <br />
<br />
Conducts workshops using spoken tutorials <br />
<br />
Gives certificates to those who pass an online <br />
<br />
test <br />
<br />
For more details, please write to <br />
<br />
contact at spoken tutorial dot org <br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Acknowledgment<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Spoken Tutorial Project is a part of the Talk to a <br />
<br />
Teacher project <br />
<br />
It is supported by the National Mission on <br />
<br />
Education through ICT, MHRD, Government <br />
<br />
of India <br />
<br />
More information on this Mission is available <br />
<br />
at the following link.<br />
<br />
|-<br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| <br />
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.106cm;"| Hope you enjoyed this '''Perl''' tutorial. This is Amol Brahmankar signing off.<br />
<br />
Thanks for joining.<br />
<br />
|}</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-03-08T12:23:32Z<p>AmolBrahmankar: /* Basic Level */</p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if-if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
# if-elsif-else-switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-02-15T04:56:36Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if-if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
# if-elsif-else-switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {</nowiki> <br><nowiki>print “Key: $key & value: $value”;</nowiki> <br><nowiki>}</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, "<FileName.txt";<br />
#* To open a file in read, write mode<br />
#** open FH, ">FileName.txt";<br />
#* To open a file in append mode<br />
#** open FH, ">>FileName.txt";<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH "I am writing in a file";<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-02-12T16:47:43Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if-if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
# if-elsif-else-switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {{</nowiki> <br><nowiki>print “Key: $key & value: $value”;{</nowiki> <br><nowiki>}{</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling<br />
#* To open a file <br />
#** open FH, FileName.txt; # FH is the file handle<br />
#* To open a file in read mode<br />
#** open FH, ‘<FileName.txt’;<br />
#* To open a file in read, write mode<br />
#** open FH, ‘>FileName.txt’;<br />
#* To open a file in append mode<br />
#** open FH, ‘>>FileName.txt’;<br />
#* Looping on each line can be done as<br />
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print “$_â€; # prints each line of a file</nowiki><br><nowiki>}</nowiki><br />
#* write into a file<br />
#** print FH “I am writing in a fileâ€;<br />
#* close the file handle<br />
#** close FH<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#** it includes a file without checking that the file is already included or not<br />
#* require<br />
#** it includes a file if it not included already.<br />
#* use<br />
#** it includes perl module files only. <br />
#** Files get included before the actual execution of the code gets start.<br />
#Perl Modules<br />
#* These are the .pm files. <br />
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.<br />
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.<br />
#PERL Module library<br />
#* CPAN<br />
#** Comprehensive Perl Archive Network is the library for modules. <br />
#** We can search for require module in CPAN. <br />
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#** If user has downloaded a module on a path of his choice and<br />
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;<br />
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,‘user/choice/path/of/file’);</nowiki><br><nowiki>};</nowiki><br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#** Windows<br />
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed. <br />
#*** This utility can be used to search and install the require module on windows environment. <br />
#*** This also installs dependencies if any.<br />
#** Linux: There are several ways on linux;<br />
#*** type cpan and enter. <br />
#**** This gives us cpan prompt. <br />
#**** Type install ModuleName and it will install the module.<br />
#*** perl -MCPAN -e 'install ModuleName';<br />
# PERL & HTML<br />
#* CGI module<br />
#** To create html pages, perl provides CGI module<br />
#** It creates CGI script with require html tags:<br />
#** There are different methods which CGI modules provide to<br />
#*** add header,<br />
#*** adding fields to the page, <br />
#*** retrieving the values of the parameters posted on to the form.<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-02-12T16:04:24Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if-if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
# if-elsif-else-switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {{</nowiki> <br><nowiki>print “Key: $key & value: $value”;{</nowiki> <br><nowiki>}{</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#** this is kind of private variable.<br />
#** scope is in the block inside where it is declared.<br />
#* local<br />
#** this kind of variables are lexically scoped variables <br />
#** that means they get the temporary value inside the block where it is used <br />
#** as soon as block ends it gets the earlier value.<br />
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.<br />
#* our<br />
#** this is kind of global variables in perl<br />
#** can be accessed without giving package name while accessing it in another package.<br />
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki><br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#** In perl we can create a reference by adding \ (backward slash) to it.<br />
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’ => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki><br />
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.<br />
#* De-referencing<br />
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.<br />
#** It is done by putting appropriate identifier in front of the reference variable.<br />
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki><br />
#Special Variables in PERL<br />
#* These variables in perl have some special meaning.<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception <br />
#** or to know exactly where the code is failing with the type of error.<br />
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki><br />
#File Handling#* open a file <br />
#* open a file in read mode<br />
#* open a file in read, write mode<br />
#* open a file in append mode<br />
#* write into a file<br />
#* close the file handle<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#* require<br />
#* use<br />
#Perl Modules<br />
#PERL Module library<br />
#* CPAN<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#**WindowsLinux<br />
# PERL & HTML<br />
#* CGI module<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-02-12T06:03:50Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#:<br><br />
#: '''Loops in Perl'''<br />
# for-foreach-Loop<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
# while-do-while Loops<br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#:<br><br />
#: '''Conditional Statements'''<br />
# if-if-else Statements<br />
#* if Statement<br />
#** if statement is used to execute piece of code only if a specified condition is satisfied.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* if-else Statement<br />
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
# if-elsif-else-switch Statements<br />
#* if-elsif-else Statement<br />
#** if-elsif-else statement is used to select one of several blocks of code to be executed.<br />
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br />
#* switch Statement<br />
#** switch statement is used to select one of many blocks of code to be executed.<br />
#** There were no Switch/Case in perl prior to 5.8 version. <br />
#** After 5.8 PERL provided Switch module.<br />
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki><br />
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki><br />
#Data Structures in Perl<br />
#* Scalar<br />
#** These are the basic variables in PERL. <br />
#** It can hold any kind of type viz. string, number etc.<br />
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;<br />
#* Array<br />
#** Array in PERL is ordered collection of data. <br />
#** It can hold data of any type.<br />
#** Array index starts from zero.<br />
#** eg: @array = (1, 5, 6, ‘abc’, 7);<br />
#* Associative Array or Hash<br />
#** Associative array or Hash in PERL is un-ordered collection of data.<br />
#** It is a key value pair. <br />
#** Key cannot be duplicate in hash whereas value can be.<br />
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br />
#More on Arrays<br />
#* Getting Last index of array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki><br />
#* Getting length of an array<br />
#** To get the length, add 1 to last index of an array<br />
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki><br />
#** Other way is use scalar function on array or assign array to a scalar variable.<br />
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki><br />
#* Accessing element of an array<br />
#** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki><br />
#** prints…. “abc”<br />
#* Looping over an array<br />
#** There are two ways to loop over an array<br />
#*** Using for loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki><br />
#*** Using for-each loop<br />
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki><br />
# Basic array functions<br />
#* push<br />
#** Add element at the end of an array<br />
#* pop<br />
#** Remove element from the end of an array<br />
#* unshift<br />
#** Add element at the start of an array<br />
#* shift<br />
#** Remove element of an array from the start.<br />
#* split<br />
#** This function splits the string and makes an array of it.<br />
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello World</nowiki><br />
#* qw<br />
#** qw stands for “Quoted word”<br />
#** It returns a list of word separated by white spaces.<br />
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki> <br />
#* sort<br />
#** sorts the array in alphabatical order.<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki> <br />
#* Basic hash functions<br />
#** keys<br />
#*** Returns keys of a hash<br />
#** values<br />
#*** Returns values of a hash<br />
#** each<br />
#*** Retrieve the next key/value pair from a hash<br />
#* Looping over a hash<br />
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {{</nowiki> <br><nowiki>print “Key: $key & value: $value”;{</nowiki> <br><nowiki>}{</nowiki> <br />
#Functions in Perl<br />
#* Simple function<br />
#** Syntax:<br><nowiki>sub sample_func {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki><br />
#* Function with parameters<br />
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki> <br />
#* Function which return single value<br />
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki><br />
#* Function which returns multiple values<br />
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki><br />
#Blocks in Perl<br />
#* Begin<br />
#** This block executes at the compilation time once it is defined.<br />
#** Anything which needs to be included before execution of the rest of the code is written here.<br />
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki><br />
#* End<br />
#** This block executes at the end.<br />
#** Anything which needs to be executed at last is written here.<br />
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki><br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#* local<br />
#* our<br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#* De-referencing<br />
#Special Variables in PERL<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#File Handling<br />
#* open a file <br />
#* open a file in read mode<br />
#* open a file in read, write mode<br />
#* open a file in append mode<br />
#* write into a file<br />
#* close the file handle<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#* require<br />
#* use<br />
#Perl Modules<br />
#PERL Module library<br />
#* CPAN<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#**WindowsLinux<br />
# PERL & HTML<br />
#* CGI module<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-01-28T08:02:40Z<p>AmolBrahmankar: </p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
: '''Installation of Perl'''<br />
# Installation of Perl 5.14.2 on Ubuntu Linux<br />
#*Installing XAMPP in Linux<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)<br />
#*Default Webserver directory will be set to "opt"<br />
#:OR<br />
#*Using default Perl installation available in Synaptic Package Manager<br />
# Installation of Perl 5.14.2 on Windows<br />
#*Installing XAMPP in Windows<br />
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)<br />
#*Default Webserver directory will be set to "htdocs"<br />
#:<br><br />
#: '''Topics'''<br />
#Variables in Perl<br />
#*Variables are used for storing values, like text strings, numbers or arrays<br />
#*All variables in PERL start with a $ sign symbol<br />
#*Declaring a variable in PERL: $var_name = value;<br />
#*e.g: <br />
#**$count = 1;<br />
#**$stringVar = ‘My Name is PERL’;<br />
#Comments in Perl<br />
#*Two types of comments -<br />
#**Single Line<br />
#**Multi Line<br />
#*Single Line comment starts with the symbol #<br />
#*Multi Line comment used to comment a chunk of code<br />
#** =cut =head or =begin =end<br />
#**Start with = sign<br />
#Loops in Perl<br />
#* for Loop<br />
#** for loop is used to execute a piece of code for certain number of times<br />
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki><br />
#* for-each Loop<br />
#** for-each loop is used to iterate a condition over an array<br />
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of<br />
array</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki><br />
#* while Loop<br />
#** while loop executes a block of code while a condition is true.<br />
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki><br />
#* do-while Loop<br />
#** do-while loop will always execute the piece of code at-least once<br />
#** It will then check the condition and repeat the loop while the condition is true<br />
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki><br />
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki><br />
#Conditional Statements<br />
#* if Statement<br />
#* if-else Statement<br />
#* if-elsif-else Statement<br />
#* switch Statement<br />
#Data Structures in Perl<br />
#* Scalar<br />
#* Array<br />
#* Associative Array or Hash<br />
#More on Arrays<br />
#* Getting length of an array<br />
#* Accessing element of an array<br />
#* Looping over an array<br />
#* Basic array functions<br />
#** push<br />
#** pop<br />
#** unshift<br />
#** shift<br />
#** split<br />
#** qw<br />
#** sort<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#* Basic hash functions<br />
#** keys<br />
#** values<br />
#** each<br />
#* Looping over a hash<br />
#Functions in Perl<br />
#* Simple function<br />
#* Function with parameters<br />
#* Function which return single value<br />
#* Function which returns multiple values<br />
#Blocks in Perl<br />
#* Begin<br />
#* End<br />
<br />
==Intermediate level== <br />
: '''Topics'''<br />
#Access Modifiers in PERL<br />
#* my<br />
#* local<br />
#* our<br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#* De-referencing<br />
#Special Variables in PERL<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#File Handling<br />
#* open a file <br />
#* open a file in read mode<br />
#* open a file in read, write mode<br />
#* open a file in append mode<br />
#* write into a file<br />
#* close the file handle<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#* require<br />
#* use<br />
#Perl Modules<br />
#PERL Module library<br />
#* CPAN<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#**WindowsLinux<br />
# PERL & HTML<br />
#* CGI module<br />
<br />
==Advanced level== <br />
: '''Topics'''<br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankarhttps://script.spoken-tutorial.org/index.php/PERLPERL2013-01-12T19:12:29Z<p>AmolBrahmankar: Created page with '==Introduction to Perl== '''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a gener…'</p>
<hr />
<div>==Introduction to Perl==<br />
<br />
'''Perl''' (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.<br />
<br />
'''Perl''' can be used as a very simple and easy to use programming language.<br />
This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming.<br />
This language is portable and reliable.<br />
Useful for applications requiring Pattern Matching extensively<br />
'''<br />
__TOC__<br />
<br />
==Basic Level==<br />
# Installation of Perl<br />
#* Installation of Perl 5.14.2 on Ubuntu Linux<br />
#* Installation of Perl 5.14.2 on Windows<br />
#Variables in Perl<br />
#Comments in Perl<br />
#Loops in Perl<br />
#* for Loop<br />
#* for-each Loop<br />
#* while Loop<br />
#* do-while Loop<br />
#Conditional Statements<br />
#* if Statement<br />
#* if-else Statement<br />
#* if-elsif-else Statement<br />
#* switch Statement<br />
#Data Structures in Perl<br />
#* Scalar<br />
#* Array<br />
#* Associative Array or Hash<br />
#More on Arrays<br />
#* Getting length of an array<br />
#* Accessing element of an array<br />
#* Looping over an array<br />
#* Basic array functions<br />
#** push<br />
#** pop<br />
#** unshift<br />
#** shift<br />
#** split<br />
#** qw<br />
#** sort<br />
#More on Hash<br />
#* Accessing element of a hash<br />
#* Basic hash functions<br />
#** keys<br />
#** values<br />
#** each<br />
#* Looping over a hash<br />
#Functions in Perl<br />
#* Simple function<br />
#* Function with parameters<br />
#* Function which return single value<br />
#* Function which returns multiple values<br />
#Blocks in Perl<br />
#* Begin<br />
#* End<br />
<br />
==Intermediate level== <br />
#Access Modifiers in PERL<br />
#* my<br />
#* local<br />
#* our<br />
#Referencing & Dereferencing in Perl<br />
#* Referencing<br />
#* De-referencing<br />
#Special Variables in PERL<br />
#Sample Perl Programs<br />
#Exception and error handling in PERL <br />
#* eval block in perl<br />
#File Handling<br />
#* open a file <br />
#* open a file in read mode<br />
#* open a file in read, write mode<br />
#* open a file in append mode<br />
#* write into a file<br />
#* close the file handle<br />
#Including files and/or modules in a PERL program<br />
#* do<br />
#* require<br />
#* use<br />
#Perl Modules<br />
#PERL Module library<br />
#* CPAN<br />
#Adding path to a default list of paths for a module<br />
#* @INC<br />
#Downloading required module onto windows or linux<br />
#* Downloading CPAN module<br />
#**WindowsLinux<br />
# PERL & HTML<br />
#* CGI module<br />
<br />
==Advanced level== <br />
#Function Prototyping<br />
#Date & Time<br />
#* Current Time Stamp<br />
#* Various Date formats<br />
#* Various operations that can be performed on date<br />
#Oops in Perl<br />
#* Object creation<br />
#* Constructor<br />
#* Destructor<br />
#* Accessing methods using an object<br />
#* Inheritance in Per<br />
#Exporting functions in Perl<br />
#* EXPORT<br />
#* EXPORT_OK<br />
#Pattern Matching / Regular expression in Perl<br />
#* Basics of pattern matching<br />
#* Syntax<br />
#* Modifiers<br />
#Database handling<br />
#* DBI module<br />
#Multithreading<br />
#* threads module<br />
#Socket Programming<br />
#* use IO::Socket::INET<br />
#General information<br />
#* Getting Perl version<br />
#* Perl installation path<br />
#* Information about the <br />
#** module<br />
#** it’s location <br />
#** from where it is included in your perl script</div>AmolBrahmankar