Perl is a fairly straightforward , widely cognise and well - prize scripting nomenclature . It is used for a multifariousness of tasks ( for example , you could use it to create the equivalent of DOS heap files or C scale script ) , but in the context of Web development it is used to developCGI scripts .
One of the nice things about Perl is that , because it is a scripting language , people give awaysource codefor their program . This gives you the chance to memorize Perl by example , and you’re able to also download and modify thousands of Perl playscript for your own economic consumption . One of the sorry thing about Perl is that much of this free computer code is impossible to understand . Perl lends itself to an improbably cryptic fashion !
This article sham that you already cognize how to program ( if you know theC scheduling terminology , this will be especially easy for you ) . Perl is well-to-do to use once you know the basics . In this article , we ’re lead to set about at the beginning and show you how to do the most common programming tasks using Perl . By the end of this clause , you will be capable to write your own Perl scripts with relative ease , and read cryptic scripts spell by others with somewhat less comfort , but this will be a safe start point .
Getting Started
To start with Perl you call for thePerl interpreter . On any UNIX motorcar there is a 99.99 - percentage probability that it ’s already there . On a Windows machine or a Mac , you need to download the in vogue button of the voice communication and put in it on your auto . ( See the links at the last of this article for more information . ) Perl is widely uncommitted on the Web and is innocent .
Next , ensure you take care in theDOCS directorythat comes with Perl – there will be user - manual - type material in there . At some point , it would not hurt to read through all of the documentation , or at least read it . Initially it will be cumbersome , but after reading this article it will make much more signified .
Hello World
Once you have Perl dilute , make trusted you have your path right set to include the Perl executable . Then , open a text editor and make a text file . In the file , place the following line :
Name the data file " test1.pl " . At the command prompt , type :
Perl will be given and carry out the computer code in the text file . You should see the words " Hello World ! " print to stdout ( standard out ) . As you could see , it is extremely loose to make and range programs in Perl . ( If you are using UNIX , you could aim a comment like # ! /usr / bin / perlon the first line , and then you will not have to type the word " perl " at the command parentage . )
Theprintcommand print things to stdout . The\nnotation is a ancestry provender . That would be more clear if you modify the test program to look like this ( # denotes a comment ):
mention that the print command interpret that it should interpret the " \n " as a transmission line provender and not as the real characters . The interpretation occurred not because of the mark command , but because of the use of double citation ( a practice calledquotingin Perl ) . If you were to use single quotes instead , as in :
the \n reference would not be interpreted but instead would be used literally .
There is also the backquote character : ` . A twain of these imply that what is inside the quote should be interpret as anoperating systemcommand , and that command should be executed with the output of the command being printed . If you were to place inside the backquotes a dictation - phone line functioning from the operating system , it would execute . For example , on Windows NT you’re able to say :
to start the DIR instruction and see a lean of files from the current directory .
You will also see the / eccentric used for quoting regular expressions .
The print command understandscommasas separators . For example :
However , you will also see aperiod :
The flow is actually astring concatenationoperator .
There is also aprintfoperator for C folks .
PERL Note
InWindows NT , you’re able to not say :
because dir is not a freestanding practicable – it ’s part of the command interpreter cmd . Typecmd /?at a DOS prompt for details .
Variables
Variables are interesting in Perl . You do not declare them , and you always employ a$to denote them . They come into existence at first usage . For lesson :
Or :
Since.is strand chain , .= has the expect meaning in the same way that " + = " does in C. Therefore , you could say :
you’re able to also createarrays :
The$#notation gets the highest index in the array , tantamount to the number of element in the array minus 1 . As in C , all array start indexing at zero .
you could also createhashes :
Here , ' bark ' is colligate with the word ' bounder ' , ' meow ' with ' cat ' , and so on . A more expressive syntax for the same contract is :
The=>operator quotes the left string and acts as a comma butterfly .
Loops and Ifs
you’re able to produce a simpleforloop like you do in coulomb :
Whilestatements are soft :
Ifstatements are similarly easy :
Theboolean operatorswork like they do in carbon :
For numbers :
Others :
If you have an raiment , you could curl through it easily withforeach :
Foreach takes each element of the raiment @a and places it in $ B vitamin until @a is exhausted .
You must use the " begin " and " end"braces–{and}– even for a unmarried line of descent .
Functions
You make asubroutinewith the wordsub . All variables passed to the subroutine arrive in an array call _ . Therefore , the following computer code works :
commend that $ # returns the highest index in the array ( the number of elements minus 1 ) , so$#_is the number of parameters minus 1 . If you like that sorting of dullness , then you will love PERL .
you may declarelocal variables in a subroutinewith the wordlocal , as in :
you could also call afunctionusing & , as in :
The & symbolisation is required only when there is equivocalness , but some programmers apply it all the time .
Toreturn a value from a subroutine , use the keywordreturn .
Reading
Reading from STDIN
To read in data from the stdin ( standard in ) , use theSTDINhandle . For example :
As long as you enter anintegernumber , this programme will process as expected. read a line at a sentence . you could also usegetcto read onecharacter , as in :
Or use read :
The 1 in the third parametric quantity to the read command is the length of the comment to read .
Reading Environment Variables
PERL limit a global hasheesh namedENV , and you’re able to habituate it toretrieve the values of surroundings variable . For object lesson :
Reading Command Line Arguments
PERL determine a global arrayARGV , which contain any command product line argument fall out to the script . $ # ARGV is the number of statement passed minus 1 , $ ARGV[0 ] is the first contestation passed , $ ARGV[1 ] is the second , and so on .
You should now be capable to read and write simple Perl scripts . You should also be able to wade into the full documentation to learn more . For extra information , see the links on the next Sir Frederick Handley Page .