PERL -- Options

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

If there are no digits, the null character is the separator. ... If -e is given, perl will not look for a script filename in the argument list. Options Note:onfirstreadingthissectionmaynotmakemuchsensetoyou.It'shere atthefrontforeasyreference. Asingle-characteroptionmaybecombinedwiththefollowingoption,ifany. Thisisparticularlyusefulwheninvokingascriptusingthe#!constructwhich onlyallowsoneargument.Example: #!/usr/bin/perl-spi.bak #sameas-s-p-i.bak ... Optionsinclude: -0digits specifiestherecordseparator($/)asanoctalnumber. Iftherearenodigits,thenullcharacteristheseparator. Otherswitchesmayprecedeorfollowthedigits. Forexample,ifyouhaveaversionof find whichcanprintfilenamesterminatedbythenullcharacter,youcansaythis: find.-name'*.bak'-print0|perl-n0eunlink Thespecialvalue00willcausePerltoslurpfilesinparagraphmode. Thevalue0777willcausePerltoslurpfileswholesincethereisno legalcharacterwiththatvalue. -a turnsonautosplitmodewhenusedwitha -n or -p. Animplicitsplitcommandtothe@Farray isdoneasthefirstthinginsidetheimplicitwhileloopproducedby the -n or -p. perl-ane'printpop(@F),"\n";' isequivalentto while(<>){ @F=split(''); printpop(@F),"\n"; } -c causes perl tocheckthesyntaxofthescriptandthenexitwithoutexecutingit. -d runsthescriptundertheperldebugger. SeethesectiononDebugging. -Dnumber setsdebuggingflags. Towatchhowitexecutesyourscript,use -D14. (Thisonlyworksifdebuggingiscompiledintoyour perl.) Anothernicevalueis-D1024,whichlistsyourcompiledsyntaxtree. And-D512displayscompiledregularexpressions. -ecommandline maybeusedtoenteronelineofscript. Multiple -e commandsmaybegiventobuildupamulti-linescript. If -e isgiven, perl willnotlookforascriptfilenameintheargumentlist. -iextension specifiesthatfilesprocessedbythe<>constructaretobeedited in-place. Itdoesthisbyrenamingtheinputfile,openingtheoutputfilebythe samename,andselectingthatoutputfileasthedefaultforprintstatements. Theextension,ifsupplied,isaddedtothenameofthe oldfiletomakeabackupcopy. Ifnoextensionissupplied,nobackupismade. Saying"perl-p-i.bak-e"s/foo/bar/;"..."isthesameasusing thescript: #!/usr/bin/perl-pi.bak s/foo/bar/; whichisequivalentto #!/usr/bin/perl while(<>){ if($ARGVne$oldargv){ rename($ARGV,$ARGV.'.bak'); open(ARGVOUT,">$ARGV"); select(ARGVOUT); $oldargv=$ARGV; } s/foo/bar/; } continue{ print; #thisprintstooriginalfilename } select(STDOUT); exceptthatthe -i formdoesn'tneedtocompare$ARGVto$oldargvtoknowwhen thefilenamehaschanged. Itdoes,however,useARGVOUTfortheselectedfilehandle. Notethat STDOUT isrestoredasthedefaultoutputfilehandleaftertheloop. Youcanuseeoftolocatetheendofeachinputfile,incaseyouwant toappendtoeachfile,orresetlinenumbering(seeexampleundereof). -Idirectory maybeusedinconjunctionwith -P totelltheCpreprocessorwheretolookforincludefiles. Bydefault/usr/includeand/usr/lib/perlaresearched. -loctnum enablesautomaticline-endingprocessing.Ithastwoeffects: first,itautomaticallychopsthelineterminatorwhenusedwith -n or -p, andsecond,itassigns$\tohavethevalueof octnum sothatanyprintstatementswillhavethatlineterminatoraddedbackon.If octnum isomitted,sets$\tothecurrentvalueof$/. Forinstance,totrimlinesto80columns: perl-lpe'substr($_,80)=""' Notethattheassignment$\=$/isdonewhentheswitchisprocessed, sotheinputrecordseparatorcanbedifferentthantheoutputrecord separatorifthe -l switchisfollowedbya -0 switch: gnufind/-print0|perl-ln0e'print"found$_"if-p' Thissets$\tonewlineandthensets$/tothenullcharacter. -n causes perl toassumethefollowinglooparoundyourscript,whichmakesititerate overfilenameargumentssomewhatlike"sed-n"orawk: while(<>){ ... #yourscriptgoeshere } Notethatthelinesarenotprintedbydefault. See -p tohavelinesprinted. Hereisanefficientwaytodeleteallfilesolderthanaweek: gfind.-mtime+7-print|perl-nle'unlink;' Thisisfasterthanusingthe-execswitchoffindbecauseyoudon'thaveto startaprocessoneveryfilenamefound. -p causes perl toassumethefollowinglooparoundyourscript,whichmakesititerate overfilenameargumentssomewhatlikesed: while(<>){ ... #yourscriptgoeshere }continue{ print; } Notethatthelinesareprintedautomatically. Tosuppressprintingusethe -n switch. A -p overridesa -n switch. -P causesyourscripttoberunthroughtheCpreprocessorbefore compilationby perl. (Sincebothcommentsandcppdirectivesbeginwiththe#character, youshouldavoidstartingcommentswithanywordsrecognized bytheCpreprocessorsuchas"if","else"or"define".) -s enablessomerudimentaryswitchparsingforswitchesonthecommandline afterthescriptnamebutbeforeanyfilenamearguments(orbeforea--). Anyswitchfoundthereisremovedfrom@ARGVandsetsthecorrespondingvariableinthe perl script. Thefollowingscriptprints"true"ifandonlyifthescriptis invokedwitha-xyzswitch. #!/usr/bin/perl-s if($xyz){print"true\n";} -S makes perl usethePATHenvironmentvariabletosearchforthescript (unlessthenameofthescriptstartswithaslash). Typicallythisisusedtoemulate#!startuponmachinesthatdon't support#!,inthefollowingmanner: #!/usr/bin/perl eval"exec/usr/bin/perl-S$0$*" if$running_under_some_shell; Thesystemignoresthefirstlineandfeedsthescriptto/bin/sh, whichproceedstotrytoexecutethe perl scriptasashellscript. Theshellexecutesthesecondlineasanormalshellcommand,andthus startsupthe perl interpreter. Onsomesystems$0doesn'talwayscontainthefullpathname, sothe -S tells perl tosearchforthescriptifnecessary. After perl locatesthescript,itparsesthelinesandignoresthembecause thevariable$running_under_some_shellisnevertrue. Abetterconstructthan$*wouldbe${1+"$@"},whichhandlesembeddedspaces andsuchinthefilenames,butdoesn'tworkifthescriptisbeinginterpreted bycsh. Inordertostartupshratherthancsh,somesystemsmayhavetoreplacethe #!linewithalinecontainingjust acolon,whichwillbepolitelyignoredbyperl. Othersystemscan'tcontrolthat,andneedatotallydeviousconstructthat willworkunderanyofcsh,shorperl,suchasthefollowing: eval'(exit$?0)'&&eval'exec/usr/bin/perl-S$0${1+"$@"}' &eval'exec/usr/bin/perl-S$0$argv:q' if0; -u causes perl todumpcoreaftercompilingyourscript. Youcanthentakethiscoredumpandturnitintoanexecutablefile byusingtheundumpprogram(notsupplied). Thisspeedsstartupattheexpenseofsomediskspace(whichyoucan minimizebystrippingtheexecutable). (Still,a"helloworld"executablecomesouttoabout200Konmymachine.) Ifyouaregoingtorunyourexecutableasaset-idprogramthenyou shouldprobablycompileitusingtaintperlratherthannormalperl. Ifyouwanttoexecuteaportionofyourscriptbeforedumping,usethe dumpoperatorinstead. Note:availabilityofundumpisplatformspecificandmaynotbeavailable foraspecificportofperl. -U allows perl todounsafeoperations. Currentlytheonly"unsafe"operationsaretheunlinkingofdirectorieswhile runningassuperuser,andrunningsetuidprogramswithfataltaintchecks turnedintowarnings. -v printstheversionandpatchlevelofyour perl executable. -w printswarningsaboutidentifiersthatarementionedonlyonce,andscalar variablesthatareusedbeforebeingset. Alsowarnsaboutredefinedsubroutines,andreferencestoundefined filehandlesorfilehandlesopenedreadonlythatyouareattemptingto writeon. Alsowarnsyouifyouuse==onvaluesthatdon'tlooklikenumbers,andif yoursubroutinesrecursemorethan100deep. -xdirectory tells perl thatthescriptisembeddedinamessage. Leadinggarbagewillbediscardeduntilthefirstlinethatstarts with#!andcontainsthestring"perl". Anymeaningfulswitchesonthatlinewillbeapplied(butonlyone groupofswitches,aswithnormal#!processing). Ifadirectorynameisspecified,Perlwillswitchtothatdirectory beforerunningthescript. The -x switchonlycontrolsthethedisposalofleadinggarbage. Thescriptmustbeterminatedwith__END__ifthereistrailinggarbage tobeignored(thescriptcanprocessanyorallofthetrailinggarbage viatheDATAfilehandleifdesired).



請為這篇文章評分?