7 of the most useful Perl command line options
文章推薦指數: 80 %
7 of the most useful Perl command line options · -v to get the Perl version · -V to get compiler details · -e execute code on the command line · -E ... Togglenavigation PerlMaven PerlTutorial Pro Login Register Books Typekeyword: Archive About 7ofthemostusefulPerlcommandlineoptions -v -e -E -p -i Perlhasalotofcommandlineoptions.Someofthemcanbeusedtodoveryusefulthings,othersaremoreesoteric. InthePerltutorialwealreadysawhowtousePerlonthecommandline, let'snowextendthatlist. -vtogetthePerlversion ThisoptionsjustshowstheversionnumberofthePerlinterpreterandtheCopyrightinformation: $perl-v Thisisperl5,version20,subversion1(v5.20.1)builtfordarwin-thread-multi-2level Copyright1987-2014,LarryWall PerlmaybecopiedonlyunderthetermsofeithertheArtisticLicenseorthe GNUGeneralPublicLicense,whichmaybefoundinthePerl5sourcekit. CompletedocumentationforPerl,includingFAQlists,shouldbefoundon thissystemusing"manperl"or"perldocperl".Ifyouhaveaccesstothe Internet,pointyourbrowserathttp://www.perl.org/,thePerlHomePage. -Vtogetcompilerdetails Besidestheversionnumber,thiscommandlineoptionwilltellPerltoprint alltheinformationabouttheplatformwearecurrentlyrunningon(Inthefollowing exampleosname=darwinmeansMacOSX. Italsoprovidestheconfigurationparameters-thewayourversionof Perlwascompiled.The sameinformationisavailableviatheConfigmoduleduringtheruntime ofourcode. The%ENVpart,ifavailable,showsthePerl-relatedenvironmentvariablesthat arecurrentlyineffect.TheseenvironmentvariablescanbesetintheUnix/Linux shellortheWindowsCommandWindow,beforerunningPerl. Finallyitliststhecontentofthe@INCarray,thatcontrolswherePerlwilllookformodules toload.Onecanchange@INCinseveralways. $perl-V Summaryofmyperl5(revision5version20subversion1)configuration: Platform: osname=darwin,osvers=13.3.0,archname=darwin-thread-multi-2level uname='darwinair.local13.3.0darwinkernelversion13.3.0:tuejun321:27:35pdt2014;root:xnu-2422.110.17~1release_x86_64x86_64' config_args='-de-Dprefix=/Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS-Dusethreads-Aeval:scriptdir=/Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/bin' hint=recommended,useposix=true,d_sigaction=define useithreads=define,usemultiplicity=define use64bitint=define,use64bitall=define,uselongdouble=undef usemymalloc=n,bincompat5005=undef Compiler: cc='cc',ccflags='-fno-common-DPERL_DARWIN-fno-strict-aliasing-pipe-fstack-protector-I/usr/local/include', optimize='-O3', cppflags='-fno-common-DPERL_DARWIN-fno-strict-aliasing-pipe-fstack-protector-I/usr/local/include' ccversion='',gccversion='4.2.1CompatibleAppleLLVM5.1(clang-503.0.40)',gccosandvers='' intsize=4,longsize=8,ptrsize=8,doublesize=8,byteorder=12345678 d_longlong=define,longlongsize=8,d_longdbl=define,longdblsize=16 ivtype='long',ivsize=8,nvtype='double',nvsize=8,Off_t='off_t',lseeksize=8 alignbytes=8,prototype=define LinkerandLibraries: ld='envMACOSX_DEPLOYMENT_TARGET=10.3cc',ldflags='-fstack-protector-L/usr/local/lib' libpth=/usr/local/lib/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.1/lib/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/usr/lib libs=-lgdbm-ldbm-ldl-lm-lutil-lc perllibs=-ldl-lm-lutil-lc libc=,so=dylib,useshrplib=false,libperl=libperl.a gnulibc_version='' DynamicLinking: dlsrc=dl_dlopen.xs,dlext=bundle,d_dlsymun=undef,ccdlflags='' cccdlflags='',lddlflags='-bundle-undefineddynamic_lookup-L/usr/local/lib-fstack-protector' Characteristicsofthisbinary(fromlibperl): Compile-timeoptions:HAS_TIMESMULTIPLICITYPERLIO_LAYERS PERL_DONT_CREATE_GVSV PERL_HASH_FUNC_ONE_AT_A_TIME_HARD PERL_IMPLICIT_CONTEXTPERL_MALLOC_WRAP PERL_NEW_COPY_ON_WRITEPERL_PRESERVE_IVUV USE_64_BIT_ALLUSE_64_BIT_INTUSE_ITHREADS USE_LARGE_FILESUSE_LOCALEUSE_LOCALE_COLLATE USE_LOCALE_CTYPEUSE_LOCALE_NUMERICUSE_PERLIO USE_PERL_ATOFUSE_REENTRANT_API Builtunderdarwin CompiledatSep16201421:55:45 %ENV: PERLBREW_BASHRC_VERSION="0.63" PERLBREW_HOME="/Users/gabor/.perlbrew" PERLBREW_MANPATH="/Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/man" PERLBREW_PATH="/Users/gabor/perl5/perlbrew/bin:/Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/bin" PERLBREW_PERL="perl-5.20.1_WITH_THREADS" PERLBREW_ROOT="/Users/gabor/perl5/perlbrew" PERLBREW_VERSION="0.63" @INC: /Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/lib/site_perl/5.20.1/darwin-thread-multi-2level /Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/lib/site_perl/5.20.1 /Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/lib/5.20.1/darwin-thread-multi-2level /Users/gabor/perl5/perlbrew/perls/perl-5.20.1_WITH_THREADS/lib/5.20.1 . -eexecutecodeonthecommandline Forone-offtasksitcanbeveryusefultobeabletorunapieceofPerlcodewithoutcreatingafile. Thecodeitselfneedstobebetweenquotes.DuetodifferencesbetweentheUnix/Linuxshell andtheMSWindowsCommandpromptweneedtousedifferentquotesaroundourcode. OnUnix/Linuxsystsem(includingMacOSX)itisrecommendedtoputourcodeinsinglequotesasinthefollowing example: $perl-e'printqq{HelloWorld\n}' HelloWorld OnMSWindowswemustusedoublequotesaroundourcode. $perl-e"printqq{HelloWorld\n}" HelloWorld Internally,itisprobablythebesttouseqandqq insteadofsingle-quoteanddouble-quote,respectively.Thatmighthelpreducetheconfusioncausedby thebehavioroftheshellandcommandprompt. -Eexecutecodeonthecommandlinewithallthelatestfeaturesenabled Sinceversion5.10ofPerlhasbeenreleased,Perlincludessomeadditional keywords(calledfeatures)inthelanguage.Forimprovedbackwardcompatibilitythesekeywordsareonly enablediftheuserexplicitlyaskforthemwithusefeature....Forexamplebywritingusefeatureqw(say);, orbydeclaringaminimalversionofPerlwithuse5.010;. Onthecommandlinewecanachievethesamebyusing-Einsteadof-e. ItwillturnonallthefeaturesoftheversionofPerlwearecurrentlyrunning. Formethemostimportantofallthesefeatures,atleastinone-linersisthe saykeywordintroducedinperl5.10. Itisjustprintwithatrailingnewlineadded.Nothingfancy,butmakestheone-linersevenshorter. Theaboveexampleswouldlooklikethese: Unix/Linux: $perl-E'sayq{HelloWorld}' HelloWorld MSWindows: $perl-E"sayq{HelloWorld}" HelloWorld Youcannoticethechangefromqqtoq.Aswedon'tneedtoincludeanewline\ninour stringswecouldswitchfromqqtoq. -nwrapthe-e/-Ecodeinawhileloop Ifweprovidethe-ncommandlineoptionitwillwrapourcodeprovidedusingeitherthe-e orthe-Eoptionsinawhilewithadiamondoperator. So perl-n-E'sayif/code/'file.txt isthesameas while(<>){ sayif/code/; } Thatwillgooverallthelinesofallthefilesprovidedonthecommandline (inthiscaseitisfile.txt)andprintouteverylinethatmatchesthe/code/regex. -pislike-nwithprint$_ The-poptionisverysimilartothe-nflag,butitalsoprintsthecontentof$_ attheendofeachiteration. Sowecouldwrite: perl-p-E's/code/foobar/'file.txt whichwouldbecome while(<>){ s/code/foobar/ print; } Thatwillprinttheresulttothescreen. -iforin-placeediting Themostcommonuseof-pistogetherwiththe-ioptionthatprovides"in-placeediting". Itmeansthatinsteadofprintingtothescreen,alltheoutputgeneratedbyourone-linerwillbewrittenbacktothesamefileitwastakenfrom. Sothisone-linerwillreplacethefirstappearanceofthestring"code"by"foobar"ineverylineofthefile"file.txt". perl-i-p-E's/code/foobar/'file.txt Screencasts Thereareanumberofscreencastsshowingsomeofthecommandlineoptions: Perlonthecommandline One-linersumofcolumninCSV AnotherarticleshowanexampleofPerlonthecommandlineusing-e,-p,-i. Writtenby GaborSzabo Publishedon2018-08-31 Comments Inthecomments,pleasewrapyourcodesnippetswithin
tagsandusespacesforindentation. commentspoweredbyDisqus Ifyouhaveanycommentsorquestions,feelfreetopostthemonthesourceofthispageinGitHub.SourceonGitHub. Commentonthispost Author:GaborSzabo GaborwhorunsthePerlMavensitehelpscompaniessetuptestautomation,CI/CD ContinuousIntegrationandContinuousDeploymentandotherDevOpsrelated systems. GaborcanhelprefactoryouroldPerlcode-base. HerunsthePerlWeeklynewsletter. ContactGaborifyou'dliketohirehisservice. BuyhiseBooksorifyoujustwouldliketosupporthim,doitviaPatreon. Perlonthecommandline(screencast)-video One-linersumofcolumninCSV-video HowtobuildperlfromsourceonLinux Howtochange@INCtofindPerlmodulesinnon-standardlocations Perlonthecommandline StringsinPerl:quoted,interpolatedandescaped @INC Thediamondoperator<>ofPerl What'snewinPerl5.10?say,//,state延伸文章資訊
- 1Linux perl command help and examples - Computer Hope
The perl command is the interpreter of the Perl programming language. Description. "Perl" officia...
- 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...
- 3How to Install Perl on Linux? - GeeksforGeeks
Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was orig...
- 4How can I call a shell command in my Perl script?
System will execute the $command with @arguments and return to your script when ... I have been u...
- 5perl linux command man page
Perl officially stands for Practical Extraction and Report Language, except when it doesn't. ... ...