Using 'Shift' in subroutine argument - PerlMonks

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

Dear monks, I am going through a Perl script where I found. mysub shift, shift;. [download]. where mysub is subroutine name. Clearquestionsandrunnablecodegetthebestandfastestanswer   PerlMonks   Using'Shift'insubroutineargument byAnonymousMonk Log inCreate a new user The Monastery Gates Seekers of Perl Wisdom Meditations Cool Uses For Perl Obfuscation Tutorials Poetry Reviews Perl News Donate Recent Threads Newest Nodes Super Search PerlMonks Discussion What's New onNov02,2007at10:29 UTC (#648616=perlquestion:printw/replies,xml) NeedHelp?? AnonymousMonkhasaskedforthewisdomofthePerlMonksconcerningthefollowingquestion: Dearmonks, IamgoingthroughaPerlscriptwhere Ifound mysubshift,shift; [download] wheremysubissubroutinename. Pleaseexplainwhathowargumentsarebeingpassed throughshiftitself.AsfarasIknowshiftisusedto gettheveryfirstelementofanarray.ButhereIcantsee anyarray.Arethese'shift's'takingvaluefromcommandlines?? Plhelp.CommentonUsing'Shift'insubroutineargumentDownloadCode Repliesarelisted'BestFirst'. Re:Using'Shift'insubroutineargument bymerlyn(Sage)onNov02,2007at10:49 UTC perldoc-fshifttellsus: shiftARRAY shiftShiftsthefirstvalueofthearrayoffandreturnsit, shorteningthearrayby1andmovingeverythingdown.Ift +here arenoelementsinthearray,returnstheundefinedvalue. +If ARRAYisomitted,shiftsthe@_arraywithinthelexicals +cope ofsubroutinesandformats,andthe@ARGVarrayatfilesc +opes orwithinthelexicalscopesestablishedbythe"eval''", "BEGIN{}","INIT{}","CHECK{}",and"END{}"constructs +. [download] Inthiscase,it'slikelythatit'sbeingusedwithinanothersubroutine,soit'sshiftingthe@_array. --RandalL.Schwartz,Perlhacker [reply][d/l] Re^2:Using'Shift'insubroutineargument bynarainhere(Monk)onNov02,2007at11:31 UTC Ifyouexecutethis usestrict; usewarnings; one(1,2,3,4); subone{ &two(shift,shift); } subtwo { print"Elements:\n@_\n"; } [download] youwouldget Elements: 12 [download] Whichmeansthatit'ssameaspassingthefirsttwoelementsofthelistthesub-routineonehasreceived.ThoughIwasnotabletocompilethecode subone{ &twoshift,shift; } [download] whichisyourcase.Providethesomecontextofyoursnippetthatwouldhelpeveryonetolearn. Theworldissobigforanyindividualtoconquer [reply][d/l][select] Re^3:Using'Shift'insubroutineargument byshmem(Chancellor)onNov02,2007at13:20 UTC ThoughIwasnotabletocompilethecode subone{ &twoshift,shift; } [download] That'sasyntaxerror.Subroutineinvocationswith&requireparens. Droptheampersandandmovethesubtwodefinitionup: usestrict; usewarnings; one(1,2,3,4); subtwo{ print"Elements:\n@_\n"; } subone{ twoshift,shift; } [download] Toresolvethebarewordtwoasasubroutinecall(intwoshift,shift),thecompilerneedstohaveseenthesubdeclarationalready.Alternative: usestrict; usewarnings; subtwo;#forwarddeclaration one(1,2,3,4); subone{ twoshift,shift; } subtwo{ print"Elements:\n@_\n"; } [download] --shmem _($_=""x(1<<5)."?\n".q·/)Oo.G°\/ /\_¯/(q/ ----------------------------\__(m.====·.(_("alwaysoffthecrowd"))."· ");sub_{s./.($e="'Itrs`mnsgdqGdbjO`qkdq")=~y/"-y/#-z/;$e.e&&print} [reply][d/l][select] Re:Using'Shift'insubroutineargument bysen(Hermit)onNov02,2007at10:54 UTC Hi, Asubroutine'sargumentscomeinviathespecial@_array.Theshiftwithoutanargumentdefaultsto@_. forexample, subemp{ $name=shift; $id=shift; print"name,$name,id$id"; } &emp('abc',10); [reply] Re^2:Using'Shift'insubroutineargument byGrandFather(Saint)onNov02,2007at18:43 UTC Theshiftwithoutanargumentdefaultsto@_. exceptinmainscope(outsideasub)[email protected]: perl-e"printshift"wibble [download] Prints: wibble [download] Perlisenvironmentallyfriendly-itsavestrees [reply][d/l][select] Re:Using'Shift'insubroutineargument byFunkyMonk(Chancellor)onNov03,2007at01:38 UTC I'vealwaysbelieveditwasano-notochangeavariable(@_or@ARGVinthiscase)morethanonceinasinglestatement.AsfarasIknow,Perl[1]doesn'tdefinewhichshiftwillbedonefirst. [1]perl,doesofcourse,dooneortheotherfirst. [reply][d/l][select] Backto SeekersofPerlWisdom LogIn? Username: Password: rememberme What'smypassword? CreateANewUser DomainNodelet? www.com|www.net|www.org NodeStatus? nodehistoryNodeType:perlquestion[id://648616]Approvedbymoritzhelp Chatterbox? andallisquiet... HowdoIusethis?|OtherCBclients OtherUsers? OtherschantingintheMonastery:(3)GrandFather Tux tybalt89 Asof2022-10-2223:12GMT Sections? SeekersofPerlWisdom CoolUsesforPerl Meditations PerlMonksDiscussion CategorizedQ&A Tutorials ObfuscatedCode PerlPoetry PerlNews about Information? PerlMonksFAQ GuidetotheMonastery What'sNewatPerlMonks Voting/ExperienceSystem Tutorials Reviews Library PerlFAQs OtherInfoSources FindNodes? NodesYouWrote SuperSearch ListNodesByUsers NewestNodes RecentlyActiveThreads SelectedBestNodes BestNodes WorstNodes SaintsinourBook Leftovers? TheSt.LarryWallShrine BuyPerlMonksGear OfferingPlate Awards RandomNode Quests Craft Snippets CodeCatacombs EditorRequests blogs.perl.org Perlsphere PerlWeekly Perl.com PerlJobs PerlMongers PerlDirectory Perldocumentation MetaCPAN CPAN VotingBooth? Mypreferredwaytoholiday/vacationis: Visitforeigncountries/cities Relaxataresort Takeacruise Gotovisitfarawayfamily Getawayfromcivilization(e.g.camping,hiking,etc.) Wanderaroundmyowncountry/region(byanypreferredmeansofconveyance) Workonasideproject(e.g.homeimprovement,side-hustle,etc.) Digitalentertainment,astockedfridge,andbeingavoluntaryshut-in What'saholiday/vacation? Other(pleasecomment)Results(67votes).Checkoutpastpolls. Notices? PerlMonksparthenogeneticallyspawnedbyTimVroom. PerlMonksisaproudmemberofthe ThePerlFoundation. BeefyBoxesandBandwidthGenerouslyProvidedby pairNetworks BuiltwiththePerlprogramminglanguage.



請為這篇文章評分?