Perl - Scalars - Tutorialspoint
文章推薦指數: 80 %
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<
延伸文章資訊
- 1Perl | scalar keyword - GeeksforGeeks
scalar keyword in Perl is used to convert the expression to scalar context. This is a forceful ev...
- 2精簡扼要的Perl 課程講義(一):純量(Scalar)與陣列 ...
Perl:1980年代誕生,直譯式語言。 print "Hello World!\n"; # 雙引號可內嵌特殊字元如: # \n Newline # \r Carriage Return # \...
- 3Perl scalar 函数- 蝴蝶教程
Perl scalar 函数 ·. 描述. scalar 此函数强制EXPR的评估在标量上下文中进行,即使它通常可以在列表上下文中运行也是如此。 ·. 句法. 以下是此函数的简单语法-. sca...
- 4perl的内置函数scalar - 立体风- 博客园
scalar可以求数组的长度,但是,在scalar的说明里面并没有这一项。 Forces EXPR to be interpreted in scalar context and returns...
- 5Perl的基本語法
Perl的資料型態大致分為四種:Scalar、Scalar Array、Hash Array、References, 看起來雖少但用起來卻綽綽有餘。尤其在寫Perl程式時可以不必事先宣告變數,這...