Special Variables - Perl 101
文章推薦指數: 80 %
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.
延伸文章資訊
- 1Perl Special Variables - CodesCracker
Special variables are predefined having special meaning in perl. The special variables in perl, u...
- 2Perl's Special Variables
One of the best ways to make your Perl code look more like … well, like Perl code – and not like ...
- 3Special Variables - Perl 101
Perl has rich signal handling capabilities; using the %SIG variable, you can make any subroutine ...
- 4Special Variables - PERL - kichwa.com
$], The first part of the string printed out when you say perl -v. It can be used to determine at...
- 5What are special variables in Perl? - Educative.io
These special variables are usually represented by using punctuation symbols right after a variab...