Impatient Perl

version: 29 April 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".



For more information, or for the latest version of this work, go to:

http://www.greglondon.com


This document was created using OpenOffice version 1.1.0

(which exports directly to PDF)

http://www.openoffice.org


running RedHat Linux

http://www.redhat.com


on a x86 machine from

http://www.penguincomputing.com





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.5 Default Script Header 8

1.6 Free Reference Material 9

1.7 Cheap Reference Material 9

1.8 Acronyms and Terms 9

2 Storage 11

2.1 Scalars 11

2.1.1 Scalar Strings 12

2.1.1.1 String Literals 12

2.1.1.2 Single quotes versus Double quotes 12

2.1.1.3 chomp 13

2.1.1.4 concatenation 13

2.1.1.5 repetition 13

2.1.1.6 length 13

2.1.1.7 substr 13

2.1.1.8 split 14

2.1.1.9 join 14

2.1.1.10 qw 14

2.1.2 Scalar Numbers 15

2.1.2.1 Numeric Literals 15

2.1.2.2 Numeric Functions 15

2.1.2.3 abs 15

2.1.2.4 int 15

2.1.2.5 trigonometry (sin,cos,tan) 16

2.1.2.6 exponentiation 16

2.1.2.7 sqrt 16

2.1.2.8 natural logarithms(exp,log) 17

2.1.2.9 random numbers (rand, srand) 17

2.1.3 Converting Between Strings and Numbers 18

2.1.3.1 Stringify 18

2.1.3.1.1 sprintf 19

2.1.3.2 Numify 19

2.1.3.2.1 oct 19

2.1.3.2.2 hex 20

2.1.4 Undefined and Uninitialized Scalars 20

2.1.5 Booleans 21

2.1.5.1 FALSE 22

2.1.5.2 TRUE 22

2.1.5.3 Comparators 23

2.1.5.4 Logical Operators 23

2.1.5.4.1 Default Values 24

2.1.5.4.2 Flow Control 25

2.1.5.4.3 Precedence 25

2.1.5.4.4 Assignment Precedence 25

2.1.5.4.5 Flow Control Precedence 25

2.1.6 References 26

2.1.7 Filehandles 27

2.1.8 Scalar Review 27

2.2 Arrays 27

2.2.1 scalar (@array) 29

2.2.2 push(@array, LIST) 29

2.2.3 pop(@array) 30

2.2.4 shift(@array) 30

2.2.5 unshift( @array, LIST) 30

2.2.6 foreach (@array) 31

2.2.7 sort(@array) 32

2.2.8 reverse(@array) 33

2.2.9 splice(@array) 33

2.2.10 Undefined and Uninitialized Arrays 33

2.3 Hashes 34

2.3.1 exists ( $hash{$key} ) 35

2.3.2 delete ( $hash{key} ) 36

2.3.3 keys( %hash ) 37

2.3.4 values( %hash ) 38

2.3.5 each( %hash ) 38

2.4 List Context 43

2.5 References 45

2.5.1 Named Referents 46

2.5.2 References to Named Referents 46

2.5.3 Dereferencing 46

2.5.4 Anonymous Referents 47

2.5.5 Complex Data Structures 49

2.5.5.1 Autovivification 50

2.5.5.2 Multidimensional Arrays 51

2.5.6 Stringification of References 51

2.5.7 The ref() function 52

3 Control Flow 53

3.1 Labels 54

3.2 last LABEL; 55

3.3 next LABEL; 55

3.4 redo LABEL; 55

4 Packages and Namespaces and Lexical Scoping 55

4.1 Package Declaration 55

4.2 Declaring Package Variables With our 56

4.3 Package Variables inside a Lexical Scope 57

4.4 Lexical Scope 58

4.5 Lexical Variables 58

4.6 Garbage Collection 60

4.6.1 Reference Count Garbage Collection 60

4.6.2 Garbage Collection and Subroutines 60

4.7 Package Variables Revisited 61

