learn_perl_by_example(1).pdf
(
144 KB
)
Pobierz
LearnPerlbyExample-PerlHandbook
forBeginners-BasicsofPerlScripting
Language
www.freebsdonline.com
Copyright©2006-2008www.freebsdonline.com
2008/01/29
ThiscourseisaboutPerlProgrammingLanguage.ItisforbeginnersanditexplainPerl
basicsinaeasytolearnway.IfyouareasysadminandyoulearnLinuxorUNIXthisis
whatyouneedtobeabletowritePerlscripts,toknowalanguageeverysysadminmust
know.
PERLisapowerfulscriptinglanguage,verypopularamongUNIX/Linuxadmins.This
tutorialswilltrytocovereverythingyouneedtoknowinordertoprograminPerl.Perl
standsforPracticalExtractionanReportLanguage,itwasfirstusedastextprocessor,it
borrowsfeaturesfromC,shellscripting(UNIXsh),sed,awk,Lisp,Pascal.Itcanbeused
alsofordevelopingdyamicwebapplicationsasCGIs.
Thistutorialwasprovidedbyhttp://www.freebsdonline.com
Youmayfreelydistributethisdocumentinanyformwithoutchangingthetextor
removingcopyrightnotice.
1
LearnPerlbyExample-PerlHandbookforBeginners-BasicsofPerlScriptingLanguage
TableofContents
1Introduction.................................................................................................................................2
2PerlVariables..............................................................................................................................3
3Perlcontrolstructures................................................................................................................7
4Definingandusingsubroutines................................................................................................10
5Usingfileparameters(positionalparameters).......................................................................11
6PerlRegularExpressions.........................................................................................................11
7AboutTutorial...........................................................................................................................15
1Introduction
PERLisapowerfulscriptinglanguage,verypopularamongUNIX/Linuxadmins.Thistutorials
willtrytocovereverythingyouneedtoknowinordertoprograminPerl.Perlstandsfor
PracticalExtractionanReportLanguage,itwasfirstusedastextprocessor,itborrowsfeatures
fromC,shellscripting(UNIXsh),sed,awk,Lisp,Pascal.Itcanbeusedalsofordeveloping
dyamicwebapplicationsasCGIs.
1.1FewthingstoknowbeforestartprogramminginPerl
Perlcodeisportable.Mostscriptsarewrittenforversion5.8orhigher.Whenstartprogramming
inPerlfirstyoumightwanttofindthepathofPerlbinary.OnmostUNIX/Linuxsystemsyoucan
dothatwithwhereiscommand:
whereisperl
Asaresultofthiscommandyoumighthave:/usr/bin/perl.Soyourscriptsmusthavefirstlineof
codewiththisvalue:
#!/usr/bin/perl
#restofcode#willbeusedforcomments.
Onfirstlineofsourcecodethislinewillhelpshellinfindingwhatbinarytousewhenrunningthe
script.
2
LearnPerlbyExample-PerlHandbookforBeginners-BasicsofPerlScriptingLanguage
Inordertoproperlyrunthescriptyoursourcecodemustalsohaveexecutableflagfortheuser
youusetorunthescript.Thiscanbeachievedforexampleforfileprog1.pladdingexecutable
flagfromcommandline:
chownu+xprogr1.pl
So,tostartyourPerlscriptyouwillfollow:
a)Createanemptyfile:
touchprogram.pl
b)Addexecutableflagtothatfile:
chownu+xprogram.pl
c)FindwhereyourPerlbinaryis:
whereisperl
(youwillgetsomethinglike/usr/bin/perl).
c)Editthatfilewithyourtexteditor,andaddperlpathwiththissyntax:#!/usr/bin/perl(notthat#
onfirstlineofyourcodewillnotbeseenascomment)
editprogram.pl
andputthere:#!/usr/bin/perl.
Note:
ForLinuxyoucanusenano,picoormcedit.EditisyourdefaulttexteditorinFreeBSD.Ifyouhaveinstalled
MidnightCommanderpackage,youcanusemcedit,whichisnice.
Note:usestrict;
putinperlsourcecodewillforceustodeclarevariablesinamoresafe(proper)way.All
variablesmustbedeclaredwithmyprefix.
Note:
Byadding-wto#!/usr/bin/perlitwillactivateperlwarnings,veryusefullfordebugging.
#!/usr/bin/perl-w
3
LearnPerlbyExample-PerlHandbookforBeginners-BasicsofPerlScriptingLanguage
2PerlVariables
Perlhas3typesofvariables:
•
scalars;
•
arrays;
•
hashes;
2.1Scalars
ExampleonhowtodefineascalarvariableinPerl:
$var1="value" #ascalarvariablevar1isdefinedandastring
#"value"isassignedtothatvariable;
$var2=100 #ascalarvariablevar2isdefined,andan
#integervalueisassigned.
Example:Toprintascalarvaluewewilluse:
print"$var1";
2.2Arrays
ExampleonhowtodefineanarrayinPerl:
@array1=("Value1","Value2",Value3");
Exampleonhowtoprintanarray:
print"Ourarrayvariablecontains:@array1\n";
Inourexamplewe’veused\nescapechartoinsertanewline(escapecharscanbeusedthesame
wayareusedinClanguage).
Thepreviousexamplewilldisplayallvaluesfromarray1array.
Todisplayoneelementofthearray:
4
LearnPerlbyExample-PerlHandbookforBeginners-BasicsofPerlScriptingLanguage
print"Firstelementofthearrayis:$array1[0]";
Asyoumightnoticewe’vedefinedarraywith@butprintedasinglevalueofthatarrayusing$.
Thisiscorrectbecausewewanttoprintasinglevalue.
Itisalsopossibletoprintmultiplevaluesfromanarray:
print"Ourarraycontains:@array1[0..2]";
Previousexamplewillprintelementsfrom0toelementnr.2fromarray1.
Youcanalsoprintmultipledistinctelementsfromarray:
print"Ourarraycontains:@array1[0,4,7]";
Thepreviousexamplewillprintonlyvaluesforelement0,4and7.Notethatinperlfirstvalueof
anarrayisnumber0.
Thatisfinebuthowdowefindthenumberofelementsofanarray?
print"Numberofelementsofanarray:$#array1";
Notethat$#array1inourexampleisnumberofelements,butbecauseelementsfromanarrayin
Perlstartswithvalue0,therealnumberofelementsofanarrayis$#array+1.
Thereisanothermethodtodefineanarray:
@array2=qw(Value1Value2Value3Value4);
Perlfunctionsforworkingwitharrays:
•
pop-removelastelementofanarray:
•
push-addanelementtotheendofarray;
•
shift-removesfirstelementofanarray;
•
unshift-addanelementtothebeginningofarray;
5
Plik z chomika:
michalr1n
Inne pliki z tego folderu:
pixel.7z
(28 KB)
winscp554source.zip
(9845 KB)
os.7z
(80071 KB)
xine.tar(2).gz
(31592 KB)
mpfrontend-code-4-trunk(1).zip
(80 KB)
Inne foldery tego chomika:
Pliki dostępne do 01.06.2025
Pliki dostępne do 19.01.2025
! Crystal Reports
! HTML CSS XHTML
█ STRZELANKI
Zgłoś jeśli
naruszono regulamin