How to Work With User Input in Perl - Qualitest

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

Perl is a language that is useful for scripting, file processing, and other forms of development. The command line can be used to gather a user's input and ... Insights TechnicalHub HowtoWorkWithUserInputinPerl HowtoWorkWithUserInputinPerl Introduction: Perlisalanguagethatisusefulforscripting,fileprocessing,andotherformsofdevelopment.Thecommandlinecanbeusedtogatherauser’sinputandcollectedbyPerl. Requirements: APerlInterpreter(shouldbenativeonLinux,ActiveStatePerlisonedistributionforWindows),andacommandlineinterface(CLI)suchascommandprompt/cmd.exeonWindowsorashellonLinux. Procedure: Setupyourtextfile(withfileextension.pl)asfollows,startingfromthefirstline: #!\usr\bin\Perl usestrict; usewarnings; my$data; ThefirstlineindicateswherethePerlinterpreterislocated.ThismaybeirrelevantonWindows,butwhetheritisincludedornot,thelocationwillbeusedonLinux.Thenextlinessignalwhatmodetorunin.Strictmodewithwarningswillthrowerrorsanddisplaywarningsifpresent.Thelastlineisdeclaringthelocationwheretheuserinputdatawillbestored. Followingthis,let’scollectuserinput: print“Pleaseentersomedata,thenpressEnter.\n”; $data=; chomp($data); Theabovelinesfirstprintamessagetothescreen,followedbyanewline(\n).Then,thedataiscollectedbytheenduserwhoisgivencontrolaftertheprompt.Followingthis,the chomp() functionwillremovethe \n newlineattheendofthedata,whetheritisthereornot. $datathencontainsthedataforfurtherusage,afterbeingcollectedby. share Newsletter Signuptoreceiveinsightfulcontent. HowtoWorkWithFilesinPerl readmore HowtouseregularexpressionsinPerlPart1:Syntax readmore



請為這篇文章評分?