Perl - Scalars - Tutorialspoint

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

Perl - Scalars, A scalar is a single unit of data. That data might be an integer number, floating point, a character, a string, a paragraph, ... Home CodingGround Jobs Whiteboard Tools Business Teachwithus PerlBasics Perl-Home Perl-Introduction Perl-Environment Perl-SyntaxOverview Perl-DataTypes Perl-Variables Perl-Scalars Perl-Arrays Perl-Hashes Perl-IF...ELSE Perl-Loops Perl-Operators Perl-Date&Time Perl-Subroutines Perl-References Perl-Formats Perl-FileI/O Perl-Directories Perl-ErrorHandling Perl-SpecialVariables Perl-CodingStandard Perl-RegularExpressions Perl-SendingEmail PerlAdvanced Perl-SocketProgramming Perl-ObjectOriented Perl-DatabaseAccess Perl-CGIProgramming Perl-Packages&Modules Perl-ProcessManagement Perl-EmbeddedDocumentation Perl-FunctionsReferences PerlUsefulResources Perl-QuestionsandAnswers Perl-QuickGuide Perl-UsefulResources Perl-Discussion SelectedReading UPSCIASExamsNotes Developer'sBestPractices QuestionsandAnswers EffectiveResumeWriting HRInterviewQuestions ComputerGlossary WhoisWho Perl-Scalars Advertisements PreviousPage NextPage  PerlOnlineTraining 46Lectures 4.5hours DeviKillada MoreDetail COMPLETEPERLProgramming 11Lectures 1.5hours HarshitSrivastava MoreDetail PerlforBeginners:LearnAtoZofPerlScriptingHands-on 30Lectures 6hours TELCOMAGlobal MoreDetail Ascalarisasingleunitofdata.Thatdatamightbeanintegernumber,floatingpoint,acharacter,astring,aparagraph,oranentirewebpage. Hereisasimpleexampleofusingscalarvariables− LiveDemo #!/usr/bin/perl $age=25;#Anintegerassignment $name="JohnPaul";#Astring $salary=1445.50;#Afloatingpoint print"Age=$age\n"; print"Name=$name\n"; print"Salary=$salary\n"; Thiswillproducethefollowingresult− Age=25 Name=JohnPaul Salary=1445.5 NumericScalars Ascalarismostofteneitheranumberorastring.Followingexampledemonstratestheusageofvarioustypesofnumericscalars− LiveDemo #!/usr/bin/perl $integer=200; $negative=-300; $floating=200.340; $bigfloat=-1.2E-23; #377octal,sameas255decimal $octal=0377; #FFhex,also255decimal $hexa=0xff; print"integer=$integer\n"; print"negative=$negative\n"; print"floating=$floating\n"; print"bigfloat=$bigfloat\n"; print"octal=$octal\n"; print"hexa=$hexa\n"; Thiswillproducethefollowingresult− integer=200 negative=-300 floating=200.34 bigfloat=-1.2e-23 octal=255 hexa=255 StringScalars Followingexampledemonstratestheusageofvarioustypesofstringscalars.Noticethedifferencebetweensinglequotedstringsanddoublequotedstrings− LiveDemo #!/usr/bin/perl $var="Thisisstringscalar!"; $quote='Iminsidesinglequote-$var'; $double="Thisisinsidesinglequote-$var"; $escape="Thisexampleofescape-\tHello,World!"; print"var=$var\n"; print"quote=$quote\n"; print"double=$double\n"; print"escape=$escape\n"; Thiswillproducethefollowingresult− var=Thisisstringscalar! quote=Iminsidesinglequote-$var double=Thisisinsidesinglequote-Thisisstringscalar! escape=Thisexampleofescape-Hello,World ScalarOperations YouwillseeadetailofvariousoperatorsavailableinPerlinaseparatechapter,butherewearegoingtolistdownfewnumericandstringoperations. LiveDemo #!/usr/bin/perl $str="hello"."world";#Concatenatesstrings. $num=5+10;#addstwonumbers. $mul=4*5;#multipliestwonumbers. $mix=$str.$num;#concatenatesstringandnumber. print"str=$str\n"; print"num=$num\n"; print"mul=$mul\n"; print"mix=$mix\n"; Thiswillproducethefollowingresult− str=helloworld num=15 mul=20 mix=helloworld15 MultilineStrings Ifyouwanttointroducemultilinestringsintoyourprograms,youcanusethestandardsinglequotesasbelow− LiveDemo #!/usr/bin/perl $string='Thisis amultiline string'; print"$string\n"; Thiswillproducethefollowingresult− Thisis amultiline string Youcanuse"here"documentsyntaxaswelltostoreorprintmultilinesasbelow− LiveDemo #!/usr/bin/perl print<



請為這篇文章評分?