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

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

Sections in This Chapter

What Is an Expression?

Expression Evaluation Context

Simple Statements and Modifiers

Dr. Herong Yang, updated in 2008
What Is an Expression?