Running Perl Scripts on Linux Systems
文章推薦指數: 80 %
There are many ways to run Perl scripts on Linux: 1. Run the "perl" command with the Perl script included in the command line. PerlTutorials-Herong'sTutorialExamples-v6.01,byDr.HerongYang PerlTutorials-Herong'sTutorialExamples ∟PerlonLinuxSystems ∟RunningPerlScriptsonLinuxSystems ThissectionprovidesatutorialexampleonhowtorunPerlscriptsonLinuxsystems.TomakeaPerlscriptfileexecutable,youneedtoadd'#!/usr/bin/perl'tothebeginningofthescript. TherearemanywaystorunPerlscriptsonLinux: 1.Runthe"perl"commandwiththePerlscriptincludedinthecommandline. Forexample,enterthefollowingcommandlineinashellwindow: /home/herong$perl-e"print'Helloworld!';" Helloworld! Notethataboutcommandwillnotworkinsomeshellwindows. Forexample,ina"bash"shellwindow,youwillgetthefollowingerror, becausethe"!"isareservedcharactertoaccessaneventinthecommandhistory. /home/herong$perl-e"print'Helloworld!';" -bash:!':eventnotfound Toavoidtheproblem,youcanruntheabovecommanda"sh"commandwindow: /home/herong$sh $perl-e"print'Helloworld!';" Helloworld!$ Oryoucanprotect"!"usingescapesequence: /home/herong$perl-e"print\"Helloworld\!\n\";" Helloworld! Oryoucanusesinglequotes('...')toprotecttheentirePerlscript fromtheshell: /home/herong$perl-e'print"Helloworld!\n";' Helloworld! HereisanotherexampleofrunningaPerlscriptinasinglecommandline: /home/herong$perl-e"for(\$i=0;\$i<10;\$i++){print\"\$i\n\";}" 0 1 2 3 4 5 6 7 8 9 Notethatdollarsign($)usedforPerlscalarvariables arealsoshellreservedcharacters,weneedtouseescapesequencestoprotectthem. Alsonotethatdoublequote(")isusedtoputtheentirescriptcodeasone commandlineparameter.Anydoublequoteinsidetheprogramneedstobeprotected as(\"). Oryoucanusesinglequotes('...')toprotecttheentirePerlscript fromtheshell: /home/herong$perl-e'for($i=0;$i<10;$i++){print"$i\n";}' 0 1 2 3 4 5 6 7 8 9 IncludingPerlscriptinthecommandlineisquickandeasy. Butyoucanonlyrunscriptsthataresmallenoughtofitintoonecommandline. 2.Runthe"perl"commandwiththePerlscriptsuppliedfromthestandardinputstream. Forexample,enter"perl"inacommandwindow. ThenenterthescriptcodefollowedbyControl-D,whichistheEndOfFile (EOF)indicator: /home/herong$perl $s=0; for($i=0;$i<10;$i++){ $s+=$i; } print"$s\n"; ^D 45 Obviously,youcanenteramuchlongerscriptinthisway.Buttheprogramisnot savepermanently. 3.Runthe"perl"commandwiththePerlscriptsuppliedinafile. Forexample,enterthefollowingPerlscriptinafilecalledhello.prg: print"Helloworld!\n"; Thenenterthefollowingcommandinacommandwindow: /home/herong$perlhello.prg Helloworld! 4.RunPerlscriptfilesascommands.Youcandothis,onlyifyouinsert aspeciallineatthebeginningofyourscriptfile:#!/usr/bin/perl,and assignexecutionpermissiontothescriptfile.Thisspeciallinerepresents thePerlinstallationlocationonthefilesystem. Forexample,enterthefollowingscriptinafilecalledhello.pl: #!/usr/bin/perl print"Helloworld!\n"; Thenassignexecutionpermissionandenterthescriptfilenametorunit: /home/herong$chmoda+xhello.pl /home/herong$./hello.pl Helloworld! Itworks! AndthisisthebestwaytorunPerlscriptsonLinuxsystems. TableofContents AboutThisBook ►PerlonLinuxSystems PerlInstallationonLinuxSystems ►RunningPerlScriptsonLinuxSystems ActivePerlonWindowsSystems DataTypes:ValuesandVariables Expressions,OperationsandSimpleStatements UserDefinedSubroutines PerlBuilt-inDebugger NameSpacesandPerlModuleFiles Symbolic(orSoft)References HardReferences-AddressesofMemoryObjects Objects(orReferences)andClasses(orPackages) TypeglobandImportingIdentifiersfromOtherPackages StringBuilt-inFunctionsandPerformance FileHandlesandDataInput/Output OpenFilesinBinaryMode OpenDirectoriesandReadFileNames FileSystemFunctionsandOperations ImageandPictureProcessing UsingDBMDatabaseFiles UsingMySQLDatabaseServer SocketCommunicationOvertheInternet XML::SimpleModule-XMLParserandGenerator XMLCommunicationModel SOAP::Lite-SOAPServer-ClientCommunicationModule PerlProgramsasIISServerCGIScripts CGI(CommonGatewayInterface) XML-RPC-RemoteProcedureCallwithXMLandHTTP RPC::XML-PerlImplementationofXML-RPC IntegratingPerlwithApacheWebServer CGI.pmModuleforBuildingWebPages LWP::UserAgentandWebSiteTesting ConvertingPerlScripttoExecutableBinary ManagingPerlEngineandModulesonmacOS ArchivedTutorials References FullVersioninPDF/EPUB RunningPerlScriptsonLinuxSystems-Updatedin2022,byDr.HerongYang
延伸文章資訊
- 1Running Perl Scripts on Linux Systems
There are many ways to run Perl scripts on Linux: 1. Run the "perl" command with the Perl script ...
- 2How 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...
- 3perl linux command man page
Perl officially stands for Practical Extraction and Report Language, except when it doesn't. ... ...
- 4Perl執行shell命令的幾種方式及其區別 - 程式人生
There are many ways to execute external commands from Perl. The most commons are: system function...
- 5How to Install Perl on Linux? - GeeksforGeeks
Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was orig...