How do I break out of a loop in Perl? - Stack Overflow

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

Bareword "break" not allowed while "strict subs" in use at ./final.pl line 154. Is there a workaround for this (besides disabling strict subs)?. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam StackOverflowforTeamsismovingtoitsowndomain!Whenthemigrationiscomplete,youwillaccessyourTeamsatstackoverflowteams.com,andtheywillnolongerappearintheleftsidebaronstackoverflow.com. Checkyouremailforupdates. Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams HowdoIbreakoutofaloopinPerl? AskQuestion Asked 13years,11monthsago Modified 2years,1monthago Viewed 596ktimes 317 New!Savequestionsoranswersandorganizeyourfavoritecontent.Learnmore. I'mtryingtouseabreakstatementinaforloop,butsinceI'malsousingstrictsubsinmyPerlcode,I'mgettinganerrorsaying: Bareword"break"notallowedwhile "strictsubs"inuseat./final.pl line154. Isthereaworkaroundforthis(besidesdisablingstrictsubs)? Mycodeisformattedasfollows: formy$entry(@array){ if($stringeq"text"){ break; } } perlloopsbreakstrict Share Improvethisquestion Follow editedMar16,2014at20:27 PeterMortensen 30.6k2121goldbadges102102silverbadges124124bronzebadges askedNov19,2008at20:21 ZainRizviZainRizvi 23.1k2020goldbadges8888silverbadges127127bronzebadges 1 24 Andifyoudidn'thave"strictsubs"on,youwouldhavegottenarun-timeerrorinsteadwhenitcouldn'tfindasubnamed"break". – PaulTomblin Nov19,2008at20:43 Addacomment  |  5Answers 5 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 467 Oh,Ifoundit.Youuselastinsteadofbreak formy$entry(@array){ if($stringeq"text"){ last; } } Share Improvethisanswer Follow editedJan5,2018at17:41 AlexanderRoskamp 4766bronzebadges answeredNov19,2008at20:23 ZainRizviZainRizvi 23.1k2020goldbadges8888silverbadges127127bronzebadges 4 Thoughthisismerelyasemanticdifferenceanddoesn'tamounttomuchelseotherthantextualconsistencywiththe"next"command. – RazorStorm Jul8,2010at20:14 1 nottomentionthat'break'isanexperimentalkeywordin>5.010right?soyoumightgetanillegaloutsidegivenblockerror,notthatyoushoulduseit.Theonlywaythatwouldevenworkisifyouusedawhileloopinsideagivenblock,andthenbreak,whichintheorywouldworkthewayyouwantedIF(andonlyif)thegivenblockonlyhadthewhileloopinit,andassumingyouwerejustfeedinggivensomescalarvariable...butwhydoallthatworkjusttouseabreak,itis(very)badform,nottomention(blah!)justsaying,its"possible"justnotagoodidea&notwhatyoumaythinkitis – osirisgothra Sep1,2014at15:50 30 forsomereason,iamcompletelyunabletorememberthiskeyword'last'.alwaysendupgoogling'perlbreakloop':( – Thupten Sep8,2015at4:50 Also,worksthesameforwhile()loops.my@array=("_","apple","orange");my$thing;while($thing=shift@array){ lastif$thing=~/[A-Za-z]/;}print($thing); #"apple" – HoldOffHunger Jul17,2018at19:06 Addacomment  |  177 Additionaldata(incaseyouhavemorequestions): FOO:{ formy$i(@listone){ formy$j(@listtwo){ if(cond($i,$j)){ lastFOO;#---> #| }#| }#| }#| }#



請為這篇文章評分?