Perl printing examples | alvinalexander.com

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

Generally you'll print simple output with the Perl print function. As a simple example, you can print a string literal using the Perl print ... 2021cookbook    #1newrelease! Perlprintingexamples ByAlvinAlexander.Lastupdated:March21,2018 PerlprintingFAQ: CanyousharesomePerlprintingexamples? ThereareseveraldifferentwaystoprintinPerl,andIthoughtI'dsharesomeexamplesheretoday. ThePerlprintfunction Generallyyou'llprintsimpleoutputwiththePerlprintfunction.Asasimpleexample,youcanprintastringliteralusingthePerlprintfunction,likethis: print"Hello,world.\n"; Noticethatyouneedtosupplythenewlinecharacterattheendofyourstring.Ifyoudon'tsupplythatnewlinecharacter,andprintmultiplelines,they'llallendupononelonglineofoutput,likethis: Hello,world.Hello,world.Hello,world. So,whenusingthePerlprintfunction,don'tforgetthenewlinecharacterattheendofthestringyou'reprinting. PrintingPerlvariableswithprint Inanyreal-worldPerlscriptyou'llneedtoprintthevalueofyourPerlvariables.Toprintavariableaspartofaastring,justusethePerlprintingsyntaxasshowninthisexample: $name='Alvin'; print"Hello,world,from$name.\n"; WhenyourunthisPerlscript,you'llseethefollowingoutput: Hello,world,fromAlvin. Perldoestheworkofreplacingthevariable$namewiththevalueofthatvariable,Alvin. Perlprinting-Doubleandsinglequotes OneimportantthingtonoteinthisexampleistheuseofdoublequoteswiththatPerlprintstatement.InPerl,whenyouplaceavariableinsideofdoublequotes,Perlcanreplacethevalueofthevariablename.Thisiscalled"variableinterpolation",anditworkswhenyouusedoublequotes,butitdoesnotworkwhenyouusesinglequotes. ThePerlprintfunctionisessentiallyblindtoanythingyouplaceinsidesinglequotes,andwilljustprintwhateveryouputinbetweensinglequoteswithoutgivingitasecondthought.Forexample,ifyoutrythatsamePerlprintstatementwithsinglequotes,likethis: #thiswon'tworkasdesired: print'Hello,world,from$name.\n'; Inthisexample,Perlcan'tseeinsidethesinglequotes,soitcan'treplace$namewithAlvin,anditendsupprinting$nameinstead,asshownhere: Hello,world,from$name\n Iforgottomentionthe\ncharacter,butasyoucanseeinthatoutput,whenyouusesinglequotes,thePerlprintfunctionalsocan'tseethe\ncharacter,soitjustprintsthetwocharacters\andn,insteadofactuallyseeingthatcharacterandprintinganewlinecharacterinitsplace. Perlprinting-OtherwaystoprintPerlvariables Icouldhavealsoprintedthatpreviousexampleasshownhere: $name='Alvin'; print"Hello,world,from".$name.".\n"; Theresultisthesame,butIdon'tcareforthisPerlprintingapproachverymuch.Forme,it'smuchhardertoread,especiallywhenyouneedtoprintmorethanonevariableatatime. FormattedprintingwiththePerlprintffunction Finally,youcanalsoprintinPerlusingthePerlprintffunction.I'vecoveredtheprintffunctioningreatdetailinmyPerlprintfformattingtutorial,soIwon'tgetintothatagainhere.Pleasevisitthattutorialformoreinformation. OtherPerlprintingideas I'msurethereareotherPerlprintingapproachesthanwhatIcanthinkofrightnow,butIthoughtI'dputtheseoutheretogetstartedwith.IfyouhaveanyPerlprintingexamplesyou'dliketoshare,justusethecommentformbelow. perl perl print printf printing perlprint APerlhashprintexample(printinghashelements) Perlarrayandforeach-HowtooperateoneachPerlarrayelement HowtoprintaPerlarray PerlCGIenvironmentvariablesexample Perl:Howtoextractlinesfromthemiddleofatextfile booksi’vewritten (Almost)FunctionalProgramming SomeofShinzenYoung’ssayingsinthefirstcorelessonsoftheBrightmindapp LearnScala3TheFastWay!(BestwaytolearnScala!) FunctionalProgramming,Simplified(aScalabook) HomagetoHaskellandFunctionalProgramming



請為這篇文章評分?