Expression Evaluation Context

This section describes expression evaluation contexts: scalar context and list context. Operations and function may behave differently in different context. A list value in a scalar context is converted by taking the last value.

Depending on where it is used, an expression will be evaluated under two different context, Scalar Context and List Context.

1. Scalar Context - When an expression is used as an argument of an operation that requires a scalar value, the expression will be evaluated under a scalar context. For example, 9.99 * EXPR requires EXPR to be evaluated in a scalar context.

2. List Context - When an expression is used as an argument of an operation that requires a list value, the expression will be evaluated under a list context. For example, sort(EXPR) requires EXPR to be evaluated in a list context.

Expressions behave differently in different contexts following these rules:

Here is a Perl tutorial script on how expressions are evaluated in a scalar or list context:

#- ExpressionContext.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#

   @links = ("herongyang.com", "perl.org", "cpan.org");

   $string = join(', ',sort(@links));   # sort() in a list context
   print($string, "\n");

   $string = "My bookmarks: ".sort(@links);
                                        # sort() in a scalar context
   print($string, "\n");

   $interest = 39.99;
   $capital = 1234.56;
   @rates = $interest/$capital;         # converting scalar to list
   print(@rates, "\n");

   $interest = 100*(0.02, 0.05, 0.03);  # converting list to scalar   
   print($interest, "\n");

Here is the output of the tutorial script:

cpan.org, herongyang.com, perl.org
My bookmarks:
0.0323921073094868
3

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

Expressions, Operations and Simple Statements

 What Is an Expression?

Expression Evaluation Context

 Simple Statements and Modifiers

 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

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version