Running Perl Scripts on Linux Systems

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

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



請為這篇文章評分?