Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Scalar Value Interpretation

This section describes how a scalar value will be interpreted in an operation. A tutorial example is provided to verify scalar value interpretation rules.

When a scalar value is used in an operation, it could be interpreted in three ways depending on the type of value the operation is expecting:

  • A scalar value will be interpreted as a number, if the operation is expecting a numeric value. If the scalar value is a numeric value, Perl will take it as is. If the scalar value is a string value, Perl will try to parse it into a number. If parsing failed, it will be interpreted as 0.
  • A scalar value will be interpreted as a string, if the operation is expecting a string value. If the scalar value is a string value, Perl will take it as is. If the scalar value is a numeric value, Perl will convert it into a string representation of that value.
  • A scalar value will be interpreted as TRUE or FALSE, if the operation is expecting a Boolean value. Perl will interpret number 0, string '0', and empty string '' as FALSE. Any other values will be interpreted as TRUE.

To verify the rules listed above, I wrote the following Perl tutorial script:

#- ScalarValue.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   print(0.00, "\n");                  # 0
   print(00.00, "\n");                 # 00
   print(1e-1, "\n");                  # 0.1
   print(1e+50, "\n");                 # 1e+050
   print(1e+100, "\n");                # 1e+100
   print(1e+1000, "\n");               # 1.#INF
   print(1e+1000 + 1, "\n");           # 1.#INF
   print('1e+1000' + 1, "\n");         # 1.#INF
   print(0.1234567890123456789, "\n"); # 0.123456789012346
   print(1 / 3, "\n");                 # 0.333333333333333
   print(1 / '3', "\n");               # 0.333333333333333
   print(1 + 3, "\n");                 # 4                
   print(1.3, "\n");                   # 1.3              
   print(1 . 3, "\n");                 # 13               
   print((1 . 3) * 5, "\n");           # 65               
   print('Hello '. 'world!', "\n");    # Hello world!     
   print(1 + 'a', "\n");               # 1                
   print(1 * 'a', "\n");               # 0                
   print(1 + '', "\n");                # 1                
   print('FALSE', "\n") if (not 0);    # FALSE            
   print('FALSE', "\n") if (not '0');  # FALSE            
   print('FALSE', "\n") if (not '');   # FALSE            
   print('TURE', "\n") if (' ');       # TURE             
   print('TURE', "\n") if (1);         # TURE             

The printed values are entered into the script as comments. You are probably surprised to see some of the printed values. Me too.

Sections in This Chapter

Scalar Values and List Values

Scalar Value Constructors

Scalar Value Interpretation

List Value Constructors

Variables - Scalar, Array and Hash

Using Scalar Variables

Using Array Variables

Using Hash Variables

"undef" Value and Undefined Variables

Dr. Herong Yang, updated in 2008
Scalar Value Interpretation