Taking Input in Perl - CodesDope

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

Taking command line arguments means to pass some argument (or give input) while calling this command to run a Perl script. For example, to give 5 as input, ... About Courses Python C C++ Java Ruby Perl Algorithms DataStructures C# Discussion Practice Blog PRO Salaries SignUp Login Welcome.pleasesignup. Username Password(min6characters) Password(again) Email SignUp Bysigninguporloggingin,youagreetoourTermsofserviceandconfirmthatyouhavereadourPrivacyPolicy. Alreadyamember?GotoLogIn Welcome.pleaselogin. UsernameorEmail Password Rememberme Forgotyourpassword Login Notregisteredyet?GotoSignUp Bysigninguporloggingin,youagreetoourTermsofserviceandconfirmthatyouhavereadourPrivacyPolicy. Introduction Print Moreprint Input Boolean Operators ifandelse whileanduntil Array String Regularexpressions forandeach Hashes Function References Modulesandpackages Classesandobjects Inheritance Filei/o Directory UsingMoose MoreMoose TakingInputinPerl Youwilllearntotakeinputfromusersinthischapter.Wedothisbyusing<>operatorinPerl.Thisoperatorreadsalineenteredthroughthekeyboardalongwiththenewlinecharacter.Let'sseethisexampleandthenwewilldiscussmoreaboutthis. print"Whatisyourname?\n"; $name=<>; print"Yournameis",$name; Output WhatisyournameSamYournameisSam <>operatorisusedtotakeinputanditdidthesame.Ittookaninputfromtheuser. $name=<>;-Thislinemeansthatthevariable'name'isequaltotheinputtakenas<>returnsthetakeninput. Onemorethingyoushouldnoticehereisthat<>alsoreturnedanewlinecharactercorrespondingtotheENTERwepressedaftergivinginput. Togetridofthisnewlinecharacter,weusechompfunction.Itjustremovesthenewlinecharacterfromtheend. print"Whatisyourname?\n"; $name=<>; chomp($name); print"Yournameis",$name,"\n"; Output WhatisyournameSamYournameisSam Asmentionedearlier,Perlstringsandnumbersareseamlesslyconvertedintoeachotherdependingonthecontextinwhichtheyareused.So,thereisnoneedtotakeseparateinputsfornumbers.Let'sseeanexample: print"Enteranumber\n"; $number=<>; chomp($number); print$number*2,"\n"; Output Enteranumber1020 Soyoujustsawthattheinputwasconvertedtoanintegerwhenitwasneeded.However,ifyouwanttoconvertitintoanumber,youcandoitasfollows: print"Enteranumber\n"; $number=<>; #converttointeger $number=$number*1; Output Enteranumber10 Takingcommandlinearguments WerunaPerlscriptbywritingperlfilename.pl(filenameisthenameoffile).Takingcommandlineargumentsmeanstopasssomeargument(orgiveinput)whilecallingthiscommandtorunaPerlscript.Forexample,togive5asinput,writingthiscommand-perlfilename.pl5ortopassboth5and10,writingthis-perlfilename.pl510. Wedothisbyusing@ARGVarray.Youwilllearnaboutarraysinaseparatechapter,fornowkeepinmindthatthefirstpassedargumentisstoredinavariable$ARGV[0],secondin$ARGV[1]andsoon.Let'sseeanexampleofpassingtwonumbersandprintingtheirsum.Wewillgetthefirstpassednumberby$ARGV[0]andthesecondby$ARGV[1]. #Commandgivenperlfilename.pl510 #filenameisthenameoffile print"$ARGV[0]+$ARGV[1]is",$ARGV[0]+$ARGV[1],"\n"; Output 5+10is15 Prev Next CODESDOPE PRO It's SimpleandConceptual with ChanceforInternship* C++inEnglish Launched GetC++(English)coursefor999 Days Hours Min Sec ProCourseFeatures SimpleVideos Questionsto Practice Solved Examples InternshipChance* Certificateof Completion Discussionwith Experts LearnforFREE NewQuestions structures-C Whatisyourfavoritegame?-Other Dating-Other HowoftenshouldIoilmyguns?-C Aretherereliablesolarpanelsuppliers?-Other WhatisthebestwaytopurchaseTwitchfollowers?-Other WhatareJava'sAdvantages?-Algorithms AskYours RecentPosts pow()inPython DutchNationalFlagproblem-Sort0,1,2inanarray memoryview()inPython next()inPython PostYours Doubt?Askquestion



請為這篇文章評分?