Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
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:
#- ScalarVariable.pl #- Copyright (c) HerongYang.com. 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:
19.99 15.992 Market price ecirp tekraM 3.99
Table of Contents
►Data Types: Values and Variables
Variables - Scalar, Array and Hash
"undef" Value and Undefined Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
Converting Perl Script to Executable Binary