Perl - Introduction to the Command Line - FLOSS Manuals (en)
文章推薦指數: 80 %
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
延伸文章資訊
- 1Perl執行shell命令的幾種方式及其區別 - 程式人生
There are many ways to execute external commands from Perl. The most commons are: system function...
- 27 of the most useful Perl command line options
7 of the most useful Perl command line options · -v to get the Perl version · -V to get compiler ...
- 3How to Install Perl on Linux? - GeeksforGeeks
Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was orig...
- 4How to Run Perl Scripts in Linux Command Line
Learn to learn Perl scripts in Linux command line. Also learn a few tricks about running Perl com...
- 5Perl - Introduction to the Command Line - FLOSS Manuals (en)
Perl is a programming language that can be used to perform tasks that would be difficult or cumbe...