Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Contents

1. What Is Perl?
2. Course Requisites and Goals
3. Perl References & Resources
4. State of Perl
5. Taste of Perl
6. Storing & Running Perl Programs
7. The Elements
8. Literals & Operators
    9. Loops and I/O
10. Grade Book Example
11. Pipe I/O and System Calls
12. Matching
13. Parsing
14. Simple CGI
15. Testing Perl Programs
16. Common Goofs

7. The Elements


Perl's Three Data Structures

  • Scalars can be numeric or character as determined by context:
        123  12.4  5E-10  0xff (hex)  0377 (octal)
    
    
    
        'What you $see is (almost) what \n you get'   'Don\'t Walk'
    
    
    
        "How are you?"  "Substitute values of $x and \n in \" quotes."
    
    
    
        `date`   `uptime -u`   `du -sk $filespec | sort -n`
    
    
    
        $x    $list_of_things[5]     $lookup{'key'}
    Single-quotes ' ' allow no substitution except for \\ and \'. Double-quotes " " allow substitution of variables like $x and control codes like \n (newline). Back-quotes ` ` also allow substitution, then try to execute the result as a system command, returning as the final value whatever the system command outputs.
  • Arrays of scalars (also called lists) are sequentially-arranged scalars:
        ('Sunday', 'Monday', 'Tuesday', 'Wednesday',
    
            'Thursday', 'Friday', 'Saturday')
    
    
    
        (13,14,15,16,17,18,19)   equivalent to  (13..19)
    
    
    
        (13,14,15,16,17,18,19)[2..4]  equivalent to  (15,16,17)
    
    
    
        @whole_list
  • Associative arrays (also called hashes) help you remember things:
        $DaysInMonth{'January'} = 31;   $enrolled{'Joe College'} = 1;
    
    
    
        $StudentName{654321} = 'Joe College';  
    
    
    
        $score{$studentno,$examno} = 89; 
    
    	
    
        %whole_hash

    Perl 5 allows combinations of these, such as lists of lists and associative arrays of lists.

    Name Conventions

    Scalar variables start with '$', even when referring to an array element. The variable name reference for a whole list starts with '@', and the variable name reference for a whole associative array starts with '%'.

    Lists are indexed with square brackets enclosing a number, normally starting with [0]. In Perl 5, negative subscripts count from the end. Thus, $things[5] is the 6th element of array @things, and

        ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[1]
    equals 'Mon'.

    Associative arrays are indexed with curly brackets enclosing a string. $whatever, @whatever, and %whatever are three different variables.

        @days = (31,28,31,30,31,30,31,31,30,31,30,31);
    
                       # A list with 12 elements.
    
    
    
        $#days         # Last index of @days; 11 for above list
    
    
    
        $#days = 7;    # shortens or lengthens list @days to 8 elements
    
    
    
        @days          # ($days[0], $days[1],... )
    
    
    
        @days[3,4,5]   # = (30,31,30)
    
    
    
        @days{'a','c'} # same as ($days{'a'},$days{'c'})
    
    
    
        %days          # (key1, value1, key2, value2, ...)
    
    

    Case is significant--"$FOO", "$Foo" and "$foo" are all different variables. If a letter or underscore is the first character after the $, @, or %, the rest of the name may also contain digits and underscores. If this character is a digit, the rest must be digits. Perl has several dozen special variables whose second character is non-alphanumeric. For example, $/ is the input record separator, newline "\n" by default. An uninitialized variable has a special "undefined" value which can be detected by the function defined(). Undefined values convert depending on context to 0, null, or false.

    The variable "$_" Perl presumes when needed variables are not specified. Thus:

        <STDIN>;          assigns a record from filehandle STDIN to $_
    
        print;            prints the curent value of $_
    
        chop;             removes the last character from $_
    
        @things = split;  parses $_ into white-space delimited
    
                          words, which become successive
    
                          elements of list @things.
    $_, $1, $2, $3, and other implicit variables contribute to Perl Paradox Number Two: What you don't see can help you or hurt you. See Quick Reference Guide Section 25, Special Variables.

    Subroutines and functions are referenced with an initial '&', which is optional if reference is obviously a subroutine or function such as following the sub, do, and sort directives:

        sub square { return $_[0] ** 2; }
    
    
    
        print "5 squared is ", &square(5);

    Filehandles don't start with a special character, and so as to not conflict with reserved words are most reliably specified as uppercase names: INPUT, OUTPUT, STDIN, STDOUT, STDERR, etc.

  •  
     
    [an error occurred while processing this directive]