Perl - Introduction to the Command Line - FLOSS Manuals (en)

文章推薦指數: 80 %
投票人數:10人

Perl is a programming language that can be used to perform tasks that would be difficult or cumbersome on the command line. Perl is included by default with ... IntroductiontotheCommandLine Perl Perlisaprogramminglanguagethatcanbeusedtoperformtasksthatwouldbedifficultorcumbersomeonthecommandline.PerlisincludedbydefaultwithmostGNU/Linuxdistributions.Usually,oneinvokesPerlbyusingatexteditortowriteafileandthenpassingittotheperlprogram. Perlscriptscanbenamedanythingbutconventionallyendwith".pl".Youcanuseanytexteditortocreatethisfile--Emacs,Vim,Gedit,orwhateveryourfavoriteis.Ascriptcouldlooklikethis: my$a=1+2; print$a,"\n"; Inthisexample,wecreateavariable(byusingmy)whichiscalled$a(thedollarsignisPerl'swayofdenotingavariable),whichstorestheresultof"1+2".Itthenusestheprintfunctiontoprinttheresult,whichshouldbe3.Thecommaconcatenatestwoormorestringstogether.Inthiscaseanewlineisappendedtotheendoftheprintedstring.AllstatementsinPerlareterminatedwithasemicolon,eveniftheyareonseparatelines.Ifwesavethisfileasfirst.pl,wecanrunitfromthecommandline. $perlfirst.pl 3 ThePerlprogramprintedout"3",justlikeweexpected.Ifwedon'twanttotype"perl"inordertorunthescript,wecanputthisline: #!/usr/bin/perl atthestart(besuretousethecorrectpathonyoursystem),anddochmod+xfirst.pltomakeitexecutable.Thenwetype./first.pltorunit. Ofcourse,wecanusePerltodomoreusefulthings.Forexample,wecanlookatallthefilesinthecurrentdirectory. my$filename; opendirDH,"."ordie"Couldnotopendirectory!"; while($filename=readdir(DH)){ print$filename,"\n"; } $perlfirst.pl perl-script.perl other-script.perl document.odt photo.png Hereweusetheopendirfunctiontoopenadirectoryforreading."DH",willbeourdirectoryhandle,howwerefertotheopendirectoryforreading.Adirectoryhandleisnotdeclaredlikeavariable,justcreatedattheinvocationoftheopendirfunction.Wepassthedirectorynameasastring(enclosedindoublequotes);thesingledotreferstothecurrentdirectory.TheoranddiestatementstellPerltoterminateexecutionifthedirectorycannotbeopened.ThefinalstringtellsPerlwhattoprintasanerrormessage. Insidethewhileloop,thereaddirfunctiontakesourdirectoryhandleandreturnsthenextfilenameinthedirectory,storingitinthedefaultvariable"$_". Let'strydoingsomethingwiththesefiles--here'sawaytofindallofthe".pl"filesinadirectory. opendirDH,"."ordie"Couldnotopendirectory!"; while($_=readdir(DH)){ print$_,"\n"if/.pl$/; } AboveweuseaPerlshorthandtocompresstheprintandevaluationintoonelinesincebothprintandiftakethedefaultvariable"$_"astheirargumentifoneisnotspecified.The"/.pl$/"operatorsays:matchanywordthatendswith".pl".Belowisasimplerbutwordierwaytopickouttheallofthefileswith".txt"inthem. my$filename; opendirDH,"."ordie"Couldnotopendirectory!"; while($filename=readdir(DH)){ if($filename=~/.txt$/){ print$filename,"\n"; } } Perlusesbraces({})togroupstatementsinbranchingandloopconstructs,suchasifandwhile.The"=~"operatortellsif,yes,when".txt"appearsattheendofthevariableorstring. WecanalsousecommandlinecodeinPerlbyusingthesystemfunction.Forexample,ifwewantedtodeleteallofthe".txt"files,wecoulduse. my$dir="./"; system("rm$dir*.txt"); Above,systempassesitsargumenttoashell,whichexecutesitexactlyasitwouldifwetypeditin.Nowifwelookforanyfilesendingin,".txt"wewon'tfindany. system("ls*.txt"); MoreinformationaboutPerl ThePerlwebsiteathttp://www.perl.orgcontainsanimpressiveamountofinformationanddocumentationaboutthePerllanguage. INTRODUCTION INTRODUCTION ABOUTTHISMANUAL BASICS GETTINGSTARTED BEGINNINGSYNTAX MOVINGAROUND COMMANDS BASICCOMMANDS STANDARDFILES CUTDOWNONTYPING SUPERUSERS REDIRECTION ADVANCED-ISH MULTIPLEFILES SEARCHINGFORFILES PIPING PROCESSES FILESTRUCTURE COMMANDHISTORY ADVANCED PERMISSIONS INTERACTIVEEDITING CHECKINGEXIT SUBCOMMANDS MOVINGAGAIN CUSTOMISATION PARAMETERSUBSTITUTION GNUSCREEN SSH INSTALLINGSOFTWARE MAKINGYOUROWNINTERPRETER TEXTEDITORS TEXTEDITORS NANO VIM EMACS KEDIT GEDIT SCRIPTING SCRIPTING MAINTAININGSCRIPTS OTHERLANGUAGES SED AWK REGULAREXPRESSIONS SCRIPTINGLANGUAGES PERL PYTHON RUBY GNUOCTAVE APPENDICES GLOSSARY COMMANDQUICKIE OUTLINE CREDITS



請為這篇文章評分?