• home
  • forum
  • my
  • kt
  • download
  • Understanding of Perl Data Types

    Author: 2007-08-10 11:36:24 From:

    This chapter describes:

    • Three built-in data types: scalars, arrays, and associative arrays.
    • How to construct scalar objects.
    • How scalar objects are interpreted in operations.
    • How to construct list objects.

    Data Types and Variables

    Perl has three built-in data types: scalars, arrays, and associative arrays.

    1. Scalar - A data type representing a single numeric value or a string of characters. A scalar object can be assigned to a scalar variable with an identifier prefixed with $: $identifier.

    2. Array - A data type derived from scalar representing an ordered list of scalars indexed by numbers, starting with 0. An array object can be assigned to an array variable with an identifier prefixed with @: @identifier.

    3. Associative Array - A data type derived from scalar representing an unordered list of scalars indexed by their associated string keys. Associative arrays are also called hashes. A hash object can be assigned to a hash variable with an identifier prefixed with %: %identifier.

    Like in many other programming languages, an identifier in Perl is a string beginning with a letter or underscore, and containing letters, underscores, and digits.

    Variables for different data types are in different name spaces. You could use the same variable identifier for a scalar, an array, or a hash.

    Scalar Value Constructors

    Scalar value constructors are used to construct scalar objects. There are two types of scalar value constructors: numerical literals and string literals.

    Numerical literals are specified in integer and floating point formats as in the following examples:

       1 
       8.8
       1e-1
       1e+50
    

    String literals are usually specified in single or double quotes as in the following examples:

       '3.14'
       'Herong\'s Notes'
       "Hello world!\n"
    

    Note that with single quotes, only allow two backslash substitutions: \' and \\. With double quotes, all backslash substitutions are allowed.

    Here is some examples to help you understand better what are good or bad scalar value constructors:

       1; # ok
       8.8; # ok
       9.9.9; # bad, not a numberic value
       1e-1; # ok
       1e+50; # ok
       1.0d; # bad, no double floating point notation allowed
       '3.14'; # ok
       1a; # bad, need quotes
       'hello'; # ok
       'dir \\home\\herong'; # ok
       'Herong's Notes'; # bad, need a backslash substitution \'
       "Herong's Notes"; # ok
       'Hello world!\n'; # ok, but \n is not a backslash substitution here
    

    Once scalar objects are constructed, they can be used in scalar variable assignment operations as the following examples:

       $one = 1;
       $x = 8.8;
       $big = 1e+50;
       $pi = '3.14';
       $msg = 'hello';
       $cmd = 'dir \\home\\herong';
       $title = "Herong's Notes";
    

    Of course, scalar objects can be used in many other operations. We will discuss them in other chapters.

    Scalar Object Interpretations

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

    • A number, if the operation is expecting a numeric value. If the content in the object is a string, Perl will try to parse it into a number. If parsing failed, it will be interpreted as 0.
    • A string, if the operation is expecting a string value. If the content in the object is a number, Perl will convert it into a string representation of that number.
    • A TRUE or FALSE, if the operation is expecting a boolean value. Perl will interpret number 0, string '0', and empty string '' as FALSE, and anything else as TRUE.

    To verify the rules listed above, I wrote the following program:

    #- ScalarObject.pl
    #- Copyright (c) 1995 by Dr. Herong Yang
    #
       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 outputted values are entered into the program as comments. You are probably surprised to see some of the outputted values. Me too.

    If a scalar variable is used in an operation, its assigned object will be interpreted and used in the operation. But if a scalar variable is not assigned (undefined), it will be interpreted as:

    • 0, if a numerical value is expected.
    • '', if a string value is expected.
    • FALSE, if a boolean value is expected.

    Here is a program to show you how a undefined scalar variable behaves in an operation:

    #- undefined.pl
    #- Copyright (c) 1995 by Dr. Herong Yang
    #
       print '(' . $a . ')', "\n";
       print 1 + $a, "\n";
       print 1 * $a, "\n";
       print 'FALSE', "\n" if (not $a);

    List Value Constructors

    List value constructors are used to construct list objects. A list object is almost identical to an array object. It represents an ordered list of scalars indexed by numbers starting with 0.

    List objects are constructed by listing individual scalars separated by commas. You should also use parentheses to protect the constructor as a single unit in operations to avoid confusions. If you do use parentheses, they you don't have to put string literals in quotes. Some good and bad examples are: in the following examples:

       'hello'; # ok, a single scalar is also a list
       3,5,7,11; # ok
       'Mon','Tue','Wed'; # ok
       ('Mon','Tue','Wed'); # ok
       (Mon,Tue,Wed); # ok
       'Mon',2,'Apple'; # ok
       'Jan',31,'Feb',28,'Mar',31; # ok
       'Jan' 'Feb' 'Mar'; # bad, need commas to separate the elements
    

    An individual scalar in a list object can be accessed by the subscription notation, [index]. In this case, you need parentheses to protect the list object. Also note that negative index can be used to count down from the last entry. If the index is out of the range, you are accessing a undefined scalar. Here is a sample program with list subscriptions:

    #- ListObject.pl
    #- Copyright (c) 1995 by Dr. Herong Yang
    #
       print((3,5,7,11)[0], "\n");           # 3
       print(('hello')[0], "\n");            # hello
       print(('hello')[1], "\n");            #
       print(('hello')[-1], "\n");           # hello
       print(('Mon','Tue','Wed')[1], "\n");  # Tue  
       print((Mon,Tue,Wed)[-1], "\n");       # Wed
    

    Once list objects are constructed, they can be used in array variable assignment operations as in the following examples:

       @messages = 'hello'; 
       @primes = 3,5,7,11;
       @vacations = 'Mon','Tue','Wed';
       @vacations = ('Mon','Tue','Wed');
       @vacations = (Mon,Tue,Wed);
       @menu = 'Mon',2,'Apple';
       @dates = 'Jan',31,'Feb',28,'Mar',31;
    

    List objects can also be used in a hash variable assignment operation. In this case, every two individual scalars will be paired together to form a key string and the associated scalar. If the last scalar has no partner, it will become a key with a undefined scalar. Here are some examples:

       %messages = 'hello'; 
       %primes = 3,5,7,11;
       %vacations = 'Mon','Tue','Wed';
       %vacations = ('Mon','Tue','Wed');
       %vacations = (Mon,Tue,Wed);
       %menu = 'Mon',2,'Apple';
       %dates = 'Jan',31,'Feb',28,'Mar',31;
       %prices = ('Milk',1.99,'Coke',0.99,'Bread',1.49);
    

    If a scalar object is used where a list object is expected, the scalar object will be converted into a list object with single scalar. If a list object is used where a scalar object is expected, the list object will be converted into a scalar object with the last scalar in the list.

    #- ListToScalar.pl
    #- Copyright (c) 1995 by Dr. Herong Yang
    #
       print(1+(3,5,7,11), "\n");    # 12
       print(1+(Mon,Tue,Wed), "\n"); # 1
       print((sort 11), "\n");       # 11
    

    Of course, list objects can be used in many other operations. We will discuss them in other chapters.

    discuss this topic to forum

    relation tutorial

    No relevant information

    New

    Hot