Perl printing examples | alvinalexander.com
文章推薦指數: 80 %
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
延伸文章資訊
- 1Perl的基本語法
print "$array[$i]\n"; } 看到$#array這個奇怪的東東沒? 這是Perl的一個特殊用法,代表這個陣列最後一個元素的註標。 由於Perl不必事先宣告變數,也不必預先宣告陣...
- 2print FILEHANDLE - Perldoc Browser
Prints a string or a list of strings. Returns true if successful. FILEHANDLE may be a scalar vari...
- 3Perl printing examples | alvinalexander.com
Generally you'll print simple output with the Perl print function. As a simple example, you can p...
- 4Use of Print in PERL - Linux Hint
“Many options exist in PERL to print different types of values and variables. The print operator ...
- 5Perl print() and say() - Javatpoint
Perl print() function is one of the most commonly used function by the Perl users. It will print ...