Special Variables - Perl 101

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

Perl has rich signal handling capabilities; using the %SIG variable, you can make any subroutine run when a signal is sent to the running process. This is ... IndexHowtogetPerlTermsDocumentationStringsNumbersArraysHashesRegexesFlowControlFilesSubroutinesPODDebuggingModulesModules:MakingyourownExternalprogramsCPANConstructsReferencesObjectsSpecialVariablesCommand-lineSwitchesAdvancedFunctionsStylePerformanceTrapsEmailHowdoI...?DeveloperToolsWebsitesPublicationsCommunityRandomTODOCodeofConduct SpecialVariables $_ $_isthe"it"variable. It'softenthedefaultparmthatbuilt-infunctionsuse, orreturninto. while(<>){#Readalineinto$_ printlc;#printlc($_) } Thisisthesameas while($it=<>){ printlc($it); } $0 $0containsthenameoftheprogrambeingrun,asgiventotheshell.IftheprogramwasrundirectlythroughthePerlinterpreter,$0containsthefilename. $catfile.pl #!/usr/bin/perl print$0,"\n"; $./file.pl file.pl $perlfile.pl file.pl $perl./file.pl ./file.pl $catfile.pl|perl - $0iswhatCprogrammerswouldexpecttofindasthefirstelementoftheargvarray. @ARGV @ARGVcontainstheargumentsgiventotheprogram,asorderedbytheshell. $perl-e'printjoin(",",@ARGV),"\n"'123 1,2,3 $perl-e'printjoin(",",@ARGV),"\n"'1"23"4 1,23,4 Cprogrammersmaybeconfused,since$ARGV[0]isreallytheirargv[1].Don'tletthismistakehappentoyou! @INC @INCcontainsallthepathsthatPerlwillsearchtofindamodule. Perlprogrammersusedtoappendorprependto@INCtoaddalibrarypath;thesedays,uselibisusedinstead.Thefollowingaremostlyequivalent: BEGIN{unshift@INC,"local/lib"}; uselib"local/lib"; %ENV %ENVcontainsacopyofthecurrentenvironment.ItistheenvironmentthatwillbegiventoanysubshellcreatedbyPerl. Thisissignificantintaintmode,as%ENVwillhaveentriesthatcanaltertheshell'sbehavior.Forthatreason,perlsecrecommendsthefollowingcodebeusedpriortoexecutingacommandintaintmode: $ENV{'PATH'}='/bin:/usr/bin';#changetoyourrealpath delete@ENV{'IFS','CDPATH','ENV','BASH_ENV'}; %SIG Perlhasrichsignalhandlingcapabilities;usingthe%SIGvariable,youcanmakeanysubroutinerunwhenasignalissenttotherunningprocess. Thisisespeciallyusefulifyouhavealong-runningprocess,andwouldliketoreloadconfigurationfilesbysendingasignal(usuallySIGHUP)insteadofhavingtostartandstoptheprocess. Youcanalsochangethebehaviorofdieandwarnbyassigningto$SIG{__DIE__}and$SIG{__WARN__},respectively. <> The"diamondoperator",<>isusedwhenaprogramisexpectinginput,butisn'tconcernedhowitarrives. Iftheprogramreceivesanyarguments,theyaretakentobefilenamesandtheircontentsaresentthrough<>.Otherwise,standardinput(STDIN)isused. <>isespeciallyusefulforfilterprograms. and__DATA__ Ifaprogramcontainsthemagictoken__DATA__onalinebyitself,anythingfollowingitwillbeavailabletotheprogramthroughthemagicfilehandle. Thisisusefulifyouwanttoincludedatawithyourprogram,butwanttokeepitseparatedfromthemainprogramlogic. $! Whenrunninganycommandthatusesthesystem,$!willbetrueifthecommandreturnedanon-truestatus,orotherwisecouldnotberun.$!willcontaintheerror. $@ Ifusingeval,$@containsthesyntaxerrorthattheevalthrew,ifany. Wanttocontribute? SubmitaPRtogithub.com/petdance/perl101 ThisworkislicensedunderaCreativeCommonsAttribution-ShareAlike4.0InternationalLicense.



請為這篇文章評分?