4.8 Calling local() on Package Variables 62

5 Subroutines 64

5.1 Subroutine Sigil 64

5.2 Named Subroutines 64

5.3 Anonymous Subroutines 65

5.4 Data::Dumper and subroutines 65

5.5 Passing Arguments to/from a Subroutine 66

5.6 Accessing Arguments inside Subroutines via @_ 66

5.7 Dereferencing Code References 67

5.8 Implied Arguments 67

5.9 Subroutine Return Value 68

5.10 Returning False 68

5.11 Using the caller() Function in Subroutines 69

5.12 The caller() function and $wantarray 70

5.13 Using wantarray to Create Context Sensitive Subroutines 71

6 Compiling and Interpreting 71

7 Code Reuse, Perl Modules 72

8 The use Statement 73

9 The use Statement, Formally 74

9.1 The @INC Array 74

9.2 The use lib Statement 75

9.3 The PERL5LIB and PERLLIB Environment Variables 75

9.4 The require Statement 75

9.5 MODULENAME -> import (LISTOFARGS) 76

9.6 The use Execution Timeline 77

10 bless() 77

11 Method Calls 79

11.1 Inheritance 81

11.2 use base 82

11.3 INVOCANT->isa(BASEPACKAGE) 83

11.4 INVOCANT->can(METHODNAME) 83

11.5 Interesting Invocants 84

12 Procedural Perl 85

13 Object Oriented Perl 85

13.1 Class 87

13.2 Polymorphism 87

13.3 SUPER 88

13.4 Object Destruction 90

14 Object Oriented Review 90

14.1 Modules 90

14.2 use Module 91

14.3 bless / constructors 91

14.4 Methods 91

14.5 Inheritance 92

14.6 Overriding Methods and SUPER 92

15 CPAN 92

15.1 CPAN, The Web Site 93

15.2 CPAN, The Perl Module 93

15.3 Plain Old Documentation (POD) and perldoc 95

15.4 Creating Modules for CPAN with h2xs 95

16 The Next Level 95

17 Command Line Arguments 96

17.1 @ARGV 97

17.2 Getopt::Declare 99

18 File Input and Output 102

18.1 open 102

18.2 close 102

18.3 read 102

18.4 write 103

18.5 File Tests 104

18.6 File Globbing 104

18.7 File Tree Searching 104

19 Operating System Commands 105

19.1 The system() function 105

19.2 The Backtick Operator 106

19.3 Operating System Commands in a GUI 106

20 Regular Expressions 106

20.1 Variable Interpolation 108

20.2 Wildcard Example 109

20.3 Defining a Pattern 109

20.4 Metacharacters 110

20.5 Capturing and Clustering Parenthesis 111

20.6 Character Classes 113

20.6.1 Metacharacters Within Character Classes 113

20.7 Shortcut Character Classes 113

20.8 Greedy (Maximal) Quantifiers 114

20.9 Thrifty (Minimal) Quantifiers 114

20.10 Position Assertions / Position Anchors 115

20.10.1 The \b Anchor 116

20.10.2 The \G Anchor 116

20.11 Modifiers 117

20.11.1 Global Modifiers 118

20.11.2 The m And s Modifiers 118

20.11.3 The x Modifier 120

20.12 Modifiers For m{} Operator 121

20.13 Modifiers for s{}{} Operator 121

20.14 Modifiers for tr{}{} Operator 121

20.15 The qr{} function 121

20.16 Common Patterns 121

20.17 Regexp::Common 122

21 Parsing with Parse::RecDescent 122

22 Perl, GUI, and Tk 125

23 GNU Free Documentation License 127




1 The Impatient Introduction to Perl

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.

1.1 The history of perl in 100 words or less

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.

1.2 Basic Formatting for this Document

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/bin/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/bin/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.

1.3 Do You Have Perl Installed

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

http://www.cpan.org


CPAN is an acronym for Comprehensive Perl Archive Network. The CPAN site contains the latest perl for download and installation, as was 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.

