Impatient Perl
version: 24 July 2004
Copyright 2004 Greg London
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".
Cover Art (Front and Back) on the book version of Impatient Perl is excluded from this license. Cover Art is Copyright Greg London 2004, All Rights Reserved.
For latest version of this work go to:
This document was created using OpenOffice version 1.1.0
(which exports directly to PDF)
running RedHat Linux
on a x86 machine from
http://www.penguincomputing.com
To buy a hardcopy of this document, go to:
http://www.lulu.com/GregLondon
It's only $8.50 plus shipping and handling.
And your purchase earns me 97 cents in royalties,
a large chunk of which will go to taxes.
The life you save may be my own.
Thank you.
Table of Contents
1 The Impatient Introduction to Perl 6
1.1 The history of perl in 100 words or less 6
1.2 Basic Formatting for this Document 6
1.3 Do You Have Perl Installed 7
1.4 Your First Perl Script, EVER 7
1.7 Cheap Reference Material 9
2 Storage 10
2.1 Scalars 10
2.1.1.2 Single quotes versus Double quotes 11
2.1.1.9 join 13
2.1.1.10 qw 13
2.1.1.11 Multi-Line Strings, HERE Documents 14
2.1.2.3 abs 15
2.1.2.4 int 15
2.1.2.5 trigonometry (sin,cos,tan) 16
2.1.2.7 sqrt 16
2.1.2.8 natural logarithms(exp,log) 16
2.1.2.9 random numbers (rand, srand) 17
2.1.3 Converting Between Strings and Numbers 17
2.1.3.2.3 Base Conversion Overview 19
2.1.4 Undefined and Uninitialized Scalars 20
2.1.5.2 TRUE 22
2.1.5.4.4 Assignment Precedence 25
2.1.5.4.5 Flow Control Precedence 25
2.1.5.4.6 Conditional Operator 26
2.2 Arrays 28
2.2.5 unshift( @array, LIST) 31
2.2.10 Undefined and Uninitialized Arrays 34
2.3 Hashes 34
2.3.1 exists ( $hash{$key} ) 35
2.3.2 delete ( $hash{key} ) 37
2.5.2 References to Named Referents 46
2.5.5 Complex Data Structures 49
2.5.5.2 Multidimensional Arrays 51
2.5.5.3 Deep Cloning, Deep Copy 52
2.5.6 Stringification of References 52
3.1 Labels 56
4 Packages and Namespaces and Lexical Scoping 56
4.2 Declaring Package Variables With our 57
4.3 Package Variables inside a Lexical Scope 58
4.6.1 Reference Count Garbage Collection 61
4.6.2 Garbage Collection and Subroutines 61
4.7 Package Variables Revisited 62
4.8 Calling local() on Package Variables 63
5.4 Data::Dumper and subroutines 66
5.5 Passing Arguments to/from a Subroutine 67
5.6 Accessing Arguments inside Subroutines via @_ 67
5.7 Dereferencing Code References 68
5.9 Subroutine Return Value 69
5.11 Using the caller() Function in Subroutines 70
5.12 The caller() function and $wantarray 71
5.13 Context Sensitive Subroutines with wantarray() 72
6 Compiling and Interpreting 72
9 The use Statement, Formally 74
9.3 The PERL5LIB and PERLLIB Environment Variables 76
9.5 MODULENAME -> import (LISTOFARGS) 76
9.6 The use Execution Timeline 77
10 bless() 78
11.3 INVOCANT->isa(BASEPACKAGE) 83
11.4 INVOCANT->can(METHODNAME) 83
13.1 Class 86
13.3 SUPER 87
14.1 Modules 89
14.4 Methods 90
14.6 Overriding Methods and SUPER 91
15 CPAN 91
15.3 Plain Old Documentation (POD) and perldoc 94
15.4 Creating Modules for CPAN with h2xs 94
17.1 @ARGV 96
17.2.1 Getopt::Declare Sophisticated Example 99
18.1 open 101
18.2 close 101
18.3 read 101
18.4 write 102
18.5 File Tests 103
19 Operating System Commands 104
19.1 The system() function 104
19.2 The Backtick Operator 105
19.3 Operating System Commands in a GUI 105
20.1 Variable Interpolation 107
20.5 Capturing and Clustering Parenthesis 111
20.5.1 $1, $2, $3, etc Capturing parentheses 111
20.5.2 Capturing parentheses not capturing 112
20.6.1 Metacharacters Within Character Classes 112
20.7 Shortcut Character Classes 113
20.8 Greedy (Maximal) Quantifiers 113
20.9 Thrifty (Minimal) Quantifiers 114
20.10 Position Assertions / Position Anchors 115
20.11 Modifiers 118
20.11.2 The m And s Modifiers 118
20.12 Modifiers For m{} Operator 121
20.13 Modifiers for s{}{} Operator 121
20.14 Modifiers for tr{}{} Operator 121
This document is for people who either want to learn perl or are already programming in perl and just do not have the patience to scrounge for information to learn and use perl. This document should also find use as a handy desk reference for some of the more common perl related questions.
In the mid 1980s, Larry Wall was working as a sys-admin and found that he needed to do a number of common, yet oddball functions over and over again. And he did not like any of the scripting languages that were around at the time, so he invented Perl. Version 1 was released circa 1987. A few changes have occurred between then and now. The current version of Perl has exceeded 5.8.0 and is a highly recommended upgrade.
Perl 6 is on the drawing board as a fundamental rewrite of the language. It is not available yet, and probably will not be available for some time.
This document is formatted into text sections, code sections, and shell sections. This sentence is part of a text section. Text sections will extend to the far left margin and will use a non-monospaced font. Text sections contain descriptive text.
Code sections are indented.
They also use a monospaced font.
This is a code section, which represents
code to type into a script.
You will need to use a TEXT EDITOR,
not a WORD PROCESSOR to create these files.
Generally, the code is contained in one file,
and is executed via a shell command.
If the code section covers multiple files,
each file will be labeled.
###filename:MyFile.pm
This code will be placed in a
file called MyFile.pm
#!/usr/local/env perl
###filename:myscript.pl
This code will be placed in a file
called myscript.pl
The first line of myscript.pl will be the
line with #!/usr/local/env perl
> shell sections are indented like code sections
> shell sections also use monospaced fonts.
> shell sections differ from code sections in
> that shell sections start with a '>' character
> which represents a shell prompt.
> shell sections show commands to type on
> the command line.
> shell sections also show the output of a script,
> if any exists.
> In simple examples, the code is shown in a
> code section, immediately followed by the output
> from running the script. The command to run
> the script is dropped to save space.
As an example, the code for a simple "Hello World" script is shown here. It can be typed into a file of any name. The name of the file is not important. The command to execute the script is not important either. In this example, the code is important, and the output is important, so they are they only things shown.
print "Hello World\n";
> Hello World
THIS DOCUMENT REFERS TO (LI/U)NIX PERL ONLY. Much of this will translate to Mac Perl and Windows Perl, but the exact translation will be left as an exercise to the reader.
To find out if you have perl installed and its version:
> perl -v
You should have at least version 5.004. If you have an older version or if you have no perl installed at all, you can download it for free from
CPAN is an acronym for Comprehensive Perl Archive Network. The CPAN site contains the latest perl for download and installation, as well as a TON of perl modules for your use.
If you are a beginner, get your sys-admin to install perl for you. Even if you are not a beginner, get your sys-admin to install perl for you.
Create a file called hello.pl using your favorite text editor. Type in the following:
#!/usr/bin/env perl
use warnings;
use strict; # comment
use Data::Dumper;
print "Hello World \n";
(The #! on the first line is sometimes pronounced "shebang")
(The .pl extension is simply a standard accepted extension for perl scripts.)
Run the script:
> perl hello.pl
Hello World
This calls perl and passes it the name of the script to execute. You can save yourself a little typing if you make the file executable:
> chmod +x hello.pl
And then run the script directly.
> hello.pl
Hello World
If "." is not in your PATH variable, you will have to run the script by typing:
> ./hello.pl
HOORAY! Now go update your resume.
Anything from a # character to the end of the line is a comment.
All the code examples are assumed to have the following script header, unless otherwise stated. It uses your PATH environment variable to determine which perl executable to run. If you need to have different versions of perl installed on your system, you can control which version of perl they will run by changing your PATH variable without having to change your script.
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
You can get quick help from the standard perl installation.
> perl -h
> perldoc
> perldoc -h
> perldoc perldoc
FAQs on CPAN: http://www.cpan.org/cpan-faq.html
Mailing Lists on CPAN: http://list.cpan.org
More free documentation on the web: http://www.perldoc.com
Still more free documentation on the web: http://learn.perl.org
"Programming Perl" by Larry Wall, Tom Christiansen, and Jon Orwant. Highly recommended book to have handy at all times. It is sometimes referred to as the "Camel Book" by way of the camel drawing on its cover. The publisher, O'Reilly, has printed enough computer books to choke a, well, camel, and each one has a different animal on its cover. Therefore if you hear reference to some animal book, it is probably an O'Reilly book. Well, unless its the "Dragon Book", because that refers to a book called "Compilers" by Aho, Sethi, and Ullman.
Perl: Originally, "Pearl" shortened to "Perl" to gain status as a 4-letter word. Now considered an acronym for Practical Extraction and Report Language, as well as Pathologically Eclectic Rubbish Lister. The name was invented first. The acronyms followed. Note that this is "Perl" with a capital "P". The "perl" with a lower case "p" refers to the exectuable found somewhere near /usr/local/bin/perl or via /bin/env perl.
CPAN: Comprehensive Perl Archive Network. See http://www.cpan.org for more.
DWIM: Do What I Mean. Once upon a time, the standard mantra for computer inflexibility was this: "I really hate this darn machine, I wish that they would sell it. It never does what I want, but only what I tell it." DWIM-iness is an attempt to embed perl with telepathic powers such that it can understand what you wanted to write in your code even though you forgot to actually type it. Well, alright, DWIM is just a way of saying the language was designed by some really lazy programmers so that you could be even lazier than they were. (They had to write perl in C, so they could not be TOO lazy.)
AUTOVIVIFY: "auto" meaning "self". "vivify" meaning "alive". To bring oneself to life. Generally applies to perl variables that can grant themselves into being without an explicit declaration from the programmer. Part of perl's DWIM-ness. "Autovivify" is a verb. The noun form is "autovivification". Sometimes, autovivification is not what you meant your code to do, and for some reason, when "do what I mean" meets autovivification in perl, autovivification wins. And now, a Haiku:
Do What I Mean and
Autovivification
sometimes unwanted
TMTOWTDI: There is More Than One Way To Do It. An acknowledgement that any programming problem has more than one solution. Rather than have perl decide which solution is best, it gives you all the tools and lets you choose. This allows a programmer to select the tool that will let him get his job done. Sometimes, it gives a perl newbie just enough rope to hang himself.
Foo Fighters: A phrase used around the time of WWII by radar operators to describe a signal that could not be explained. Later became known as a UFO. This has nothing to do with perl, except that "foo" is a common variable name used in perl.
Fubar: Another WWII phrase used to indicate that a mission had gone seriously awry or that a piece of equipment was inoperative. An acronym for Fouled Up Beyond All Recognition and similar interpretations. This has nothing to do with perl either, except that fubar somehow got mangled into foobar, and perl is often awash in variables named "foo" and "bar", especially if the programmer wishes to hide the fact that he did not understand his code well enough to come up with better names. If you use a $foo variable in your code, you deserve to maintain it.
Perl has three basic storage types: Scalars, Arrays, and Hashes.
The most basic storage type is a Scalar.
Arrays and Hashes use Scalars to build more complex data types.
Scalars are preceded with a dollar sign sigil. A "$" is a stylized "S".
sigil : A symbol. In Perl a sigil refers to the symbol in front of a variable.
Scalars can store Strings, Numbers (integers and floats), References, and Filehandles.
Perl is smart enough to know which type you are putting into a scalar and handle it.
my $diameter = 42;
my $pi = 3.1415;
my $initial = 'g';
my $name = 'John Doe';
my $ref_to_name = \$name
Without "use strict;" and without declaring a variable with a "my", using a variable causes perl to create one and initialize it to undef. This undef value will stringify to "" or numify to 0, depending how the undefined variable is used. This is called autovivication. (Stringification and Numification are covered later.)
Autovivify : to bring oneself to life.
In some situations, autovivication is handy. However, in certain situations, autovivification can be an unholy monster.
my $circumference = $pie * $diameter;
# oops, $pie doesn't exist. Autovivified to undef,
# numified to 0, therefore $circumference is zero.
Without use warnings; use strict; perl will autovivify a new variable called "pie", initialize it to zero, and assume that is what you meant to do. There is no reason that warnings and strictness should not be turned on in your scripts.
Scalars can store strings. You do not have to declare the length of the string, perl just handles it for you automatically.
String literals must be in single or double quotes or you will get an error.
print hello;
Error: Unquoted string "hello" may clash with reserved word
You can use single quotes or double quotes to set off a string literal:
my $name = 'mud';
my $greeting = "hello, $name\n";
print $greeting;
> hello, mud
You can also create a list of string literals using the qw() function.
my ($first,$last)=qw( John Doe );
print "first is '$first'\n";
print "last is '$last'\n";
> first is 'John'
> last is 'Doe'
Single quoted strings are a "what you see is what you get" kind of thing.
my $name = 'mud';
print 'hello $name';
> hello $name
Double quotes means that you get SOME variable interpolation during string evaluation. Complex variables, such as a hash lookup, will not be interpolated properly in double quotes.
my $name = 'mud';
print "hello $name \n";
> hello mud
Note: a double-quoted "\n" is a new-line character.
You may get rid of a newline character at the end of a string by chomp-ing the string. The chomp function removes one new line from the end of the string even if there are multiple newlines at the end. If there are no newlines, chomp leaves the string alone. The return value of chomp is what was chomped (seldom used).
My $string = "hello world\n";
chomp($string);
warn "string is '$string' \n"
> string is 'hello world' ...
String concatenation uses the period character "."
my $fullname = 'mud' . "bath";
Repeat a string with the "x" operator.
my $line = '-' x 80; # $line is eighty hypens
Find out how many characters are in a string with length().
my $len = length($line); # $len is 80
substr ( STRING_EXPRESSION, OFFSET, LENGTH);
Spin, fold, and mutilate strings using substr(). The substr function gives you fast access to get and modify chunks of a string. You can quickly get a chunk of LENGTH characters starting at OFFSET from the beginning or end of the string (negative offsets go from the end). The substr function then returns the chunk.
my $chunk = substr('the rain in spain', 9, 2);
warn "chunk is '$chunk'";
> chunk is 'in' ...
The substr function can also be assigned to, replacing the chunk as well. You need a string contained in a variable that can be modified, rather than using a constant literal in the example above.
my $string = 'the rain in spain';
substr($string, 9, 2) = 'beyond';
warn "string is '$string'";
> string is 'the rain beyond spain' ...
split(/PATTERN/, STRING_EXPRESSION,LIMIT);
Use the split function to break a string expression into components when the components are separated by a common substring pattern. For example, tab separated data in a single string can be split into separate strings.
my $tab_sep_data = "John\tDoe\tmale\t42";
my ($first,$last,$gender,$age)
= split(/\t/, $tab_sep_data);
You can break a string into individual characters by calling split with an empty string pattern "". The /PATTERN/ in split() is a Regular Expression, which is complicated enough to get its own chapter. However, some common regular expression PATTERNS for split are:
\t tab-separated data
\s+ whitespace-separated data
\s*,\s* comma-separated data
join('SEPARATOR STRING', STRING1, STRING2, ...);
Use join to stitch a list of strings into a single string.
my $string = join(" and ",
'apples', 'bananas', 'peaches');
warn "string is '$string'";
> string is 'apples and bananas and peaches'...
The qw() function takes a list of barewords and quotes them for you.
my $string =
join(" and ", qw(apples bananas peaches));
warn "string is '$string'";
> string is 'apples and bananas and peaches'...
Perl allows you to place a multi-line string in your code by using what it calls a "here document”.
My $string = <<”ENDOFDOCUMENT”;
Do What I Mean and
Autovivification
sometimes unwanted
ENDOFDOCUMENT
warn "string is '$string'”;
> string is 'Do What I Mean and
> Autovivification
> sometimes unwanted' at ...
The '<<' indicates a HERE document, followed by the name of the label indicating the end of the here document. Enclosing the label in double quotes means that perl variables in the document will get interpolated as strings. Enclosing the label in single quotes means that no string interpolation occurs.
Perl then reads the lines after the '<<' as string literal content until it sees the end of string label positioned at the beginning of a line.
Perl generally uses floats internally to store numbers. If you specify something that is obviously an integer, it will use an integer. Either way, you simply use it as a scalar.
my $days_in_week = 7; # scalar => integer
my $temperature = 98.6; # scalar => float
Perl allows several different formats for numeric literals, including integer, floating point, and scientific notation, as well as decimal, octal, and hexadecimal.
Binary numbers begin with "0b"
hexadecimal numbers begin with "0x"
Octal number begin with a "0"
All other numeric literals are assumbed to be decimal.
my $solar_temp_c = 1.5e7; # centigrade
my $solar_temp_f = 27_000_000.0; # fahrenheit
my $base_address = 01234567; # octal
my $high_address = 0xfa94; # hexadecimal
my $low_address = 0b100101; # binary
Use abs to get the absolute value of a number.
my $var1 = abs(-3.4); # var1 is 3.4
my $var2 = abs(5.9); # var2 is 5.9
Use "int" to convert a floating point number to an integer. Note that this truncates everything after the decimal point, which means you do NOT get rounding. Truncating means that positive numbers always get smaller and negative numbers always get bigger.
my $price = 9.95;
my $dollars = int ($price);
# dollars is 9, not 10! false advertising!
my $y_pos = -5.9;
my $y_int = int($y_pos);
# y_int is -5 (-5 is "bigger" than -5.9)
If you want to round a float to the nearest integer, you will need to write a bit of code. One way to accomplish it is to use sprintf:
my $price = 9.95;
my $dollars = sprintf("%.0f", $price);
# dollars is 10
The sin, cos, and tan functions return the sine, cosine, and tangent of a value given in RADIANS. If you have a value in DEGREES, multiply it by (pi/180) first.
my $angle = 45; # 45 deg
my $radians = $angle * ( 3.14 / 180 ); # .785 rad
my $sine_deg = sin($angle); # 0.707
my $sine_rad = sin($radians); # 0.707
If you need inverse sine, cosine, or tangent, then use the Math::Trig module on CPAN.
Use the "**" operator to raise a number to some power.
my $seven_squared = 7 ** 2; # 49
my $five_cubed = 5 ** 3; #125
my $three_to_the_fourth = 3 ** 4; # 81
Use fractional powers to take a root of a number:
my $square_root_of_49 = 49 ** (1/2); # 7
my $cube_root_of_125 = 125 ** (1/3); # 5
my $fourth_root_of_81 = 81 ** (1/4); # 3
Standard perl cannot handle imaginary numbers. Use the Math::Complex module on CPAN.
Use sqrt to take the square root of a positive number.
my $square_root_of_123 = sqrt(123); # 11.0905
The exp function returns e to the power of the value given. To get e, call exp(1);
my $value_of_e = exp(1); # 2.7183
my $big_num= exp(42); # 2.7183 ** 42 = 1.7e18
The log function returns the inverse exp() function, which is to say, log returns the number to which you would have to raise e to get the value passed in.
my $inv_exp = log($big_num); # inv_exp = 42
If you want another base, then use this subroutine:
sub log_x_base_b {return log($_[0])/log($_[1]);}
# want the log base 10 of 12345
# i.e. to what power do we need to raise the
# number 10 to get the value of 12345?
my $answer = log_x_base_b(12345,10); # answer = 4.1
Note that inverse natural logs can be done with exponentiation, you just need to know the value of the magic number e (~ 2.718281828). The exp function is straightforward exponentiation:
# big_num = 2.7183 ** 42 = 1.7e18
my $big_num = $value_of_e ** 42;
Natural logarithms simply use the inverse of the value (i.e. 1/value) with exponentiation.
# inv_exp = 2.7183 ** (1/1.7e18) = 42
my $inv_exp = $value_of_e ** (1/$big_num);
The rand function is a pseudorandom number generator (PRNG).
If a value is passed in, rand returns a number that satisfies ( 0 <= return <=input )
If no value is passed in, rand returns a number in the range ( 0 <= return < 1 )
The srand function will seed the PRNG with the value passed in. If no value is passed in, srand will seed the PRNG with something from the system that will give it decent randomness. You can pass in a fixed value to guarantee the values returned by rand will always follow the same sequence (and therefore are predictable). You should only need to seed the PRNG once. If you have a version of perl greater than or equal to 5.004, you should not need to call it at all, because perl will call srand at startup.
Many languages require the programmer to explicitely convert numbers to strings before printing them out and to convert strings to numbers before performing arithemetic on them. Perl is not one of these languages.
Perl will attempt to apply Do What I Mean to your code and just Do The Right Thing. There are two basic conversions that can occur: stringification and numification.
Stringify: Converting something other than a string to a string form.
Perl will automatically convert a number (integer or floating point) to a string format before printing it out.
my $mass = 7.3;
my $volume = 4;
warn "mass is '$mass'\n";
warn "volume is '$volume'\n";
> mass is '7.3' ...
> volumn is '4' ...
Even though $mass is stored internally as a floating point number and $volume is stored internally as an integer, the code did not have to explicitely convert these numbers to string format before printing them out. Perl will attempt to convert the numbers into the appropriate string representation. If you do not want the default format, use sprintf.
If you want to force stringification, simply concatenate a null string onto the end of the value.
my $mass = 7.3; # 7.3
my $string_mass = $mass .= ''; # '7.3'
Use sprintf to control exactly how perl will convert a number into string format.
sprintf ( FORMAT_STRING, LIST_OF_VALUES );
For example:
my $pi = 3.1415;
my $str = sprintf("%06.2f",$pi);
warn "str is '$str'";
> str is '003.14' ...
Decoding the above format string:
% => format
0 => fill leading spaces with zero
6 => total length, including decimal point
.2 => put two places after the decimal point
f => floating point notation
To convert a number to a hexadecimal, octal, binary, or decimal formated string, use the following FORMAT_STRINGS:
hexadecimal "%lx" The letter 'l' (L)
octal "%lo" indicates the input is
binary "%lb" an integer, possibly
decimal integer "%ld" a Long integer.
decimal float "%f"
scientific "%e"
Numify: Converting something other than a number to a numeric form.
Sometimes you have string information that actually represents a number. For example, a user might enter the string "19.95" which must be converted to a float before perl can perform any arithemetic on it.
You can force numification of a value by adding integer zero to it.
my $user_input = '19.95'; # '19.95'
my $price = $user_input+0; # 19.95
If the string is NOT in base ten format, then use oct() or hex()
The oct function can take a string that fits the octal, hexadecimal, or binary format and convert it to an integer.
binary formatted strings must start with "0b"
hexadecimal formatted strings must start with "0x"
All other numbers are assumed to be octal strings.
Note: even though the string might not start with a zero (as required by octal literals), oct will assume the string is octal. This means calling oct() on a decimal number could be a bad thing.
To handle a string that could contain octal, hexadecimal, binary, OR decimal strings, you could assume that octal strings must start with "0". Then, if the string starts with zero, call oct on it, else assume it's decimal. This example uses regular expressions and the conditional operator.
my $num = ($str=~m{^0}) ? oct($str) : $str + 0;
The hex() function takes a string in hex format and converts it to integer. The hex() function is like oct() except that hex() only handles hex base strings, and it does not require a "0x" prefix.
Given a decimal number:
my $decimal=12;
Convert from decimal to another base using sprintf:
my $hex = sprintf("%lx", $decimal);
my $oct = sprintf("%lo", $decimal);
my $bin = sprintf("%lb", $decimal);
If you want to pad the most significant bits with zeroes and you know the width, use this:
# 08 assumes width is 8 characters
my $p_hex = sprintf("%08lx", $decimal);
my $p_oct = sprintf("%08lo", $decimal);
my $p_bin = sprintf("%08lb", $decimal);
If you have a string and you want to convert it to decimal, use the conditional operator and oct().
sub convert_to_decimal
{ ($_[0]=~m{^0}) ? Oct($_[0]) : $_[0] + 0; }
warn convert_to_decimal('42'); # dec
warn convert_to_decimal('032'); # oct
warn convert_to_decimal('0xff'); # hex
warn convert_to_decimal('0b1001011'); # bin
If you want to know how many bits it would take to store a number, convert it to binary using sprintf (don't pad with zeroes) and then call length() on it.
warn length(sprintf("%lb", 255)); # 8
All the examples above initialized the scalars to some known value before using them. You can declare a variable but not initialize it, in which case, the variable is undefined.
If you use a scalar that is undefined, perl will stringify or numify it based on how you are using the variable.
An undefined scalar stringifies to an empty string: ""
An undefined scalar numifies to zero: 0
Without warnings or strict turned on, this conversion is silent. With warnings/strict on, the conversion still takes place, but a warning is emitted.
Since perl automatically performs this conversion no matter what, there is no string or arithematic operation that will tell you if the scalar is undefined or not.
Use the defined() function to test whether a scalar is defined or not.
If the scalar is defined, the function returns a boolean "true" (1)
If the scalar is NOT defined, the function returns a boolean "false" ("").
If you have a scalar with a defined value in it, and you want to return it to its uninitialized state, assign undef to it. This will be exactly as if you declared the variable with no initial value.
my $var; # undef
print "test 1 :";
if(defined($var)) {print "defined\n";}
else {print "undefined\n";}
$var = 42; # defined
print "test 2 :";
if(defined($var)) {print "defined\n";}
else {print "undefined\n";}
$var = undef; # undef as if never initialized
print "test 3 :";
if(defined($var)) {print "defined\n";}
else {print "undefined\n";}
> test 1 :undefined
> test 2 :defined
> test 3 :undefined
Perl does not have a boolean "type" per se. Instead, perl interprets scalar strings and numbers as "true" or "false" based on some rules:
Strings "" and "0" are FALSE,
any other string or stringification is TRUE
2) Number 0 is FALSE, any other number is TRUE
3) all references are TRUE
4) undef is FALSE
Note that these are SCALARS. Any variable that is not a SCALAR is first evaluated in scalar context, and then treated as a string or number by the above rules. The scalar context of an ARRAY is its size. An array with one undef value has a scalar() value of 1 and is therefore evaluated as TRUE.
A subroutine returns a scalar or a list depending on the context in which it is called. To explicitely return FALSE in a subroutine, use this:
return wantarray() ? () : 0; # FALSE
This is sufficiently troublesome to type for such a common thing that an empty return statement within a subroutine will do the same thing:
return; #FALSE
The following scalars are interpreted as FALSE:
integer 0 # false
float 0.0 # false
string '0' # false
string '' # false
undef # false
ALL other values are interpreted as TRUE, which means the following scalars are considered TRUE, even though you may have expected some of them to be false.
string '0.0' # true
string '00' # true
string 'false' # true
float 3.1415 # true
integer 11 # true
string 'yowser' # true
If you are doing a lot of work with numbers on a variable, you may wish to force numification on that variable ($var+0) before it gets boolean tested, just in case you end up with a string "0.0" instead of a float 0.0 and get some seriously hard to find bugs.
Note that the string '0.0' is TRUE, but ('0.0'+0) will get numified to 0, which is FALSE. If you are processing a number as a string and want to evaluate it as a BOOLEAN, make sure you explicitely NUMIFY it before testing its BOOLEANNESS.
Built in Perl functions that return a boolean will return an integer one (1) for TRUE and an empty string ("") for FALSE.
Comparison operators return booleans, specifically an integer 1 for true and a null string "" for false. The "Comparison" operator ("<=>" and "cmp") return a -1, 0, or +1, indicating the compared values are less than, equal to, or greater than. Distinct comparison operators exist for comparing strings and for comparing numbers.
|
Function |
String |
Numeric |
|---|---|---|
|
equal to |
eq |
== |
|
not equal to |
ne |
!= |
|
less than |
lt |
< |
|
greater than |
gt |
> |
|
less than or equal to |
le |
<= |
|
greater than or equal to |
ge |
>= |
|
Comparison (<-1, ==0,>1) |
cmp |
<=> |
Note that if you use a string operator to compare two numbers, you will get their alphabetical string comparison. Perl will stringify the numbers and then perform the compare. This will occur silently; perl will emit no warning. And if you wanted the numbers compared numerically but used string comparison, then you will get the wrong result when you compare the strings ("9" lt "100").
String "9" is greater than (gt) string "100".
Number 9 is less than (<=) number 100.
If you use a numeric operator to compare two strings, perl will attempt to numify the strings and then compare them numerically. Comparing "John" <= "Jacob" will cause perl to convert "John" into a number and fail miserably. However, if warnings/strict is not on, it will fail miserably and SILENTLY, assigning the numification of "John" to integer zero.
The numeric comparison operator '<=>' is sometimes called the "spaceship operator".
Perl has two sets of operators to perform logical AND, OR, NOT functions. The difference between the two is that one set has a higher precedence than the other set.
The higher precedence logical operators are the '&&', '||', and '!' operators.
|
function |
operator |
usage |
return value |
|---|---|---|---|
|
AND |
&& |
$one && $two |
if ($one is false) $one else $two |
|
OR |
|| |
$one || $two |
if ($one is true) $one else $two |
|
NOT |
! |
! $one |
if ($one is false) true else false |
The lower precedence logical operators are the 'and', 'or', 'not', and 'xor' operators.
|
function |
operator |
usage |
return value |
|---|