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

Using Scalar Variables

This section describes how a scalar variable can be assigned with a scalar value. An unassigned scalar variable contains the (undef) value. The undef() function removes the assigned value from a scalar variable.

Scalar variables can be used in a Perl script under these rules:

  • A scalar variable should be assigned with a scalar value constructor using an assignment operation. For example, $price = 19.99.
  • A scalar variable without any scalar value assigned is an undefined variable and contains a special value called, undef.
  • The assigned value can be removed from a scalar variable using the undef() function.
  • A scalar variable can be used in a numeric operation, where the value contained in the scalar variable will be interpreted as a number.
  • A scalar variable can be used in a string operation, where the value contained in the scalar variable will be interpreted as a string.
  • A scalar variable can be used in a Boolean operation, where the value contained in the scalar variable will be interpreted as TRUE or FALSE.

See the previous section for scalar value interpretation rules.

Here are some examples of scalar variable assignment operations with scalar value constructors:

   $one = 1;
   $x = 8.8;
   $big = 1e+50;
   $pi = '3.14';
   $msg = 'hello';
   $cmd = 'dir \\home\\herong';
   $title = "Herong's Notes";

Here is a Perl tutorial script on how to use scalar variables:

#- ScalarVariable.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#
   $price;                             # Undefined
   print($price, "\n");                # (blank)

   $price = 19.99;                     # Defined
   print($price, "\n");                # 19.99  

   $price = $price * 80/100;           # In an arithmetic operation
   print($price, "\n");                # 16.992

   $price = "Market price";            # Changed to a string value
   print($price, "\n");                # Market price

   $price = reverse($price);           # In a function call
   print($price, "\n");                # ecirp tekraM

   $shipping = 3.99;
   $price = $price + $shipping;        # Auto-converted to a number: 0
   print($price, "\n");                # 3.99

   undef($price);                      # Undefined again
   print($price, "\n");                # (blank)

Here is the output of the tutorial script:

19.99
15.992
Market price
ecirp tekraM
3.99

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
Using Scalar Variables