Scalars





                    CHAPTER 3: SCALARS


   What Is Scalar Data?

     - Number

     - String


   Strings

     - Sequence of characters

     - Character can be any 8-bit ASCII value


   String Literals

     - Single-Quoted

     - Double-Quoted


   Single-quoted Strings

     - Sequence of characters enclosed in single quotes

     - No variable interpolation or special backslash escape handling

     - To get a single quote inside a single-quoted string: \'

     - To get a backslash inside a single-quoted string: \\

     - Backslash ONLY special if followed by a single-quote or backslash

     - Ex.

         'hello, world'            # hello, world
         'don\'t do that'          # don't do that
         ''	                   # the null string
         'hello, world\n'          # hello, world\n
         'hello, world\\n'         # hello, world\n
         'hello
          world'                   # hello<newline>world


   Double-quoted Strings

     - Sequence of characters enclosed in double quotes

     - Variable interpolation and special backslash escape handling done

     - Ex.

         "hello, world"            # hello, world
         "don't do that"           # don't do that
         ""                        # the null string
         "hello, world\n"          # hello, world<newline>
         "hello
          world"                   # hello<newline>world


   Double-Quoted String Backslash Escapes

         \n		Newline
         \r		Carriage Return
         \t 		Tab
         \f		Formfeed
         \b		Backspace
         \v		Vertical Tab
         \a		Bell
         \e		Escape
         \001		Octal ASCII value (here Ctrl-A)
         \x20		Hex ASCII value (here space)
         \cD		Control character (here Ctrl-D)
         \\		Backslash
         \"		Double Quote
         \l		Lowercase next letter
         \L		Lowercase all following letters until \E
         \u		Uppercase next letter
         \U		Uppercase all following letters until \E
         \E		Terminate \L or \U
  

   Operations On Strings

     - Concatenation: .

     - Replication  : x

     - Comparison   : eq, ne, lt, gt, le, ge

     - Signed Result Comparison : cmp (returns -1, 0 or 1)

     - Ex.

         "bob " . "tarr"           # bob tarr
         "bob" x 4                 # bobbobbobbob
         ("bob" lt "joe")          # true


   Numbers

     - Integer Literals

     - Float Literals

     - Internally, all numbers are double-precision floating-point values

     - Literal octal numbers begin with a 0

     - Literal hex numbers begin with a 0x or 0X

     - Ex.

         -513
         10.3
         -1.4E12
         045
         0x7f


   Operations On Numbers

     - Arithmetic   : +, -, *, /, **, %

     - Bitwise      : &, |, ^, ~

     - Bitshift     : <<, >>

     - Comparison   : ==, !=, <, >, <=, >=

     - Signed Result Comparison : <=> (returns -1, 0 or 1)

     - Ex.

         (4 + 5)                   # 9
         9 / 4                     # 2.25
         9.2 % 4.7                 # 1
         (4 != 4)                  # false

     - Remember Precedence And Associativity!!  (See the list in the 
       Llama book.  Note that the list in the Camel book is incorrect!)


   Scalar Variables

     - Variable that holds a single scalar value

     - Name begins with a $

     - Scalar variables have their own namespace


   Variable Names

     - Must begin with a letter, followed by any number of letters, 
       digits or underscores

     - Case sensitive

     - All characters significant


   Scalar Assignment Operator (=)

     - Used to give a value to a scalar variable

     - Scalar variables have the undef value before they are first 
       assigned 

     - The undef value looks like a numeric 0 or a null string

     - Ex.

         $name = "Bob";
         $x = 4;
         $x = $x + 2;
         $y = 6 + ($z = 4);

     - A scalar assignment itself has a value.  This value is the value 
       (string or number) which is assigned to the variable.  So, the 
       value of ($z = 4) is 4.

     - Assignment is right associative

  
   Scalar Binary Assignment Operators

     - Shorthand for many common assignments
  
     - Ex.

         $x += 2;               # Same as $x = $x + 2;

     - Binary Assignment Ops: +=, -=, *=, /=, **=, %=,, <<=,
                              >>=, &=, |=, ^=, .=, x=

   
  Scalar Autoincrement/Autodecrement

     - Shorthand for adding or subtracting 1 

     - Autoincrement: ++
 
     - Autodecrement: --

     - Suffix form: Value of expression uses the value of the variable 
                    BEFORE it is incremented (decremented)

     - Ex.

         $x = 2;
         $y = $x++ + 5;         # $y is 7, $x is 3

     - Prefix form: Value of expression uses the value of the variable 
                    AFTER it is incremented (decremented)

     - Ex.

         $x = 2;
         $y = --$x + 5;         # $y is 6, $x is 1

   
   Conversion Between Numbers And Strings

     - If a string value is used where a numeric value is needed, Perl 
       automatically converts the string to its numeric equivalent.  
       Trailing non-number stuff and leading whitespace is ignored.

     - If a numeric value is used where a string value is needed, Perl 
       automatically converts the number to its string equivalent.

     - Ex.

         $str1 = "123";
         $str2 = "4.5bob";
         $str3 = "bob6.8";
         $x = $str1 + 5;           # $x is 128
         $y = $str2 + 5;           # $y is 9.5
         $z = $str3 + 5;           # $z is 5

         $str4 = "BT" . 456;       # $str4 is BT456


   Scalar Variable Interpolation

     - Substitution of a scalar variable reference with its value done 
       inside a double-quoted string literal

     - Can use {} around the name of a variable to delimit it

     - Ex.

         $name = "Bob Tarr";
         $str1 = "My name is $name";     # $str1 is My name is Bob Tarr
         $str2 = "My name is $names";    # $str2 is My name is
         $str3 = "My name is ${name}s";  # $str3 is My name is Bob Tarrs

         $x = '$name';
         $y = "$x";                      # $y is $name


   The Chop Function

     - Removes the last character from a scalar variable

     - Returns the character chopped

     - Used to remove the newline from input lines

     - Ex.

         $name = "Bob Tarr";
         $char = chop ($name);     # $char is r
                                   # $name is Bob Tar


   The Length Function

     - Returns the number of characters in a scalar variable

     - Ex.

         $name = "Bob Tarr";
         $x = length ($name);      # $x is 8




Bob Tarr
University of Maryland, Baltimore County
tarr@umbc.edu