1.4 Your First Perl Script, EVER

Find out where perl is installed:

> which perl

/usr/local/bin/perl


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.

EVERY perl script should have this line:

use warnings; use strict; use Data::Dumper;


1.5 Default Script Header

All the code examples are assumed to have the following script header, unless otherwise stated:

#!/usr/local/bin/perl

use warnings;

use strict;

use Data::Dumper;


Optionally, you may try the header shown below. 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;

1.6 Free Reference Material

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

1.7 Cheap Reference Material

"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. Anyways, when I get a graphic that I like, I will slap it on the front cover and give this document a picture name as well.

1.8 Acronyms and Terms

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 Petty Ecclectic Rubbish Lister. The name was invented first. The acronyms followed.

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.

2 Storage

Perl has three basic storage types: Scalars, Arrays, Hashes.

The most basic storage type is a Scalar.

Arrays and Hashes use Scalars to build more complex data types.

2.1 Scalars

Scalars are preceded with a dollar sign sigil. A "$" is a stylized "S".

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 "" or 0. This is called autovivication.

Autovivify : to bring oneself to life.


In most common 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 zero,

# 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.




2.1.1 Scalar Strings

Scalars can store strings. You do not have to declare the length of the string, perl just handles it for you automatically.

2.1.1.1 String Literals

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'

2.1.1.2 Single quotes versus Double quotes

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 cannot be put in double quoted strings and get interpolated properly.

my $name = 'mud';

print "hello $name \n";

> hello mud


Note: a double-quoted "\n" is a new-line character.

2.1.1.3 chomp

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' ...

2.1.1.4 concatenation

String concatenation uses the period character "."

my $fullname = 'mud' . "bath";

2.1.1.5 repetition

Repeat a string with the "x" operator.

my $line = '-' x 80; # $line is eighty hypens

2.1.1.6 length

Find out how many characters are in a string with length().

my $len = length($line); # $len is 80

2.1.1.7 substr

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' ...

2.1.1.8 split

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.

2.1.1.9 join

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'...

2.1.1.10 qw

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'...






2.1.2 Scalar Numbers

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

2.1.2.1 Numeric Literals

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 base 10.

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

2.1.2.2 Numeric Functions

2.1.2.3 abs

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

2.1.2.4 int

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

2.1.2.5 trigonometry (sin,cos,tan)

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.

2.1.2.6 exponentiation

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.

2.1.2.7 sqrt

Use sqrt to take the square root of a positive number.

my $square_root_of_123 = sqrt(123); # 11.0905

2.1.2.8 natural logarithms(exp,log)

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);

2.1.2.9 random numbers (rand, srand)

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.



2.1.3 Converting Between Strings and Numbers

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.

2.1.3.1 Stringify

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'

2.1.3.1.1 sprintf

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"

2.1.3.2 Numify

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()

2.1.3.2.1 oct

The oct function can take a string that fits the octal, hexadecimal, or binary format and convert it to an integer.

binary formatted strings start with "0b"

hexadecimal formatted strings 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. Which means calling oct() on a decimal number would 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 its decimal. This example uses regular expressions and a tertiary operator, which are explained later.

my $num = ($str=~m{^0}) ? oct($str) : $str + 0;

2.1.3.2.2 hex

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.

2.1.4 Undefined and Uninitialized Scalars

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


2.1.5 Booleans

Perl does not have a boolean "type" per se. Instead, perl interprets scalar strings and numbers as "true" or "false" based on some rules:


1) 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

2.1.5.1 FALSE

The following scalars are interpreted as FALSE:

integer 0 # false

float 0.0 # false

string '0' # false

string '' # false

undef # false

2.1.5.2 TRUE

ALL other values are interpreted as TRUE, which means the following scalars are considered TRUE, even though you might not have expected 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.




2.1.5.3 Comparators

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 compare 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 numberic comparison operator '<=>' is sometimes called the "spaceship operator".

2.1.5.4 Logical Operators

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