Taking Input in Perl - CodesDope
文章推薦指數: 80 %
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
延伸文章資訊
- 1Prompt, read from STDIN, read from the keyboard in Perl
IO::Prompter. This was a very simple solution using only the core language and providing a very s...
- 2Perl STDIN | How STDIN works in Perl? with Examples and ...
Perl STDIN is used to get input from standard console by using STDIN in perl. STDIN in perl is al...
- 3Perl Tutorial - User Input - Tizag Tutorials
A tutorial of PERL , standard input in PERL via the command prompt. ... <STDIN> stands for standa...
- 4How to get input from the terminal in Perl - Stack Overflow
I'm taking questions from the terminal and sending reply with dialog, but my program takes only t...
- 5精簡扼要的Perl 課程講義(四):標準輸入輸出與檔案輸入輸出
標準輸入輸出. # 標準輸入輸出 # (1) STDIN, STDOUT, STDERR print "Enter your name:"; $input = <STDIN>; # 從STDIN...