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

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:

  • A list value constructor is a list of individual scalar values separated by commas. For example, 'hello' and 3,5,7,11.
  • You should use parentheses to protect a list value constructor as a single unit to avoid confusions. For example, ('hello') and (3,5,7,11).
  • If you do use parentheses, then you don't have to put string literals in quotes. For example, (hello) and (Mon,Tue,Wed).

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) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   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

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

Data Types: Values and Variables

 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

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 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 Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 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

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
List Value Constructors