Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
List Value Constructors
This section describes what is a list value constructor, a list of scalar values separated by commas. Elements of a list value can be accessed with the subscription notation [i].
List value constructors are used to construct list values. A list value represents an ordered list of scalar values indexed by numbers starting with 0.
A list value constructor must follow these rules:
Here are some good and bad examples of list value constructors:
'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 Jan, Feb, Mar; # bad, need quotes
An element in a list value can be accessed by the subscription notation, [index]. In this case, you need to use parentheses to protect the list value. 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 Perl script with list subscriptions:
#- ListValue.pl #- Copyright (c) HerongYang.com. All Rights Reserved. # print((3,5,7,11)[0], "\n"); # 3 print(('hello')[0], "\n"); # hello print(('hello')[1], "\n"); # (undefined) print(('hello')[-1], "\n"); # hello print(('Mon','Tue','Wed')[1], "\n"); # Tue print((Mon,Tue,Wed)[-1], "\n"); # Wed
Table of Contents
►Data Types: Values and Variables
Variables - Scalar, Array and Hash
"undef" Value and Undefined Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
CGI.pm Module for Building Web Pages
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary