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

What Is an Expression?

This section describes what is an expression, a mixture of values, variables, operations of expressions and function calls. A tutorial example is provided to show you examples of expressions..

An expression is a mixture of values, operations of expressions and function calls that can be evaluated to a value. Examples of expressions are:

  • A scalar value is an expression: 9.99, "Hello world!", ...
  • A list value is an expression: ("PI", 3.14, "Price", 9.99), ...
  • The undef value is an expression: undef
  • A scalar, array, or hash variable is an expression: $price, @links, %siteRanks, ...
  • An arithmetic operation is an expression: 1+2, 1/3, $price * 5, ...
  • A string concatenation operation is an expression: "Hello ".$name, ...
  • A comparison operation is an expression: $sales > $expenses, $price == undef, ...
  • A function call is an expression: sort(@links), undef($price), ...
  • A list value subscription is an expression: ('Mon','Tue','Wed')[1], ...
  • A array variable subscription is an expression: $links[1], ...
  • A hash variable subscription is an expression: $siteRanks{"herongyang.com"}, ...
  • An expression can have multiple operations: $capital*(1+$interest)**$years, ...
  • There are many more types of expression examples not listed above.

Here is a Perl tutorial script showing you examples of expressions:

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

   print(9.99, "\n");                  # A scalar numeric value

   print("Hello world!", "\n");        # A scalar string value

   print(("PI", 3.14, "Price", 9.99), "\n");        
                                       # A list value

   print(undef, "\n");                 # The undef value

   $price = 9.99;
   print($price, "\n");                # A scalar variable
   
   @links = ("herongyang.com", "perl.org", "cpan.org");
   print(@links, "\n");                # An array variable

   %siteRanks = ("herongyang.com", 4, "perl.org", 6, "google.com", 9);
   print(%siteRanks, "\n");            # A hash variable

   print($price*5, "\n");              # An arithmetic operation

   $name = "Herong";
   print("Hello ".$name, "\n");        # A string concatenation

   $sales = 876.54;
   $expenses = 654.32;
   print($sales>$expenses, "\n");      # A comparison operation

   print(sort(@links), "\n");          # A function call

   print(('Mon','Tue','Wed')[1], "\n");
                                       # A list value subscription

   print($links[1], "\n");             # An array subscription

   print($siteRanks{"herongyang.com"}, "\n");
                                       # A hash subscription

   $capital = 100.00;
   $interest = 0.0475;
   $years = 5;
   print($capital*(1+$interest)**$years, "\n");
                                       # Multiple operations

Here is the output of the tutorial script:

9.99
Hello world!
PI3.14Price9.99

9.99
herongyang.comperl.orgcpan.org
herongyang.com4google.com9perl.org6
49.95
Hello Herong
1
cpan.orgherongyang.comperl.org
Tue
perl.org
4
126.115991387686

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

 Printable Copy - PDF Version

What Is an Expression? - Updated in 2014, by Dr. Herong Yang