Earlier we talk about Scalars data type in small section which define briefly, now we would describe it in very broader manner and try to understand various aspects of Scalar variable of Perl. We will also try to build a program and will move around for practical example of it.
So Lets start with an example that contain some example of Scalar variable, primarily there are two type of variables, one is Numeric and another is String. In below program, we will try to cover both in simple form and will further make it complex with further examples.
#!/usr/bin/perl print "Enter your Name : "; chomp($name=<STDIN>); print "How many years of experience in Perl language : "; chomp ($exp=<STDIN>); print "\n$name has $exp years of experience in Perl language\n\n"
In this program, we tried to cover both String as Name variable and Numeric as exp variable. So output of it would be..
# perl variable_scalar Enter your Name : Sang How many years of experience in Perl language : 5 Sang has 5 years of experience in Perl language
With above output it is clear that its quit easy to provide scalar variable with some Numeric and String value, But what if these value are bit complex, So let’s try to cover some complex examples.
Numeric Scalar Example
We already saw some example of Numeric Scalar variable in Perl language and now we are going to see some other complex Numeric examples , like negative, Float, bigfloat etc. These are possible value of Numeric variables and will also try to cover some operations with those Numeric values.
#!/usr/bin/perl print "Type one int value : "; chomp($int=<STDIN>); print "Type one negative value: "; chomp($negative=<STDIN>); print "Type one float value : "; chomp($float=<STDIN>); print "Type one bigfloat value : "; chomp($bigfloat=<STDIN>); print "\nyou typed int: $int ,negative: $negative ,float : $float ,bigfloat: $bigfloat \n";
Now we need to run it and see how perl behave on various Numeric scalars values.
# perl variable_scalar_numeric Type one int value : 56 Type one negative value: -07.432 Type one float value : 45.96567 Type one bigfloat value : 13_453_3.222456_789E-7 you typed int: 56 ,negative: -07.432 ,float : 45.96567 ,bigfloat: 13_453_3.222456_789E-7
Now we know Perl has capability to handle various types of Numeric variables, Although there could many more we could present through Perl program other than we mentioned above.
String variable Example
Now we should start taking some example of String variable examples and try to understand String variable in Perl.
Below program has some String variables.
#!/usr/bin/perl #Single string variable $word="Word"; #Multiple string variable $sentence="Multi word sentence"; #Multi-line String variable $paragraph="This need to Multi-line sentence Second line of Multi-line sentence"; print "$word\n $sentence\n $paragraph\n";
Let’s see how it’s output comes out of it and show String scalar variable.
Word Multi word sentence This need to Multi-line sentence Second line of Multi-line sentence
Now we have covered almost simple scalar variables and their examples with output. I Hope this make you clear about Perl Scalar Variable definition and its use for perl programs.
Scalar variable Operation
Let’s do some operations on variables. For same let’s create a simple code that have operation for number.
#!/ust/bin/perl print "Scalar variable Operation\n\n"; print "Enter a variable : "; $a=<STDIN>; print "Enter another variable : "; $b=<STDIN>; print "Addition of a+b: ", $a+$b,"\n"; print "Multiplication of axb: ", $a*$b,"\n"; print "Division of a/b: ", $a/$b, "\n";
Let’s see output for same Perl program if we use Numeric values for variables asked through user running program.
l# perl sacalar_variable_operation Scalar variable Operation Enter a variable : 34 Enter another variable : 1234 Addition of a+b: 1268 Multiplication of axb: 41956 Division of a/b: 0.0275526742301459
Now we could see in mentioned above program and its output, we did variable operation within print statement for number types. But what if we provide strings instead of number, it should come out with error for same.
# perl sacalar_variable_operation Scalar variable Operation Enter a variable : sd Enter another variable : er Addition of a+b: 0 Multiplication of axb: 0 Illegal division by zero at sacalar_variable_operation line 17, <STDIN> line 2. # perl sacalar_variable_operation Scalar variable Operation Enter a variable : 12 Enter another variable : fg Addition of a+b: 12 Multiplication of axb: 0 Illegal division by zero at sacalar_variable_operation line 17, <STDIN> line 2.
In above output we used Scalar string values, it did Addition and multiplication but while division it denied to work on same.
Special literal variables
Perl has some global variables , some of them are stored in some special literal formats know as special literals variables some of them as been displayed in below program and also described further.
#!/usr/bin/perl print "File name: ", __FILE__ , "\n"; print "Line Number: ", __LINE__ ,"\n"; print "Package: ", __PACKAGE__ ,"\n"; __END__ print "This would not print as after __END__"
Below is output for same.
# perl special_literals_variable File name: special_literals_variable Line Number: 4 Package: main
Here we used to print some Special literals,
__FILE__ will display script name.
__LINE__ will display line number.
__PACKAGE__ will display current package name default is main
__END__ Perl will ignore further statements after __END__.