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:

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:

#- Copyright (c) All Rights Reserved.
   $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:

Market price
ecirp tekraM

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

 Image and Picture Processing

 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 Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials


 Full Version in PDF/EPUB