IntroductionWelcome| 00:04 | Hi! I'm Bill Weinman, and I'd like to
welcome you to Perl 5 Essential Training.
| | 00:10 | Perl is a very flexible and powerful
programming language that's well suited for
| | 00:14 | a variety of projects, from simple
scripts to complex web applications.
| | 00:19 | In this course, I'll show you how to
use Perl and how to take advantage of its
| | 00:23 | unique and powerful features.
| | 00:26 | I'll start by giving you an overview of
Perl, so you can get started right away.
| | 00:30 | This Perl Quick Start is designed for
the experienced programmer who wants to
| | 00:34 | leverage their existing knowledge to get
up to speed with Perl quickly and with
| | 00:39 | as little detail as necessary.
| | 00:41 | Then we'll get right into the
details of the language, syntax and
| | 00:45 | structure, conditionals, loops,
operators, data structures, subroutines,
| | 00:51 | and regular expressions.
| | 00:53 | I'll show you how to do common tasks in
Perl including File I/O, databases, CGI
| | 00:58 | and web development.
| | 01:00 | And I'll show you how to use modules
to leverage your work and the work of
| | 01:04 | others to get your projects
done faster, and with less effort.
| | 01:08 | Finally, I'll walk you through some
real-world examples of working Perl code,
| | 01:12 | so you can see how the pieces fit together,
and to help you get started on your own projects.
| | 01:18 | This course is intended for users who
already have some programming knowledge.
| | 01:22 | If you have experience in at least
one other modern programming language,
| | 01:26 | this course is for you.
| | 01:28 | I've been a programmer since the 70s, and
I've been using Perl since the early 90s.
| | 01:32 | I'm glad to be able to share the
knowledge and experience I've gained along the
| | 01:36 | way, so that you can write
better applications today.
| | 01:40 | Now let's get started with
Perl 5 Essential Training.
| | Collapse this transcript |
| Using the exercise files| 00:00 | If you are a premium member of the
lynda.com Online Training Library, or if you
| | 00:06 | are watching this tutorial on a DVD-ROM,
you have access to the Exercise Files
| | 00:10 | used throughout this title.
| | 00:12 | I have copied the Exercise Files to my
desktop, and I recommend that you do the
| | 00:16 | same, or at least keep track of where you have
put them, so that you know how to get at them.
| | 00:21 | And the Exercise Files are organized by
chapter, so we can see here Chapter 01,
| | 00:26 | Chapter 02, Chapter 03 and so on.
| | 00:30 | Throughout most of this title, we'll
be using Eclipse, and we'll be accessing
| | 00:35 | the Exercise Files from inside of Eclipse.
| | 00:38 | And as we do so, we'll be making
working copies of the individual files before
| | 00:44 | we actually work on them.
| | 00:46 | In this example, if I wanted to make
a working file of this countlines.pl,
| | 00:50 | I'm holding down the Option key on
this Macintosh, or if you're on a PC
| | 00:56 | running Windows, you would hold down
the Ctrl key, or if you're on another
| | 01:00 | operating system, you'll use whatever
key it is that commonly copies files
| | 01:04 | when you drag them.
| | 01:06 | And I'm dragging it to the folder icon,
because that just seems to be the most
| | 01:10 | reliable way to do this in Eclipse.
| | 01:12 | And when I let go of it, Eclipse will
ask me for a new name, and I will say
| | 01:17 | newname.pl and press the
Return key on my keyboard.
| | 01:22 | And then I'll work in that working copy,
so that I don't overwrite the original
| | 01:27 | file, and if I want to go back and the
do the exercise again, I can do that.
| | 01:32 | If you are a monthly subscriber, or
an annual subscriber to lynda.com, you
| | 01:36 | don't have access to the Exercise Files,
but you can follow along from scratch
| | 01:41 | with your own assets.
| | 01:42 | Now let's get started with
Perl 5 Essential Training.
| | Collapse this transcript |
| Prerequisites| 00:00 | Before we begin learning about
Perl, let's talk a little bit
| | 00:03 | about prerequisites.
| | 00:05 | Before you start this course, you should
have a basic understanding of programming.
| | 00:09 | This course is about Perl and Perl is a
programming language, so you'll need to
| | 00:13 | know the basics of
programming before you get started.
| | 00:16 | This is not a programming primer.
| | 00:18 | It's not about how to write programs.
| | 00:20 | It's about how to use the
language Perl for writing programs.
| | 00:25 | You don't need to be an expert, but you
should have a little bit of experience
| | 00:29 | at least writing working code in a
modern programming language, and a modern
| | 00:34 | programming language would be something like, C,
Java, JavaScript, PHP, Python, any of
| | 00:40 | those, anything that's a procedural
language, or block-oriented language, or an
| | 00:44 | object-oriented language will be
fine for the prerequisite experience.
| | 00:49 | You will need a computer
system with Perl 5 installed.
| | 00:53 | You're not going to find anything with
Perl 4, or older versions of Perl, as a rule.
| | 00:59 | Perl 5 is already about 15 years
old as of the time of this recording.
| | 01:04 | So, any system that has Perl on
it has probably got Perl 5 on it.
| | 01:09 | Most computer systems today, with the
exception of Windows systems, come with Perl
| | 01:14 | already installed. Macs
already have Perl installed.
| | 01:18 | Virtually all UNIX systems
already have Perl installed.
| | 01:21 | And if you have a Windows system, it's
easy to install Perl, and we'll go over
| | 01:26 | how to do that in an upcoming lesson.
| | 01:29 | It's recommended that you have the
latest version of Perl, which is Perl 5.10, as
| | 01:35 | of the time of this recording.
| | 01:37 | It's not necessary.
| | 01:38 | Perl 5.6 or later will work just fine
for most of what we cover in the course.
| | 01:43 | We will have a chapter on New Features
in Perl 5.10, but they're not required
| | 01:48 | for the rest of the course.
| | 01:51 | Finally, you will need a Text
Editor; any Text Editor will do.
| | 01:55 | A good programming Text
Editor is strongly recommended.
| | 02:00 | You do not want to use Word Processor,
and you do not want to use the little
| | 02:04 | Text Editor that comes with your
operating system. You probably want to get
| | 02:08 | a good Text Editor.
| | 02:11 | In this course, we'll be using an
integrated development environment called Eclipse.
| | 02:16 | Eclipse is not required to do the
course, although if you want to install
| | 02:21 | Eclipse, there will be a lesson on how to
install and configure Eclipse for use with Perl.
| | 02:27 | Any Text Editor, whatever you normally
use for your programming work, is going to
| | 02:31 | work just great for working with Perl.
| | Collapse this transcript |
| What is Perl?| 00:00 | What is Perl?
| | 00:02 | Perl is a programming language, and
that means simply that it's a language for
| | 00:07 | defining computer programs.
| | 00:10 | Perl is an interpreted language, which
means that there is a runtime system that
| | 00:16 | requires that the source code be
present at the time that it's run.
| | 00:19 | Of course, there are other ways to run
Perl. You can use something like mod_perl,
| | 00:23 | so if you see there is a limitation,
there are ways to get around this. But in
| | 00:28 | its native form, Perl is an interpreted
language, and the source code must be
| | 00:31 | present at the time that
you run the script or program.
| | 00:37 | Perl was designed for text processing.
| | 00:40 | Perl originally came from the UNIX
world, and it was designed to replace a
| | 00:45 | number of other utilities that are
commonly used in UNIX systems for system
| | 00:49 | administration and scripting and
building small applications and as such,
| | 00:54 | it's good at processing text because
virtually all of the configuration files in a
| | 01:01 | UNIX system, the way that you do things
in a UNIX system is by manipulating text
| | 01:06 | and moving text around.
| | 01:08 | That made it well suited for Internet
work because the Internet grew out of the
| | 01:12 | same culture. And a great deal of what
goes on in the Internet is dealing with
| | 01:17 | plain text files, and Perl
is exceptionally good at that.
| | 01:20 | And for that reason, you see a lot
of Perl scripts on the Internet.
| | 01:24 | You see a lot of web applications built in Perl.
| | 01:27 | That sort of work is often done in Perl.
| | 01:32 | Perl is a general-purpose, high-level
language, and what that means is that you
| | 01:37 | can generally do a lot of things with Perl.
| | 01:40 | You can build all kinds of different
applications and as a high-level language,
| | 01:45 | that means that it's relatively
easier to use than a lower-level language,
| | 01:50 | something like C or Assembly
Language or something like that.
| | 01:54 | But Perl is a little bit easier to get
into, a little bit easier to use, a little
| | 01:58 | bit easier for a novice to read.
| | 02:01 | Perl has a very simple and very flexible syntax.
| | 02:06 | This means that there is
often more than one way to do it.
| | 02:09 | In fact, "there is more than one way
to do it," is one of the slogans in the
| | 02:13 | Perl world, and it's often pronounced TMTOWTDI.
| | 02:16 | If you look at the letters that
abbreviate those words and if you cross your
| | 02:22 | eyes just so, you can see
the words tim-toady in it.
| | 02:26 | Perl is sometimes also called the Swiss
Army Chainsaw of programming languages
| | 02:30 | because it is so very, very flexible
and so very, very powerful that you can
| | 02:35 | do a great deal of things that you
might have not even thought of doing with
| | 02:39 | something like Perl, and you can do them
easily and quickly with very little coding.
| | 02:45 | Finally, perl, with a lowercase p, is the
interpreter of the runtime environment
| | 02:50 | for the Perl language.
| | 02:52 | So, we're going to take a moment here
and take a look at the Perl program itself.
| | 02:59 | So, here I have a command line on a
Macintosh, which is basically a UNIX system
| | 03:03 | with a really slick front end.
| | 03:05 | If you are running on a PC or on some
other system, just do whatever it is that
| | 03:10 | you normally do to bring up the command line,
and these commands will work exactly the same.
| | 03:16 | Type the word perl with -v, and that's
a lower case v. Now what this does is it
| | 03:22 | runs the Perl program, and it
gives me the version number.
| | 03:26 | So, this is the Perl that comes with
Mac OS X version 10.6, and this is Perl
| | 03:34 | version 5.10.0, which is almost the
latest version as the time that we are
| | 03:39 | recording this, 5.10.1 is out now.
| | 03:42 | But if I could upgrade if I want,
but it's really not necessary.
| | 03:46 | The program perl, all lowercase letters,
is the Perl Interpreter itself, and if
| | 03:52 | I use a capital V, we get a whole lot
more information about how it's compiled
| | 03:58 | and what libraries are
included and what options are -
| | 04:01 | it's like a very, very detailed version string.
| | 04:04 | And if I change directories into my
Exercise Files here, I can actually run a script.
| | 04:14 | I have hello.pl, and I can type perl
and hello.pl, and it will run the program.
| | 04:23 | So, that, in a nutshell, is what perl
is in a lowercase p. It's the runtime
| | 04:27 | environment or the
interpreter for the Perl language.
| | Collapse this transcript |
| Installing ActiveState's ActivePerl on Windows| 00:00 | If you're using a PC, running some
form of Windows, then you probably do not
| | 00:06 | have Perl installed.
| | 00:07 | You can check really easily by
bringing up a command line, like that, and you
| | 00:15 | can type perl -v, and if you get Perl
is not recognized, or something like that,
| | 00:21 | then you do not have Perl installed,
and you'll need to install Perl.
| | 00:25 | So, we're going to go ahead and download
and install the ActiveState Perl. It is free.
| | 00:33 | And it's the recommended Perl.
| | 00:35 | There is another popular Perl called
Strawberry Perl, and it's just not quite
| | 00:40 | ready for primetime as the ActiveState one.
| | 00:43 | You're welcome to use it.
| | 00:44 | All our exercises will work. You can learn
the Perl language, but you'll be a little
| | 00:48 | bit on your own as to how to get it
installed and working and how to set up your
| | 00:53 | development cycle with it.
| | 00:54 | So, we're going to go ahead and
download Perl from the ActiveState web site, so
| | 01:00 | this is at www.activestate.com.
| | 01:03 | So, we'll just click on ActivePerl and
click on Download Now, and we'll go ahead
| | 01:09 | and Download, Save the file, click on
the maybe later for that, and we'll close
| | 01:17 | this, and there we've got the download complete.
| | 01:19 | Right-click on this and Open
Containing Folder because that's nice and
| | 01:24 | convenient, and we'll just double-click here.
| | 01:27 | And we get this nice little dialog box.
| | 01:29 | Depending on the version of Windows
that you're running, you may or may not get
| | 01:33 | exactly the same dialogs that I'm getting.
| | 01:35 | You'll need to Run this.
| | 01:37 | Let's say Next, and accept the License
Terms, and we'll just go ahead on and
| | 01:44 | install everything, and Next again, and Install.
| | 01:51 | So, this is installing
ActivePerl version 5.10.1.
| | 01:57 | The way they are version numbering,
this Build number means that this is the
| | 02:00 | seventh release of Perl 5.10. All right.
| | 02:09 | So, that's done.
| | 02:10 | I'm going to uncheck the release
notes because I don't need that.
| | 02:12 | You can leave it checked if
you want and take a look at them.
| | 02:15 | Click on Finish, and we'll close this,
and close this, and now when we run
| | 02:23 | command, type perl -v,
| | 02:27 | we get the Perl version.
| | 02:29 | We can see this is Perl version 5.10.1,
and that's exactly what we want.
| | 02:36 | One more thing I'm going to do is
I'm going to make convenient little
| | 02:38 | shortcut over here.
| | 02:40 | And what I like to do is I make a
cmd.exe shortcut and in the Properties, under
| | 02:48 | the Start in, I'll put the path to
the Exercise Files, c:\users\Bill
| | 02:55 | Weinman\Desktop\Exercise Files.
| | 03:02 | Click Apply and OK.
| | 03:05 | And now when I double-click on this, I
get right to my Exercise Files directory
| | 03:10 | there, and you can see that that's
where all the Exercise Files are.
| | 03:13 | I've got them on my Desktop.
| | 03:14 | So, you can make this go
wherever it is that you want it to go.
| | 03:17 | So, I'm going to go into my Quick
Start directory, which is right here, 01.
| | 03:24 | And if I just press Tab, it completes it for me.
| | 03:28 | That's a Windows 7 feature.
| | 03:30 | Some of the earlier versions
of Windows might not do that.
| | 03:33 | And I want to run the hello perl, just
to check it out, so I'm going to type
| | 03:38 | perl and hello.pl, and
there it says Hello, World!
| | 03:44 | Take a look at that.
| | 03:46 | That's just a very simple little
Perl program, so perl and hello.pl.
| | 03:52 | And so, we can see that Perl is working
on this machine, and we have installed
| | 03:56 | successfully ActiveState Perl
version 5.10 on a Windows machine.
| | Collapse this transcript |
| Installing Eclipse on Windows| 00:00 | So, now we're going to
install Eclipse on this PC.
| | 00:04 | This PC already has Perl installed
on it. And Eclipse is an Integrated
| | 00:09 | Development Environment,
usually just called an IDE.
| | 00:14 | Eclipse was originally developed for
Java, and it's actually written in Java,
| | 00:18 | and it's very popular and common.
| | 00:22 | We're going to be using Eclipse with the
Eclipse Perl Integration, the EPIC plug-in.
| | 00:28 | And we're going to be using it as an
Integrated Development Environment for Perl.
| | 00:32 | Now, it's important to note, at this point,
that this is not required to do this course.
| | 00:37 | You can do all the exercises in this course.
| | 00:39 | You can learn Perl from this
course without installing Eclipse.
| | 00:43 | So, if you have a preferred development
environment on the PC, or some other way
| | 00:49 | that you like to do this, by all means,
go ahead and use that. You do not need
| | 00:54 | to use Eclipse to complete this course.
| | 00:57 | We're just using Eclipse, because it's
convenient. And it provides us with a
| | 01:01 | common development environment across
all platforms, so that people that do
| | 01:05 | choose to use it, will have exactly the
same screen layout and everything, or at
| | 01:09 | least the opportunity to have
what I have here on the screen.
| | 01:13 | So, you don't need to install Eclipse.
| | 01:14 | But if you would like to, then follow along,
by all means, and we will go ahead and do that.
| | 01:19 | Now, Eclipse runs in Java, so it
requires Java, and Windows PCs do not come
| | 01:25 | with Java installed.
| | 01:27 | I'm here at the Java.com homepage.
| | 01:30 | I'm going to click on Do I have Java
to find out if this PC has Java on it.
| | 01:36 | In fact, this PC does not have Java, so
I'm going to go ahead and download Java
| | 01:41 | now, which I can do with this link here.
| | 01:43 | I'm going to click on Free Java
Download, Save File, and there we have it.
| | 01:53 | Java has downloaded.
| | 01:55 | It's not a very big file.
| | 01:56 | I'm going to right-click on this and
click on Open Containing Folder, because
| | 02:01 | that's a convenient thing to do here
with Firefox, and double-click on this
| | 02:06 | installer, click Run, and
Install, and there we go.
| | 02:16 | So, now it is installing Java. There we go!
| | 02:21 | We have successfully installed Java.
| | 02:23 | So, I'll press Close, and I will
close this window and close this window.
| | 02:30 | You'll see here in the
browser, it says, "Congratulations!
| | 02:32 | You have installed Java."
| | 02:34 | So, I will close that.
| | 02:37 | On to Eclipse. Now, I'm going
to go ahead and download Eclipse.
| | 02:42 | Eclipse is the IDE, and it's written in Java.
| | 02:44 | I'm going to click on this
Download Eclipse button here.
| | 02:49 | Now, there are a number of different choices
for installing Eclipse, as you can see here.
| | 02:56 | Feel free to look through them.
| | 02:57 | Any of them will work for our purposes.
| | 03:00 | I'm going to install the
Classic Eclipse down at the bottom.
| | 03:04 | You'll notice that there are
many choices for how to download it.
| | 03:08 | Here, if I just hover over this link
with the mouse point, you see at the bottom
| | 03:12 | in the Status bar, it
shows an Eclipse for Win32.
| | 03:16 | If I come over here, I can see that I
get it that exact same link there or a
| | 03:22 | 64-bit version over here.
| | 03:23 | I'm going to go ahead and
install the default one.
| | 03:26 | I recommend that you do too.
| | 03:27 | The default one, which will be the
main title of whichever one of these you
| | 03:31 | choose, I'm choosing the Eclipse Classic.
| | 03:34 | I'm going to click on that, and I'm going
to download it from the default mirror here.
| | 03:39 | It may take a minute. Go
ahead and say Save. All right!
| | 03:45 | Again, I'm going to right-click on
this in the Download window and Open
| | 03:49 | Containing Folder, and there we have Eclipse.
| | 03:52 | Now, you'll notice that
Eclipse comes in a ZIP file.
| | 03:55 | Depending on your Windows version,
different things will happen if you just
| | 03:58 | double-click on a ZIP file.
| | 03:59 | So, I'll just right-click on it,
and I get this pop-up menu.
| | 04:03 | I'll select Extract All.
| | 04:08 | The default location will be
right there in my Downloads folder.
| | 04:10 | So, I'm going to click Extract.
| | 04:13 | Depending on your computer,
this can take a minute.
| | 04:16 | There's actually quite a few files in here.
| | 04:18 | It tends to slow down
towards the end, and there we go.
| | 04:23 | Windows has opened this folder up for
me and your version may not do that.
| | 04:27 | If necessary, you can just double-click
on the folder, not the ZIP icon, but the
| | 04:32 | folder icon, and get to the same place.
| | 04:35 | So, I have it there.
| | 04:35 | So, I'm going to use this other window,
cleverly, to navigate to my Program Files.
| | 04:42 | This is Windows 7, so I have a choice
of Program Files or Program Files (x86).
| | 04:47 | I'm going to select the Program Files
(x86), and I'm just going to go ahead and
| | 04:53 | drag this Eclipse folder from inside
the Eclipse-SDK, where it unzipped.
| | 04:59 | I'm going to drag that Eclipse folder
right here into Program Files (x86).
| | 05:04 | We'll just go ahead and move it.
| | 05:06 | It doesn't need to copy, and we don't
need to have a copy of that anymore, and
| | 05:10 | close this other window, and open this up,
and fire up Eclipse. Double-click on
| | 05:17 | that, and I'll get this, "Are you sure?"
| | 05:20 | Of course, I'm sure.
| | 05:22 | Now, it wants me to select a workspace,
so you can just use the default or what
| | 05:28 | I do is I have a place where I put such
things. So, I go into Files, and I click
| | 05:34 | Make New Folder, and I call
it Eclipse, and select OK.
| | 05:40 | Now I have Files\Eclipse.
| | 05:42 | That's where I like to put things.
| | 05:44 | It's really up to you.
| | 05:45 | As long as you know where it
is and you know how to find it.
| | 05:48 | So, I'll click OK, and Eclipse fires up.
| | 05:52 | Now at this point, it's a
good idea to check for updates.
| | 05:57 | Now since we just downloaded from
their web site, we can presume, probably,
| | 06:01 | safely, that there aren't any updates available.
| | 06:04 | I've had it happen where installing
Eclipse, I've downloaded from the web site
| | 06:08 | and I've checked for updates and
there have been updates available.
| | 06:10 | It's a good idea to just go ahead and
check, and this can take a few minutes.
| | 06:16 | Okay, so there we are.
There is nothing to update this time.
| | 06:21 | Now we're going to install EPIC.
| | 06:23 | Now if we go back over here to the
browser, click on the EPIC tab, and this
| | 06:27 | is an epic-ide.org.
| | 06:31 | Go ahead and click on Download.
| | 06:33 | You'll see this whole description.
| | 06:35 | It actually makes a lot of sense to
install EPIC from inside of Eclipse.
| | 06:41 | In this case, I've got two different
versions that I can do the testing version.
| | 06:44 | In this case, most of the time, I
stay away from testing versions.
| | 06:48 | In this case, it's been so long, and it
is considered so stable in the community
| | 06:53 | that I'm going to go ahead and use the
testing version, and it does say, recommended.
| | 06:58 | So, I'm going to highlight this and
just copy that URL, because I know that
| | 07:03 | we're going to need it.
| | 07:04 | Then come back over here to Eclipse,
and I'm going to click on Help >
| | 07:10 | Install New Software.
| | 07:13 | Then right in here, I'm going
to paste that URL and select Add.
| | 07:18 | I'm going to name it EPIC, E-P-I-C, and click OK.
| | 07:26 | You'll notice at this point, oh,
| | 07:27 | it came up quickly that time.
| | 07:29 | Sometimes, it may stay on
that pending for a long time.
| | 07:33 | Just be patient. You will
eventually get to this screen.
| | 07:37 | I'm going to click the check box and say Next.
| | 07:42 | It's found EPIC Version 0.6.35.
Click Next again. Accept the terms of the
| | 07:49 | license, and Finish.
| | 07:52 | Now we'll install EPIC.
| | 07:54 | Again, this can take a while.
| | 07:57 | You'll notice a few things happening.
You'll notice, sometimes up here, we'll
| | 08:01 | get this dialog box, and I'll just say OK,
because I know that unsigned content is okay.
| | 08:07 | Sometimes, you'll see the word Blocked
come up in that other dialog box, and
| | 08:11 | sometimes it will stay there for a
while, and you just want to go ahead and
| | 08:15 | let it do its thing. It is working.
| | 08:17 | Some of the messages look
like it might not be working.
| | 08:20 | Just let it go through until you get
to this point, where it is strongly
| | 08:23 | recommended, and this is
not just plain recommended.
| | 08:26 | This is strongly
recommended that you restart Eclipse.
| | 08:29 | So, I don't even know why
they let me not restart it.
| | 08:31 | I'm just going to go ahead and restart it.
| | 08:33 | And that will restart Eclipse, and
we'll have the Perl integration in there.
| | 08:40 | It remembered my workspace.
| | 08:42 | So, you can use this as
a default if you want to.
| | 08:45 | I'm going to leave that unchecked for now,
and I'm going to say OK. And here we go!
| | 08:51 | We're now in Eclipse.
| | 08:52 | So, a lot of different choices here.
You can learn about Eclipse if you like.
| | 08:56 | I'm going to go right to the workbench,
and I'm going to go right over here and
| | 09:01 | change my Perspective to Perl Perspective.
| | 09:04 | So, I click on this little doodad
there and click on Other and click on Perl.
| | 09:11 | So, now I know that I have
Perl installed in my Eclipse.
| | 09:17 | I'm going to maximize the
window. Create a new Perl project.
| | 09:22 | I'm going to name it Perl 5
Essential Training. Click Finish.
| | 09:31 | And now within that, I'm going to
right-click and say New > Folder.
| | 09:38 | I'm going to click on Advanced, Link to a
folder in the file system, and browse for it.
| | 09:46 | On my Desktop, I have my Exercise Files.
| | 09:49 | Now this is where I put the Exercise
Files. You can have them wherever you like.
| | 09:53 | But that's what I'm selecting is the
Exercise Files from this course, and I'm
| | 09:57 | going to say OK, Finish.
| | 10:00 | There we have the Exercise Files.
| | 10:02 | So, I'll click on this little arrow
next to it and it opens up, click on the
| | 10:06 | little arrow next to chapter one, and I
just want to run the hello.pl, and make
| | 10:12 | sure I'm double-clicking on it. There it is.
| | 10:13 | There is our Hello World in Perl.
| | 10:15 | Then in the Run menu, I'm going to
click Run, and there we have, in our Console
| | 10:22 | at the bottom, Hello, World!
| | 10:24 | So, we can see, at this point, that
Eclipse is installed, Perl is installed, the
| | 10:30 | Perl extensions to Eclipse are installed, and
we have everything we need for it to be working.
| | 10:36 | So, I know we've been sitting here for
awhile, but I'm going to take a moment
| | 10:40 | here, and we're going to do a
little bit of configuration.
| | 10:44 | In the Window menu, under Preferences,
there's a couple of things that we want to do here.
| | 10:50 | I want to go under Perl EPIC, and Editor.
| | 10:54 | Now, the way that this comes, by
default, it's going to insert tabs.
| | 11:00 | If you like tabs, go ahead and leave the
tabs alone. My files don't have tabs in them.
| | 11:05 | I'm not a fan of tabs, because
they don't work across platforms and
| | 11:09 | across environments.
| | 11:10 | So, I'm going to click on Use spaces
instead of tabs, and 4 spaces per indent.
| | 11:15 | This will make everything work just the way
that we expect it, and I'm going to say OK.
| | 11:19 | There are a lot of other things that
you can do to configure here, and I'll
| | 11:24 | leave that up to you.
| | 11:25 | But there you have it. The Eclipse IDE
is installed. Perl is working, and it's
| | 11:30 | all tested and working on this Windows PC.
| | Collapse this transcript |
| Installing Eclipse on Mac| 00:00 | So, now we're going to
install Eclipse on a Macintosh.
| | 00:05 | Now it is important to note that
you do not need Eclipse in order to
| | 00:09 | complete this course.
| | 00:10 | Eclipse is not required.
| | 00:12 | Eclipse is an integrated
development environment, or an IDE.
| | 00:15 | As such, it's a useful thing, but a lot
of developers work in different ways and
| | 00:21 | so if your preferred method of coding
is to use a stand-alone text editor or
| | 00:28 | different IDE, VI or
whatever, then do it that way.
| | 00:33 | Eclipse is not required for this course.
| | 00:36 | This is a course about Perl and as
such, whatever editor or development
| | 00:41 | environment you choose to use will work,
and you will be able to follow along
| | 00:45 | with the exercises just fine.
| | 00:47 | All of that said, let's go ahead and
install Eclipse on this Macintosh right now.
| | 00:53 | So, we're here at the Eclipse web site,
which is www.Eclipse.org, and I'm going
| | 00:58 | to click on the big yellow button that
says Download Eclipse. And that will
| | 01:02 | get me this page here where there are a
number of choices for different Eclipse downloads.
| | 01:08 | Feel free to look through these and if you
decide that one of them works best for you,
| | 01:12 | go ahead and install that one. Any of
them will work of them work for our purposes.
| | 01:16 | We're going to install a Perl plug-in,
which is not on this page, so I'm going
| | 01:20 | to start with the basic Eclipse Classic,
which is this last choice down here.
| | 01:25 | Then I'm going to install the
EPIC Perl plug-in on top of that.
| | 01:28 | So, I'm going to go ahead and click on
that - get this download started here.
| | 01:35 | Eclipse is written in Java. It requires Java.
| | 01:39 | It runs under the Java environment,
and the Macintosh, of course, comes with
| | 01:44 | Java already installed, and if you have
been running your updates, then your Java
| | 01:48 | should be up-to-date.
| | 01:49 | You might want to make sure before you
install Eclipse that you have an updated
| | 01:54 | Java environment on your Mac. Here we go.
| | 01:57 | The download is complete.
| | 01:59 | So, I'm going to right-click on this.
| | 02:01 | If you have a one-button
mouse, you can Ctrl+Click on it.
| | 02:05 | Select Show in Finder, and
there it is in my Downloads folder.
| | 02:10 | I'm going to double-click on this now.
Eclipse does not have an installer per se.
| | 02:14 | So when I double-click on this, it's going to -
| | 02:16 | first, it will uncompress, then it will
unpack the .tar file, and it gives me this folder.
| | 02:22 | And in the folder, all of the parts of
the Eclipse package, and I'm just going
| | 02:26 | to take that entire folder and drag it
into applications and let go. So, that
| | 02:30 | moves the folder out of the Downloads
folder and into the Applications folder.
| | 02:35 | And I'll select that and scroll
down and find Eclipse in here.
| | 02:39 | Here is the Eclipse application.
| | 02:41 | I'm going to drag that into my dock because
I'll be using that, and then I will close this folder.
| | 02:46 | I can close the downloads and run Eclipse.
| | 02:51 | And now it's going to ask me
where I want to put my workspace.
| | 02:54 | Now you can use the default location if you like.
| | 02:57 | I like to put it some place else.
| | 03:00 | I keep a Files folder for things like this.
| | 03:03 | I'll just go ahead and create a new
folder inside of there and call it
| | 03:08 | Eclipse, and that will be
my location, and I'll say OK.
| | 03:19 | Now Eclipse starts up and you get this
lovely screen here with all kinds of choices.
| | 03:24 | Now before we select any of these
choices, I know, from personal experience, that
| | 03:29 | sometimes I can download something
from somewhere, and this has happened with
| | 03:32 | Eclipse before, and I
didn't get the latest version.
| | 03:35 | So, I'm going to go in the Help
menu and select Check for Updates.
| | 03:40 | But what it does is make sure that I'm
running the latest version of Eclipse.
| | 03:47 | Okay, so this means that we do have the
latest version and if not, it would give
| | 03:51 | us an opportunity to install that.
| | 03:54 | Now we are going to go ahead and install the
Perl plug-in for Eclipse, which is called EPIC.
| | 03:58 | I go back to my browser here.
| | 04:01 | When I click on the EPIC web site tab
that I have already opened and what this
| | 04:05 | is is an EPIC plug-in for the Eclipse
IDE. and I click on Download, and you want
| | 04:12 | to install EPIC from inside of Eclipse.
| | 04:15 | And this is the URL to use.
| | 04:18 | Now the reason that this is recommended
is because this is actually the stable
| | 04:22 | version. The older version is still available.
| | 04:25 | The newer version, it considers it a
testing release, but it's been so long
| | 04:29 | since there has been any updates, and
the word that I've been reading is that
| | 04:34 | this is completely stable and ready for
prime-time, or as ready as anything is in
| | 04:39 | terms of a Perl IDE, and I've been using this.
| | 04:43 | This is the version that I'm
going to recommend that you install.
| | 04:46 | Select this URL and right-click and
select Copy. Switching back to Eclipse, I'm going
| | 04:54 | to go to the Help menu and Install New
Software. And then right up where it says
| | 05:00 | Type or Select a site, I'm going to
paste in that URL and click Add. And I'll go
| | 05:06 | ahead and name it EPIC and you see the
word Pending comes up here. And that can
| | 05:12 | happen for a long time or very
quick, like it did in this case.
| | 05:15 | Select the EPIC main components and select Next.
| | 05:20 | I'll select Next again. You see that
it's found the EPIC Version 0.6.35 and
| | 05:27 | press Next. Accept the Terms of the
License Agreement. Select Finish, and now
| | 05:33 | we'll start installing.
| | 05:35 | Now as it installs, you'll see
different messages come up here.
| | 05:40 | Some of them say something
about that something is blocked.
| | 05:43 | Don't worry about it.
| | 05:44 | It's running several processes at once,
and it's just waiting for one to finish
| | 05:49 | before the next one starts up.
| | 05:51 | This can take a while. It can go quickly.
| | 05:53 | It depends on how busy the web site is.
Go ahead and say OK when you get this
| | 05:59 | warning about the Unsigned Content.
| | 06:04 | It is now strongly recommended, and
this is not just plain recommended. This is
| | 06:07 | strongly recommended that you go ahead
and restart Eclipse, and so I'm going to
| | 06:12 | say yes and restart Eclipse,
because there's just no reason not to.
| | 06:17 | And it gives me my default location again,
and there we have it. Eclipse is running.
| | 06:25 | So, now you can browse through
these different choices if you like.
| | 06:29 | I'm going to go directly to the workbench,
and I'm going to maximize the window,
| | 06:35 | and I'm going to switch to a Perl view.
| | 06:37 | So, over here, you have your open
perspectives in a JAVA perspective, and I'm
| | 06:43 | going to switch to a Perl perspective.
| | 06:44 | So, I click on that open
perspectives and I select other and Perl, OK.
| | 06:51 | And now I'm going to open a new Perl Project.
| | 06:55 | I will call it the Perl 5 Essential Training,
say Finish and so now I have a Perl project.
| | 07:05 | And I'm going inside of this.
| | 07:07 | I'm going to right-click on that,
and I'm going to say New > Folder.
| | 07:14 | Click on Advanced and Link to a folder
in the system and Browse and go right to
| | 07:20 | my desktop and my Exercise
Files and select that and Finish.
| | 07:26 | And so now I have the Exercise Files
loaded in here, and they're not copied.
| | 07:30 | This is linked to the file
system ,so that's important.
| | 07:34 | So, just to make sure everything's
working, I'm going to go in here and select my
| | 07:37 | Hello, Perl, Run from the menu,
and there is our output Hello, World!
| | 07:43 | So, we know that this is all working.
| | 07:45 | One more thing really quickly before we're done:
| | 07:48 | I have a thing about tabs and so I'm
going to in the Eclipse menu where the
| | 07:55 | Preferences and under Perl EPIC, open
that up, and I'm going to click on Editor.
| | 08:01 | I'm going to say Use spaces instead of tabs.
| | 08:03 | You don't have to do this.
| | 08:04 | This is my way, but the files that
are in here are done this way and so
| | 08:09 | this'll make them match.
| | 08:11 | And 4 spaces per indent, say OK, and
now we have installed the Eclipse IDE with
| | 08:19 | the Perl plug-in, and we're ready
to go ahead and write some Perl.
| | Collapse this transcript |
|
|
1. Perl Quick-Start Guide for Experienced ProgrammersHello World| 00:00 | So, let's start by taking a
look at Hello World in Perl.
| | 00:05 | Hello World is a tradition in
programming, and its purpose is to get all of the
| | 00:11 | coding out of the way, so we
can look at the environment.
| | 00:14 | It's really about, how do I do my
development cycle? How do I write code,
| | 00:20 | edit code, test code, debug code, go around in
that cycle? And it's not about the code itself.
| | 00:28 | This is not about, how do I write
the world's simplest program in Perl?
| | 00:32 | This is about, how do I do these
other things around of the coding using a
| | 00:37 | piece of code that's simple enough,
that it can get out of the way, so I can
| | 00:40 | look at my environment?
| | 00:42 | In this case, I am using
the Eclipse environment.
| | 00:45 | You may be using a different environment.
| | 00:47 | So, our purpose here is to take a
moment and get familiar with whatever our
| | 00:52 | development cycle is going to be.
| | 00:54 | In this case, here in Eclipse, I am
going to go ahead and open my Exercise
| | 00:58 | Files, and you can do that yourself.
| | 01:01 | Again, if you're not using Eclipse,
just use whatever tools you are used to.
| | 01:05 | Open a folder and open a file, so in
the 01 Quick Start folder, inside Exercise
| | 01:10 | Files, you'll find hello.pl.
| | 01:13 | And I'll just double-click on that here
and bring that up, and there is the code.
| | 01:17 | So, I can see it in the Editor. I can edit it.
| | 01:21 | I can do whatever I need to do there, but the
first thing I want to do is I want to run it.
| | 01:25 | For my part, I am just going to select
Run from the Run menu, and there it is.
| | 01:30 | It's running this Console down
here, and it says, Hello, World!
| | 01:34 | For your part, if you are doing it a
different way, you may be running it on a
| | 01:38 | command line, and that would look different.
| | 01:40 | For example, here I have a command line,
and I'm going to change directories
| | 01:47 | into my Exercise Files, which is
under Desktop and Exercise Files and 01
| | 01:53 | Quick Start. And I am going to look in
this directory, and I see there is all
| | 02:00 | my different scripts.
| | 02:01 | If I want to run Hello World, I just I
type perl hello.pl, and that's how you
| | 02:08 | would run a Perl script.
| | 02:09 | There are other ways to do it.
| | 02:11 | Again, whatever environment you are using,
you need to do a little experimenting
| | 02:16 | and find the way to run a
Perl script in your environment.
| | 02:20 | I just want to take a moment and
talk about the two different Hello World
| | 02:26 | scripts that I have here.
| | 02:28 | One of them is called hello-simple, and
you'll notice that that one doesn't have
| | 02:33 | any of this extra information.
| | 02:35 | It doesn't have the comment at the top.
| | 02:36 | It doesn't have the use strict and use warnings.
| | 02:39 | I consider all of these things important,
and we will get to them in different
| | 02:43 | parts of this course.
| | 02:45 | This is technically the simplest
script that you can write in Perl.
| | 02:49 | And we can run it here using
Run and there the hello-simple.
| | 02:53 | It says Hello, World!
| | 02:56 | down there in our Console.
| | 02:58 | And again, in the command line
environment, we can do the same things, say perl
| | 03:02 | hello-simple.pl, and it will run that as well.
| | 03:09 | So, that's our Hello World script, and
we have the two versions of it there.
| | 03:14 | And that should give you an opportunity
to see how to do the edit and running a
| | 03:19 | development cycle in your environment.
| | Collapse this transcript |
| Counting lines in a file| 00:00 | Continuing in our Quick Start, if you
already have some programming experience,
| | 00:05 | you're just looking for quick answers
to how to do simple things in Perl, you
| | 00:09 | are in the right place.
| | 00:11 | In this lesson, we are going to
look at how to read a file in Perl.
| | 00:15 | So, in your 01 Quick Start folder, in your
Exercise Files, you will see countlines.pl.
| | 00:22 | Open that up.
| | 00:23 | I am going to maximize this
window, so we can look at the code.
| | 00:26 | This is a simple script that simply
reads the file, counts the lines in the file.
| | 00:31 | And so you can see, it starts by
declaring a variable and the name of the
| | 00:38 | file, linesfile.txt,
| | 00:40 | opens the file, and it reads all of
the lines in the file into a variable,
| | 00:46 | using this syntax here.
| | 00:48 | And then it closes the file, and it
counts the number of lines in the file
| | 00:52 | and prints that result.
| | 00:54 | All of these details will be
covered in other parts of this course.
| | 00:58 | If you are looking for Quick Start
on just how to read lines in a file and
| | 01:03 | count them, this is the place.
| | 01:05 | I can go ahead and run this, and there it is.
| | 01:09 | There are 50 lines in linesfile.txt.
| | 01:12 | And if we look at linesfile.txt right
here, we can see it's got 50 lines of
| | 01:19 | that, and there they are.
| | 01:27 | So, just quickly, this is
the simple way to open a file.
| | 01:31 | We are going to do a whole chapter
on File I/O later on, where we will be
| | 01:35 | using a module for reading files,
which is more flexible and a more
| | 01:39 | object-oriented way to do it.
| | 01:41 | But you simply use the open function,
and the first argument is a file handle,
| | 01:47 | which looks like that in Perl.
| | 01:49 | And the second argument is the file name,
and you can see that the file name is
| | 01:55 | a variable that's declared up
here, and it's linesfile.txt.
| | 01:59 | And then, using that file handle with
the angle brackets, we will read the
| | 02:03 | entire file all at once
and assign it to an array.
| | 02:08 | So, this is an array, and it will have
one line per element in the array, and
| | 02:14 | then you can close file because it's
already red, and it's in the lines array.
| | 02:19 | So, the lines array here, you can
count the elements in array by simply
| | 02:24 | casting it to a scalar.
| | 02:26 | And again, we'll look at that in the
chapter on arrays and how to do that,
| | 02:32 | and assign that to a variable,
and then you can print out.
| | 02:35 | There are count lines in file
name and you get this result.
| | 02:40 | So, that's the simple answer to "How
do I read a file and count its lines
| | 02:45 | in Perl?"
| | Collapse this transcript |
| Using a loop in Perl| 00:00 | Continuing in our Perl QuickStart, if
you're looking for a quick answer to, "How
| | 00:06 | do I use a loop in Perl?"
you are in the right place.
| | 00:10 | In this case, we are going to take our
countlines example from the reading a
| | 00:14 | file movie, and we are going to do the
same exercise but using a loop instead.
| | 00:20 | In the countlines script, if you remember,
I'll go ahead, and I'll open that here,
| | 00:24 | we read the entire file
into an array all at once.
| | 00:28 | The problem with this approach is that
if your file is very, very large, you run
| | 00:33 | the risk of crashing your computer,
having a stack overflow, having problems
| | 00:38 | with that, because you've
ran it all, all at once.
| | 00:40 | In this movie, what we are going to do
is we are going to read that same file.
| | 00:45 | We are going to read it line
by line, using a loop instead.
| | 00:49 | So, here we have the same file name,
linesfile.txt, which is just a simple
| | 00:56 | little flat file with 50 lines of text in it.
| | 00:59 | In this case, we are going to open the
file and read the file line by line, and
| | 01:05 | we are going to use the IO::File module.
| | 01:09 | In the countlines example, we just use
Perl's built-in native, open, close and
| | 01:15 | reading the file handle technique.
| | 01:17 | That's fine for some very simple
applications, but most of the time, you are
| | 01:22 | going to want to use a richer interface.
| | 01:25 | In this case, we are using the IO::File
interface, which is very, very common in Perl.
| | 01:30 | This is the way that file IO is commonly done.
| | 01:34 | It's an object-oriented interface.
| | 01:36 | So, the first thing we do is we
create a new file handle object, and that's
| | 01:41 | using this syntax here.
| | 01:43 | So, IO::File and this dash and right-
angle bracket to create an arrow and new
| | 01:51 | So, new is a method inside the IO::File object
-oriented module, and new has two arguments.
| | 01:57 | It's got the file name and an R, which
indicates that we are opening the file
| | 02:02 | for read-only and not for writing.
| | 02:05 | If we didn't get a file handle, if
it didn't work, we can print an error
| | 02:09 | message, which will say, "Cannot open file name,"
and we'll take a look at that in a moment.
| | 02:14 | Then once we have the file
opened, we count the lines.
| | 02:19 | So, we start with a count = 0 and we
use a while loop, and we'll talk later
| | 02:25 | on about the different kinds of loops
available in Perl, and we'll get into
| | 02:28 | the details on that.
| | 02:30 | So, we get a line, one at a time, and
we count them one by one using the
| | 02:35 | increment operator for our number,
which started at 0. And so for each line, it
| | 02:40 | will get incremented by 1. We are all done.
| | 02:43 | We close the file handle
and we print out the results.
| | 02:48 | So, if I go ahead and run,
you'll see there's our results.
| | 02:52 | There are 50 lines in the text file.
| | 02:54 | If for some reason it cannot open the
file - and let's just go ahead and change
| | 02:58 | the name of the file here.
| | 03:00 | So, I am going to put an X in front of that.
| | 03:01 | The file name will be wrong, and
it won't be able to open the file.
| | 03:05 | So, I'll save and run.
| | 03:10 | Now, we get this error message, Cannot open
xlinefiles.txt (No such file or directory).
| | 03:18 | So, what this is here's the error
message, Cannot open, and there is the file
| | 03:22 | name from the variable
here $filename, $filename.
| | 03:26 | The error message is in this special
variable, and we'll have a whole chapter on
| | 03:31 | the special variables.
| | 03:32 | This is the one that gives you
errors from the operating system $!
| | 03:37 | or as we say, dollar bang.
| | 03:40 | That gives you the error
message from the operating systems.
| | 03:42 | So, Cannot open $filename error message.
| | 03:46 | In our results, we saw that it says Cannot
open linesfile.txt (No such file or directory).
| | 03:55 | So, we'll change this back, and we'll save,
and we will run again, and there we have it.
| | 04:03 | There are 50 lines in lines file.
| | 04:06 | So, that's how you use a loop. Move this back.
| | 04:10 | So, that's a very simple
loop, using a while loop.
| | 04:13 | We have got a Conditional here, also,
| | 04:15 | a little bonus. You can see how to do that.
| | 04:17 | You see how to get an error message out.
| | 04:20 | This is a much more robust
way to do the count lines.
| | 04:23 | It's not going to crash
| | 04:24 | if you have got a lot of lines in
your file; it's reading them one by one.
| | 04:29 | This is a much more robust way to
read a text file in Perl using a loop.
| | Collapse this transcript |
| Using subroutines in Perl| 00:00 | Continuing in our QuickStart, we are
going to look at subroutines now. If you
| | 00:04 | are looking for how to do subroutines in Perl,
| | 00:07 | for simplicity, for modularity
and readability, this is how you do
| | 00:12 | subroutines in Perl.
| | 00:13 | Our third countlines example, again,
we are counting the lines in a file.
| | 00:17 | And this time, it's a more
complete program, and we've broken up the
| | 00:21 | functionality into several different subroutines.
| | 00:24 | Personally, I like to use a main
subroutine for a main entry point, and
| | 00:30 | oftentimes, I'll just do it like this.
| | 00:32 | At the top of the File, I've got my
use strict, warnings, IO:File, and then I
| | 00:39 | just call the main function and using
this syntax here, that actually calls the
| | 00:44 | main function with the command
line arguments as function arguments.
| | 00:49 | So, if you are familiar with C, this
does something similar to how that works or
| | 00:53 | Java or any command line-oriented language.
| | 00:56 | This makes it work, kind
of like you would expect.
| | 00:59 | So, here we have our main subroutine,
and the first argument is the $filename.
| | 01:06 | So, if you were to call the command
line and say the countlines3 and the file
| | 01:11 | name, then it would bring that in.
| | 01:13 | And if you didn't call it, you can
use this logical OR, and it will assign,
| | 01:18 | instead, linesfile.txt.
| | 01:22 | Then the $count we get
from our countlines function.
| | 01:25 | So, that's all encapsulated.
| | 01:27 | It's all hidden in that countlines
function and makes it very simple from the
| | 01:31 | main subroutine point of view.
| | 01:33 | Then you simply report the results.
| | 01:36 | So, this little three line main, it tells
you, at a glance, exactly what's going on.
| | 01:41 | Then you can look at the countlines function.
| | 01:44 | I have a little comment at the top
that says, "Here's how you call it and
| | 01:47 | here's what it returns."
| | 01:48 | I think that's a great habit to
get into as you write subroutines.
| | 01:53 | Then here is the body of it.
| | 01:55 | This is where all the work is done.
| | 01:57 | We read in the file name.
| | 01:59 | We give an error if we don't
have a file name. We open the file.
| | 02:03 | We give an error if we cannot open the file.
| | 02:05 | Here is a nice way to do that.
| | 02:07 | This is simpler than how we did it in
the last example using the logical OR.
| | 02:12 | This is a very low precedence OR that Perl
has that makes it very easy to do things this
| | 02:18 | You go and you create the instance of
the object, in this case, open the file.
| | 02:22 | And if that didn't work, you
just give an error message.
| | 02:25 | It's a very nice simple syntax for that.
| | 02:28 | Now, we count the lines with a while loop.
| | 02:32 | Again, this is exactly the same as we
did in the last example, except we are
| | 02:36 | using the postfix while, which is
something that is fairly unique to Perl.
| | 02:42 | There aren't a lot of
languages that have a syntax like this.
| | 02:45 | So, it allows you to do this entire
loop on one line and you just say count
| | 02:50 | while you are getting lines.
| | 02:51 | It's very easy to read and
easy to look at and easy to code.
| | 02:55 | Then we return the results.
| | 02:57 | We have a couple of other little
subroutines here, which are just useful
| | 03:00 | things that I like to do.
| | 03:02 | I have one called message, which
just prints a string with a new line.
| | 03:07 | In Perl 5.10, there is a new keyword
called Say, which does the same thing.
| | 03:13 | But unfortunately, Perl 5.10 is
not widely enough deployed to be able to
| | 03:18 | really use those new features in
production and expect them to work on
| | 03:21 | anybody else's computer.
| | 03:23 | So, this way that I have been
doing this for years using my little
| | 03:26 | message subroutine and
| | 03:28 | a similar one for error where I print
the $0 is the file name of the Perl script
| | 03:35 | and then $e and for the error message,
which we get from the shift here.
| | 03:41 | Simply print an error (string) and then
exit and so when have an error it does -
| | 03:45 | it prints the error message and exits.
| | 03:47 | So, that makes all of
these other things very easy.
| | 03:50 | So, we can use our error function here.
| | 03:52 | We can use the error function here,
and we can use the message function here,
| | 03:59 | just to print out our status message
that there are so many lines in the file.
| | 04:04 | So, it makes the whole program very
readable, and it puts the functionality of
| | 04:10 | the different things that we're
doing in their own nice, little
| | 04:12 | self-contained, little containers, like
this one here for counting the lines.
| | 04:17 | So, yes, the script is
longer than our other versions.
| | 04:20 | In my opinion, it's far superior.
| | 04:22 | It's got better error
reporting and better modularity.
| | 04:26 | It's going to be a whole lot easier for
you to work with something like this using
| | 04:30 | this kind of a modular
structured programming style.
| | 04:34 | So, let's go ahead and run it,
and we'll run it right here.
| | 04:37 | We see there are 50 lines in the text file.
| | 04:41 | If we go and change the file
name, like we've tried before -
| | 04:45 | I'll save that and run again -
| | 04:47 | we see that we get an error message.
| | 04:49 | In this case, we're using the error subroutine.
| | 04:53 | So, let's go ahead and go back to the other
view where we can see all of this at once.
| | 04:58 | In the error subroutine, we see that
we have this $0 here, and that's what
| | 05:04 | prints out all of this, and that's our
entire path name to the program we are running.
| | 05:08 | So, we can see this error is
coming from countlines3.pl.
| | 05:12 | The error says Cannot open the file
because there is (no such file or directory).
| | 05:16 | So, we'll ahead, and we'll put our file
name back and save and run again and we
| | 05:24 | get the 50 lines in the text file.
| | 05:29 | So, that is the more modular
structured, more modern programming technique
| | 05:33 | version using subroutines.
| | 05:36 | Now, you have the basics of Perl.
| | 05:39 | You have the basics.
| | 05:41 | If you're an experienced programmer,
and you're just looking for the
| | 05:44 | basics, you've got that.
| | 05:46 | In the rest of the course, we'll be
going through the details of how all
| | 05:50 | these different things work and all
the different features of the Perl
| | 05:53 | programming language.
| | Collapse this transcript |
| Using Perl's documentation| 00:00 | I want to take a quick moment, and
I want to talk about Perl's online
| | 00:03 | built-in documentation.
| | 00:06 | On any system that has Perl properly
installed, you can type Perldoc at a
| | 00:11 | command line and get complete
documentation on any subject within Perl.
| | 00:16 | So, for example, if I want to know
about the IO file modul,e I just type perldoc
| | 00:20 | IO::File, like that, and I
have the IO file documentation.
| | 00:27 | The format of the documentation is very
similar to man pages if you're familiar
| | 00:32 | with UNIX man pages.
| | 00:34 | This is pretty much what they look
like, and they are built using Perl's
| | 00:38 | internal pod documentation, P-O-D, which
stands for Plain Old Documentation, and
| | 00:44 | we'll be talking about that later in this course.
| | 00:47 | So, basically, if you want to know
anything about anything in Perl, you just
| | 00:52 | type that perldoc with that subject
and you get all kinds of - you get
| | 00:56 | whatever documentation is available,
and then in the case of the modules, it's
| | 01:00 | the documentation that was written
by the module authors, and it's in this
| | 01:03 | perldoc man-like format.
| | 01:06 | If you want to get documentation on a
built-in function, you say Perl doc -f and
| | 01:12 | something like print, which is a built-
in function, and you get everything that
| | 01:16 | you ever wanted to know about print.
And I am using the Page Up and Page Down
| | 01:20 | keys. That may be different on your system.
| | 01:23 | If you're using the Eclipse IDE, you
can have access to all the perldocs just by
| | 01:30 | highlighting something, and in this case,
I'm using the right mouse button and I
| | 01:36 | see Perldoc right here and I
click on that, and then it comes up.
| | 01:41 | I can maximize this window.
| | 01:42 | Again, you've probably got a bigger
screen than I do, and you can arrange these
| | 01:46 | windows differently, and there is
all the documentation on that module.
| | 01:50 | In fact, you can also get the source
code for the module, and in this case, you
| | 01:54 | can see the pod documentation there at
the top, and then there's the actual code
| | 01:58 | down further down. And likewise,
| | 02:01 | if you want to get documentation on a
built-in function, you just highlight
| | 02:06 | that built-in function.
| | 02:07 | Let's see, we've got print down here some
place, and it's Perldoc, and there
| | 02:13 | you get the documentation
on that built-in function.
| | 02:16 | So, this is Perl's built-in
documentation, and any properly installed Perl will
| | 02:21 | have this available.
| | Collapse this transcript |
|
|
2. General SyntaxUnderstanding how Perl uses whitespace| 00:00 | As we begin our discussion of
general syntax of the Perl language, we are
| | 00:05 | going to start by talking about
whitespace. And in your 02 Syntax Folder, in
| | 00:12 | your Exercise Files,
| | 00:13 | I just want to take this file here,
this syntax.pl, and make a copy of it.
| | 00:18 | And so I'm going to right-click on it
and select Copy, and right-click on the
| | 00:24 | Folder and push Paste.
| | 00:26 | I want to call it whitespace.pl.
| | 00:29 | And so now I'm working with a copy of this.
| | 00:33 | I'll close this one, and I'll open
whitespace.pl, and in that way, our
| | 00:39 | Syntax File remains untouched while
we play around with the copy, which is
| | 00:44 | always a good idea.
| | 00:46 | In order to discuss whitespace, we
want to turn on the Features in the Editor
| | 00:49 | that allows us to see
whitespace and manipulate whitespace.
| | 00:53 | And so I'm going to go into my
Preferences here in Eclipse, and you'll do this
| | 00:57 | with whatever Editor that you are using,
assuming that it has features like this,
| | 01:01 | and most of them do.
| | 01:02 | And I'm going to take out
the Insert tabs for spaces.
| | 01:05 | I'm going to turn on the
Show whitespace characters.
| | 01:08 | This is under General >
Appearance > Editors > Text Editors.
| | 01:12 | I am going to press Apply,
and then I know, in Eclipse, I need to do
| | 01:16 | the same thing in the Perl > Editor.
| | 01:19 | Down here, I need to take out that.
| | 01:21 | Otherwise, it'll continue to Insert Spaces
instead of Tabs. Then we'll press apply and OK.
| | 01:27 | And now we can see the whitespace characters.
| | 01:30 | We have the little paragraph
marker, which is for the new line.
| | 01:34 | We have these little dots for spaces
and if inset a tab here, you'll see that I
| | 01:39 | get this double right angle
bracket thing instead of a tab.
| | 01:44 | So, now we can talk about whitespace.
| | 01:47 | whitespace in Perl is new
lines, space characters and tabs.
| | 01:54 | And whitespace is pretty much
ignored in most circumstances.
| | 01:58 | There is a couple places where whitespace is
actually required, and so we'll talk about that.
| | 02:03 | Let's start the discussion by just
removing a bunch of whitespace from this
| | 02:07 | function here, and I'm going to
actually just shorten this text down a little
| | 02:11 | bit, and we'll get rid of this whitespace,
and so now we have this subroutine here
| | 02:18 | that's got only whitespaces between the
words sub and main, and that's actually required.
| | 02:24 | There has to be some kind of
whitespace in order for that keyword to be
| | 02:28 | isolated, and so that the Interpreter
knows that's a key word and not part of
| | 02:32 | the name of the function of the subroutine.
| | 02:36 | And so I'll go ahead, and I'll Save and
Run this, and we can see that we get the
| | 02:41 | word short text there. So, this is
actually still working, even though I've taken
| | 02:46 | out all of that whitespace.
| | 02:47 | So, most of the whitespace is ignored.
| | 02:50 | I can put in some whitespace here.
| | 02:52 | I can use maybe a Tab there.
| | 02:55 | I can put whitespace here and here.
| | 02:57 | I can put whitespace there, and there,
and there, and all of this whitespace,
| | 03:02 | and it can be new lines,
| | 03:03 | it can be combinations of things. I
can - it's a new line and a tab there.
| | 03:10 | I could actually indent
this a bunch, if I wanted to.
| | 03:12 | I can do all of these things,
and this will still run.
| | 03:16 | I'll Save, File > Save and
Run, and it's still working.
| | 03:23 | So, all of that whitespace is
actually ignored, and in fact, even this
| | 03:26 | required whitespace, I can put in a new line
and a couple of tabs, and that will still run.
| | 03:33 | That's pretty much how Perl uses whitespace.
| | 03:37 | In most places, you can eliminate it entirely.
| | 03:39 | It's really just necessary where it's
necessary to separate keywords and end
| | 03:44 | things that need to be separate.
| | 03:47 | And other than that, whitespace can
be wherever you want it to be for
| | 03:50 | formatting purposes, using my Undo
function here to bring this back to
| | 03:57 | something like its original state.
| | 03:59 | And this is the style that I happen to like.
| | 04:03 | I think it's important, when you're program,
| | 04:05 | to use the whitespace - if you've got a
language where whitespace is optional -
| | 04:10 | to use the whitespace as a way of
setting apart the things that are
| | 04:14 | important, making it easy on the eye,
so that you can read the code when you
| | 04:20 | come back to it at later time.
| | 04:21 | So, I tend to use whitespace as a way
of formatting the code so that I can read
| | 04:28 | it later, and that, in my opinion, is
the function of whitespace in a language
| | 04:32 | like Perl, where whitespace is mostly ignored.
| | Collapse this transcript |
| Terminating statements with semicolons| 00:00 | Continuing with our discussion of
general syntax in the Perl language, we're
| | 00:04 | going to talk now about semicolons.
| | 00:07 | I'd like to start by making a copy of
the syntax.pl file, so that we're not
| | 00:12 | working with the original.
| | 00:13 | Let me go ahead and do that here and
call it semicolons.pl, and we'll go
| | 00:21 | ahead and open that up.
| | 00:23 | You'll notice that statements in
Perl are terminated by semicolons.
| | 00:28 | There is one at the end of that statement there.
| | 00:30 | There is one at the end of that
statement there, and there is one there.
| | 00:34 | In general, they are required.
| | 00:37 | There are some exceptions
where a semicolon is not required.
| | 00:41 | A semicolon is not required at the end of a block.
| | 00:45 | So, if we take this semicolon, here, out -
because it's at the end of a block,
| | 00:49 | it's followed by this closing curly brace -
it doesn't actually generate an error at all.
| | 00:54 | I can go ahead and save this, and run,
and you'll see the script still works
| | 00:59 | fine, and it doesn't generate any errors.
| | 01:01 | I'm going to go ahead and put that back.
| | 01:05 | On the other hand, if I insert another
statement and go ahead and save that,
| | 01:13 | you'll notice that we have errors.
| | 01:14 | You'll see that the Eclipse system has
already started to parse it, and it has
| | 01:18 | found this error here.
| | 01:19 | It's a syntax error, "Bareword
found where operator expected."
| | 01:24 | Now this line here is
where the error actually is.
| | 01:27 | But this is the message from Perl.
| | 01:30 | This is the Editor knows
that that's what the problem is.
| | 01:34 | If we go ahead and turn on Explain Errors/
Warnings here, we can see that we have two errors.
| | 01:41 | We have the syntax error, and we have
this Bareword found where operator expected.
| | 01:47 | Now the syntax error is in red,
and that's matching up with this.
| | 01:50 | The Bareword found where operator
expected is matching up with this.
| | 01:55 | So, what's actually going on here,
the Eclipse system is obscuring
| | 02:00 | what's actually going on.
| | 02:01 | It's trying to make it a
little bit more friendly for you.
| | 02:03 | I want to show it to you in the
command line, so that you get an idea of the
| | 02:07 | kind of errors that you are actually
going to see when you have a problem like
| | 02:11 | this, so that you'll recognize it,
and you'll be able to say, "Oh!
| | 02:14 | I must be missing a semicolon."
| | 02:15 | I'm going to switch to my terminal
here, and I'm going to run this script.
| | 02:23 | Here is the error message
that you get out of Perl.
| | 02:27 | You'll notice that both of
these errors are saying line 13.
| | 02:32 | And if we look at line 13, that is this
line here. It is not the line where we
| | 02:38 | actually have the problem.
| | 02:40 | The reason for this is that the
Perl interpreter doesn't really notice
| | 02:44 | that something is missing until it gets
another statement, and it says, "Wait a minute.
| | 02:48 | This is not what's expected here."
| | 02:51 | It's treating this all as one statement,
but it sees two function calls without
| | 02:55 | any punctuation in between them.
| | 02:58 | So, it's not really
discovering the error until line 13.
| | 03:02 | That's why you have this "Bareword
found where operator expected at line 13."
| | 03:07 | That's the first thing that it noticed.
| | 03:09 | I'm not expecting that.
| | 03:11 | In this context, it's not even
knowing that that's a function call.
| | 03:15 | So, the problem is, and here it says,
syntax error at line 13, and that's as good
| | 03:22 | as the interpreter can do.
| | 03:25 | So, when you get that error, when
you get the error that says, "Bareword
| | 03:28 | found where operator expected," or even
just a syntax error, you want to look
| | 03:32 | at the line before and say, "Am I missing a
semicolon or a comma or some sort of punctuation?"
| | 03:39 | So, I'm going to go ahead and put the
semicolon back in there, and we're going to save it.
| | 03:44 | Now we see all of our error messages go away.
| | 03:47 | This, of course, it stays.
| | 03:49 | It's just the explanations, but
our error indicators here go away.
| | 03:53 | We can run the script, and it works just fine.
| | 03:56 | We get the two messages.
| | 03:59 | If I go back into the command line and I run
the script, we see the result that we expect.
| | 04:06 | So semicolons, in summary,
semicolons are required in Perl.
| | 04:11 | They terminate a statement.
| | 04:13 | So, a statement must have a
semicolon at the end of it.
| | 04:16 | The one exception is, at the
end of a block, a semicolon is not
| | 04:21 | necessarily required.
| | 04:22 | But it's still a really, really good idea.
| | 04:24 | I suggest that you put them there.
| | 04:26 | If you're missing a semicolon at the end
of a block, Perl is not going to complain.
| | 04:30 | But if you go and insert, say another
statement or two after that and forget to
| | 04:35 | put in the semicolon, you're going to
get errors, and you're going to have to
| | 04:37 | figure out where those errors are.
| | 04:39 | It's best just to put them in anyway.
| | 04:42 | They're supposed to be there.
| | 04:43 | It's just an exception to the rule.
| | 04:45 | So, semicolons terminate statements and
you want to use them all the time, even
| | 04:51 | when they're not absolutely required.
| | Collapse this transcript |
| Creating comments| 00:00 | Continuing our discussion of general syntax
in Perl, I'm going to talk about comments.
| | 00:06 | So, we'll start by taking this syntax.pl
script and making a copy of it.
| | 00:12 | I'll name it, comments.pl, just
so we don't overwrite the original.
| | 00:17 | Go ahead and edit it and
do whatever we want to it.
| | 00:21 | I'm going to start by writing a
little subroutine so we have something to
| | 00:25 | write a comment about.
| | 00:27 | This is a recursive factorial.
| | 00:30 | It's typically a programming exercise
that you might get in school if you're
| | 00:35 | learning programming.
| | 00:38 | A factorial is a mathematical function.
| | 00:41 | It's the product of all the integers
up to and including the integer that
| | 00:45 | you are factorialing.
| | 00:47 | So, we'll start with the
number and shift or return 0;.
| | 00:53 | That means that the function will be
passed a number and if it's not passed a
| | 00:59 | number, I'll just return zero, which
will be the answer for a missing number.
| | 01:04 | If $n > 1 - because if it's not, then
the factorial is always 1 - then we'll
| | 01:12 | return $n * factorial
| | 01:16 | as the recursive
part, because it's calling itself, $n - 1.
| | 01:22 | And else, return 1;.
| | 01:26 | So, in the case where it is
equal to 1, we just return 1.
| | 01:29 | Now I can explain all of
this in comments if I want to.
| | 01:34 | I can say here, factorial (n), returns
the product of all integers up to and
| | 01:47 | including n. I can say here, return 0 if no n.
| | 01:57 | So, comments in Perl are introduced
with the hash mark, that symbol there,
| | 02:02 | little tic-tac-toe-y-looking thing.
| | 02:05 | Everything including the hash mark
and to the end of the line is ignored by
| | 02:11 | the Perl interpreter.
| | 02:12 | So, they can be in a line by themselves
like that, or they can be out here at the
| | 02:17 | end of a line that actually has code in it.
| | 02:21 | So, anytime you have this hash mark,
everything from there to the end of will
| | 02:25 | be ignored by the interpreter.
| | 02:26 | So, that is a comment.
| | 02:29 | So, I can say here, only
compute for n > 1, return 1 for n = 1.
| | 02:46 | I can say recursion,
because it's calling itself.
| | 02:50 | So, I have all these comments that I can make.
| | 02:53 | In real life, I don't tend
to write this many comments.
| | 02:56 | Comments are an excellent thing.
| | 02:58 | They're great thing to do, because it
explains what's going on to somebody who
| | 03:04 | comes by to read it later.
| | 03:06 | If you have too many comments, there is
a tendency for people to just read the
| | 03:10 | comments, and then they miss the code.
| | 03:12 | So, something like this one that
says recursion is really superfluous.
| | 03:17 | This one here is probably also
superfluous because you can see from the code, if
| | 03:21 | ($n >1), so there's some
guidelines for how to comment.
| | 03:28 | Then up here, I can just call the function.
| | 03:31 | I can say $n = shift or 5 if nothing is
passed in and $r = factorial of n, and
| | 03:47 | message ("$n factorial is $r").
| | 03:54 | I can save this and run it. 5 factorial is 120.
| | 04:01 | If you remember, the first few
factorials from math class many, many years ago,
| | 04:07 | you'll remember that that is the correct answer.
| | 04:09 | You'll notice that I forgot the
semicolon at the end here, and it worked anyway,
| | 04:14 | because it's the end of a block.
| | 04:17 | I'm going to go ahead and put that in.
| | 04:20 | So, here we have, just an example of
where you might use comments and how they
| | 04:26 | are formatted in Perl.
| | 04:28 | So, it's a good idea to
comment your subroutines, like this -
| | 04:31 | I usually do - with how it's
called and what it returns.
| | 04:36 | You might want to have comments on
anything that you might want to explain that
| | 04:39 | might actually warrant some explanation.
| | 04:42 | I don't know. In this case,
| | 04:43 | in real life, I might not have
commented any of this, because this all seems
| | 04:47 | pretty simple to me.
| | 04:48 | But for the benefit of people who
might come after me and might want to read
| | 04:53 | this code later, I might put these
comments here, and I might say something up
| | 04:57 | here, like, computed
recursively to make that obvious.
| | 05:03 | But that's how comments are used in Perl,
and that's how comments are formatted in Perl.
| | 05:08 | Those are some general guidelines
for using comments in your code.
| | Collapse this transcript |
| Best practices for formatting code| 00:00 | Continuing our discussion of general
syntax in Perl, I would like to talk about
| | 00:04 | a few best practices in
making your Perl scripts.
| | 00:08 | So, we'll start with our syntax.pl,
and we'll make a copy of it, and we'll
| | 00:14 | name it general.pl.
| | 00:18 | Go ahead and open that.
| | 00:19 | But I'm going to maximize this,
because we're really just going to be
| | 00:22 | talking about this.
| | 00:23 | This is a template, and I've
included it in your Exercise Files.
| | 00:28 | If we look over here at the Exercise
Files, outside of all these folders, you'll
| | 00:33 | see one that says template.pl.
| | 00:35 | And that's what this is.
| | 00:39 | This has in it just a lot of the
things that I tend to put in my Perl scripts
| | 00:44 | as a starting place.
| | 00:46 | I use something very similar to this.
| | 00:47 | When I'm writing a new Perl script,
I'll start from a little template file.
| | 00:50 | It will have pretty much
the same stuff that's in here.
| | 00:54 | So, let's talk about
what these all elements are.
| | 00:56 | The first one you'll see is at the top,
and this is called the shebang line
| | 01:00 | in the Perl world.
| | 01:02 | And that refers to these two
characters here, which is the comment marker,
| | 01:07 | the has mark, or the pound sign, followed by
an exclamation point, which is often
| | 01:11 | called a bang.
| | 01:13 | What that does is it tells the UNIX
shell to go ahead and run this interpreter
| | 01:20 | when it encounters this kind of a file.
| | 01:22 | So, the interpreter here is /usr/bin/
perl, which is the common location of Perl
| | 01:27 | on a UNIX-based system.
| | 01:28 | So, I'm on a Macintosh here,
which is a UNIX-based system.
| | 01:32 | If I go over to my command line, which
is a bash shell - this is a UNIX shell -
| | 01:37 | I'll look at the files here, and I see
there is my general.pl, which is the file
| | 01:43 | we're looking at in the Editor there.
| | 01:45 | If I run it the normal way, just say
Perl and general.pl like that, you'll see
| | 01:50 | that it executes the Perl script.
| | 01:52 | I can also just say ./general.pl,
and it will run the same way.
| | 02:01 | It is actually running Perl.
| | 02:02 | It's doing exactly the same thing.
| | 02:04 | It's just that the shell is reading that
first line, and it sees that first line
| | 02:12 | refers to a Perl program, and it
will go ahead, and it will run Perl.
| | 02:16 | It just does the same thing.
| | 02:19 | It's a shortcut;
| | 02:20 | it's a convenience, and it's a good idea.
| | 02:22 | There are some environments like a web
server environment, which may also read
| | 02:27 | this line and find the right
interpreter for doing something like running CGI.
| | 02:31 | So, it's a good idea.
| | 02:32 | And again, if you have it in your
template, it doesn't cost you anything to
| | 02:35 | put it there.
| | 02:36 | The next couple of lines are just
comments that say who wrote the script, how
| | 02:41 | you can contact him, and who it belongs to.
| | 02:45 | It's always a good idea to have that there.
| | 02:46 | You can also add a line that says this
is generally what the script does. You can
| | 02:50 | have a little bit of usage information.
| | 02:52 | You can refer to another
file that has documentation.
| | 02:55 | So, a lot of good things you can
do in a comment block at the top of
| | 02:58 | your script.
| | 02:59 | So, I suggest you always include one.
| | 03:02 | The next two lines are use strict and
use warnings and most Perl programmers,
| | 03:07 | most Perl books will tell you, always
use strict and use warnings, and I'm going
| | 03:11 | to say the same thing.
| | 03:12 | It will give you a few extra error
messages while you're writing and debugging.
| | 03:17 | In the long run, it will clean up your
coding, and it will make your programs
| | 03:20 | more portable and just more reliable.
| | 03:23 | So, it's a great idea to always
include use strict and use warnings.
| | 03:29 | This next bit is something that
a lot of programmers don't do.
| | 03:32 | I do.
| | 03:33 | I find this to be an
important discipline in programming.
| | 03:38 | Perl does not require a main function.
| | 03:40 | Some languages do, like C and Java.
| | 03:42 | I think it's a great discipline.
| | 03:46 | It keeps that main piece of code tidy.
| | 03:48 | It keeps it all in one place.
| | 03:50 | It keeps from having code outside
of the scope of a block in the global
| | 03:55 | scope, where it can messy.
| | 03:57 | It can get lost.
| | 03:58 | It can be more difficult to maintain.
| | 04:00 | It can get interspersed with all kinds
of declarations and things that belong
| | 04:04 | out there.
| | 04:05 | So, I just think this is a great discipline.
| | 04:07 | It's not required, but again,
it doesn't really cost anything.
| | 04:11 | You just put it in your
template, and it's always there.
| | 04:15 | Likewise, with these two and
sometimes I'll include some other ones as well
| | 04:19 | in my templates.
| | 04:21 | Message, again, starting in Perl 5.10,
there is an equivalent function called
| | 04:26 | Say, but at this point in time, as I
record this, I can't really count on 5.10
| | 04:32 | being installed on many systems.
| | 04:34 | So, I'll go ahead I'll continue to
use this for a few more years until 5.10
| | 04:39 | is widely deployed.
| | 04:40 | In the meantime, all this is
is it's this function here.
| | 04:44 | It allows me to write something to the
standard out and not have to put a new
| | 04:49 | line at the end of it, which
is oftentimes easy to forget.
| | 04:52 | It's just a convenience function.
| | 04:54 | Likewise, with error, I'll often
have this go to standard error instead
| | 04:57 | of standard out.
| | 04:58 | In this case, it's going to standard out.
| | 05:00 | We'll talk about that when we talk
about I/O and streams, but what this does is
| | 05:05 | it outputs an error message, and it exits.
| | 05:09 | That's a great convenience for
error conditions and debugging.
| | 05:13 | So, there you have it.
| | 05:14 | These are, I just think some minimal
best practices, some minimal habits that
| | 05:18 | you'll want to get into.
| | 05:20 | Like I said before, it's easy.
| | 05:21 | It doesn't cost anything.
| | 05:22 | You just put it in your template and
use it as a starting place whenever you
| | 05:25 | start a new Perl project.
| | Collapse this transcript |
|
|
3. Variables and ValuesUnderstanding values and variables| 00:00 | I'm going to talk for a moment about
the concepts of values and variables, and
| | 00:03 | how these are represented in Perl.
| | 00:06 | Values represent data,
whereas variables contain values.
| | 00:13 | For example, here we have a
Perl statement, $number = 42;.
| | 00:19 | The dollar sign indicates that this is going
to be a variable, and there's several
| | 00:25 | different symbols that can introduce a variable.
| | 00:27 | We'll get to those later on.
| | 00:29 | In this case, the dollar sign means that the
variable is one-dimensional, which, in Perl,
| | 00:33 | is called a scalar.
| | 00:35 | And number is the name of the variable.
| | 00:38 | So, you have the dollar sign followed
by the name of the variable.
| | 00:40 | The equals sign is the assignment operator, so
we're assigning a value to the variable.
| | 00:47 | The value is 42, which is an integer
number, and the semicolon, of course,
| | 00:52 | terminates the statement.
| | 00:54 | So, here we have a Perl statement
that assigns the value, 42, to the
| | 00:59 | variable $number.
| | 01:02 | Perl provides several types of values.
| | 01:06 | Values can be numbers, as it is in this case.
| | 01:09 | They can be strings, which are a
sequence of characters that may be displayed
| | 01:14 | on a screen.
| | 01:16 | These numbers and strings can be
aggregated into different type of structures,
| | 01:20 | including scalars, which is a one-
dimensional value that can contain one
| | 01:25 | value, Arrays, which can contain
sequences of values, Hashes, which are like
| | 01:31 | arrays, but they are keyed or indexed,
and References, which are values that
| | 01:38 | point to other values.
| | 01:39 | We'll get into all of
these later on in this chapter.
| | 01:43 | It's important to note, at this point,
that Perl uses context to determine type.
| | 01:48 | Perl is not a strongly typed language.
| | 01:51 | In other words, when you declare a
variable, you don't declare its type.
| | 01:55 | Its type is determined by the context
of the value that's being assigned to it.
| | 02:01 | We will get into that in a lot
more detail in this chapter as well.
| | 02:05 | So, the concept you want to get from
this is the distinction between value
| | 02:09 | and variable.
| | 02:10 | A value is the data itself, and the
variable is the container that contains
| | 02:16 | the value.
| | Collapse this transcript |
| Declaring numeric variables| 00:00 | In your Exercise Files folder, you'll
notice a folder 03 Variables, and inside
| | 00:05 | of that folder, there is a file
variables.pl. Go ahead and make a copy of that
| | 00:10 | now, so we don't write over the original.
| | 00:12 | We'll just call it numbers.pl.
| | 00:15 | Now, go ahead and open that,
and there we have numbers.pl.
| | 00:24 | We're just going to start right here
and start declaring some variables,
| | 00:30 | my $decimal = 12345;.
| | 00:35 | So, what this does is it declares a
variable called decimal, and because of the
| | 00:39 | dollar($) sign, this is a scalar
variable, which means it's a one-dimensional
| | 00:43 | variable and assigns the value
12345 to it, which is a decimal number.
| | 00:48 | Now first of all, notice this my
keyword, and what that does is it declares a
| | 00:54 | variable to have a lexical scope
inside the block and which you have defined.
| | 00:59 | If you hover the mouse over here in
Eclipse, you will get a little pop-up
| | 01:04 | definition and we see this definition:
"A 'my' declares the listed variables to
| | 01:11 | be local (lexically) to the
enclosing block, file, or 'veal'."
| | 01:16 | So, why this is important is because
with strict and warnings invoked up here, if
| | 01:23 | you do not declare the lexical scope
of a variable, if you just say decimal
| | 01:28 | equals like that, you will get an
error. And you see if I hover my mouse over
| | 01:31 | here, it says global symbol
requires explicit package name.
| | 01:36 | So, all that means is you forgot to type my.
| | 01:39 | You want to put my there, and that
makes this lexically scoped to inside
| | 01:45 | this block.
| | 01:46 | What that means is that if you have
another variable on the outside here, = 23456,
| | 01:57 | and you go ahead and use this,
| | 02:00 | you'll be using the one that's inside
the scope and not the one outside of
| | 02:03 | the scope.
| | 02:04 | So, if I say message($decimal)
and run that, we will get the 12345.
| | 02:14 | If I had not declared this in here,
and we run that, we would get the 23456.
| | 02:23 | That is the meaning of the my declaration.
| | 02:29 | It declares the variable as being
lexically scoped inside of the block that
| | 02:36 | you're enclosing it in, and you
just need to do that all the time.
| | 02:39 | You need to always have the my
there as long as you're using strict
| | 02:42 | and warnings.
| | 02:43 | It's there as a discipline to make
your code more reliable, so that you don't
| | 02:48 | end up mixing up your name spaces and
having a variable that you think is one
| | 02:53 | thing and turns out to be
something else entirely.
| | 02:56 | So, this is a decimal number, because
we have assigned a decimal value to it.
| | 03:01 | A decimal value is a sequence of
numbers that does not begin with 0.
| | 03:05 | If it begins with zero, it's an octal number.
| | 03:12 | So, that would give us an
entirely different value.
| | 03:15 | I am going to go ahead here, and I'm
going to put in these message statements as
| | 03:19 | I go along, decimal is $decimal and octal
| | 03:27 | is $octal, and I see I have a typo here.
| | 03:33 | I misspelled decimal there.
| | 03:39 | Now, if we save this and run it, I'll
see that decimal is this number and octal
| | 03:45 | is this entirely different number.
| | 03:47 | That's because of this leading 0 here
tells us that this is an octal number and
| | 03:53 | so it assigns it in base 8, but when we
print it here, this will always print in
| | 03:59 | decimal base 10 and so it's converting
it, and there is the value that we get.
| | 04:04 | So, we are getting a different value.
| | 04:05 | So, just know that if you have a leading
0, you are always going to get an octal
| | 04:09 | value and not a decimal value.
| | 04:12 | We can also do hexadecimal, which is
base 16, and that looks like this, and
| | 04:18 | I will go ahead and copy and paste here,
say hex is $hex and if I run that,
| | 04:31 | it looks like that.
| | 04:33 | Again, it's printing it in decimals,
so we get this other looking value.
| | 04:38 | We'll go ahead and show you
some more types of numbers.
| | 04:42 | We can do binary.
| | 04:46 | That's done with a 0b, and go ahead
and do a print statement for that, binary,
| | 05:00 | and run it and see that
we get yet another value.
| | 05:05 | And the other types include a
floating-point number, or a number with an
| | 05:18 | exponent, scientific notation. e56
means that you would move the decimal point
| | 05:28 | over 56 places to the left.
| | 05:31 | We'll go ahead, and we'll print those
as well. float, $float and exp for the
| | 05:47 | scientific notation one.
| | 05:49 | I will go ahead and save and run,
and we see these values here.
| | 05:55 | These are the different types of
numbers available, and you'll notice that the
| | 06:00 | assignment statement has the various
types of numbers on the right, but on the
| | 06:04 | left, they all look pretty much the
same because we are just assigning them to
| | 06:08 | a scalar container.
| | 06:11 | The value itself, the context of
it is what's determining the type.
| | 06:16 | So, the way that I type this is
what determines the type of the number.
| | 06:21 | So, those are the basic number types
that are available in Perl, and that is how
| | 06:27 | you specify them in an assignment.
| | Collapse this transcript |
| Declaring character string variables| 00:00 | Continuing our discussion of
variables in Perl, we're going to talk
| | 00:03 | about strings.
| | 00:05 | So, we'll go ahead and make
a copy of this variables.pl,
| | 00:08 | so we don't write over it and just
call it strings.pl. And we'll open that
| | 00:16 | and here we go.
| | 00:18 | So, this, of course, is a string
because it's in quotes, and if we were to assign
| | 00:26 | it to a variable instead, and we
could say my $s = "this is a string";
| | 00:38 | and if we message($s);
| | 00:43 | and Run that, it will say this is a string.
| | 00:48 | So, strings can be in
single quotes or double quotes.
| | 00:52 | This editor, of course, wants to match
quotes automatically, so I have to do a
| | 00:56 | little finagling there.
| | 00:58 | This will work just as well, since
this is a string. And the difference is is
| | 01:06 | that if I had another variable, say my $
n = 42 and I put a $n in there like that,
| | 01:18 | and run it, if I have the single
quotes, it doesn't interpolate.
| | 01:22 | It'll go ahead and say $n. If instead, I
have it in the double quotes - I'm just
| | 01:28 | going to go ahead and do a different one
| | 01:31 | where $s = "this is a string:
| | 01:34 | $n", like that. What does this
warning say? Ah, masks an earlier one.
| | 01:42 | So, that error means that I have named
two variables with the same name in this
| | 01:47 | scope, so I'll call this one s2 and
this one s1, and I'll message($s1), and I
| | 01:53 | will message($s2), and
I'll save that and run it.
| | 02:01 | Now we'll see that the second
time it actually interpolates.
| | 02:05 | It takes this $n, and it replaces it
with the value, so it's reading that
| | 02:11 | variable and interpolating the
variable, rather than just displaying the
| | 02:15 | characters $n. With the single quotes,
it does not interpolate, and so it will
| | 02:20 | not get you the value of $n. It will
just put out the characters, a dollar sign
| | 02:25 | and an n character.
| | 02:27 | So, that's the difference there.
| | 02:29 | Now, in fact, you don't
always have to use quotes.
| | 02:33 | If I wanted to say put this $n in quotes,
I would have to say \" and \" in order
| | 02:42 | to actually get the quote character,
and I've got two quote characters next to
| | 02:47 | each other because one of them is
escaped with the backslash and one of them is
| | 02:50 | not, and then I'll get quote
marks around this. Save and run.
| | 02:57 | I get quote marks there.
| | 02:58 | If I want quote marks in this one,
because it has the single quote, I can just
| | 03:02 | put in a quote mark like that, and I
don't have to escape it because there isn't
| | 03:07 | a danger of it being interpreted as
ending the string. And so I'll Save and Run
| | 03:14 | and I get the quote marks there.
| | 03:16 | The opposite is true if I want to use
the single quotes. If I want to use the
| | 03:19 | single quote here, I just put it in
there like that because I'm in double quotes
| | 03:25 | and so I don't have to escape it.
But in this case, if I want to use the single
| | 03:29 | quote, I actually have to escape it and
use the backslash single quote because
| | 03:34 | my string is enclosed in single quotes
and so I'll Save that and Run, and you'll
| | 03:42 | see that now we have single quotes.
| | 03:45 | You can get around this by using
Perl's special quote operator, which is a q
| | 03:50 | followed by a delimiter.
| | 03:52 | So, for example, here in this first
one, I can do a single quote by saying q
| | 03:57 | and using any of the delimiter characters.
| | 04:00 | So, in this case, I'm going to use say
a curly brace and I'll go ahead and put
| | 04:05 | the other curly brace at the end here.
| | 04:07 | Now I don't have to quote these single quotes.
| | 04:10 | I can just do it like that and because
there's just one q, this works exactly
| | 04:16 | like the single quotes.
| | 04:17 | So, it won't interpolate the $n, and
I'll Run that, and it looks just like that.
| | 04:22 | And in this other case, if I wanted to
interpolate I can use two qs, and I'll
| | 04:28 | put in the bracket, and Eclipse
goes and matches my bracket for me and so I
| | 04:34 | have to move it over there. And now I
have two qs followed by a delimiter and so
| | 04:39 | this will interpolate.
| | 04:40 | It works just like a double quote.
| | 04:42 | So, I'll go ahead Save and
Run, and we have that there.
| | 04:47 | So, I can put double quotes in here
without escaping them, just like this,
| | 04:53 | and that will work.
| | 04:55 | I'm going to Save and Run, and we
have our double quotes there, and
| | 04:59 | it's interpolated.
| | 05:00 | It's getting me the value.
| | 05:02 | You can use any delimiters. For example,
| | 05:05 | I could use a vertical line instead of
the curly brace, and in that case, I don't
| | 05:11 | need a matching one because there's
only one kind of them. And this will work
| | 05:15 | just fine there. And so this is a
flexible way of being able to declare strings
| | 05:22 | without using quote marks for
cases where you might want to actually have
| | 05:25 | the quote marks inside of the string.
| | 05:29 | Finally, let's talk a little
bit about concatenating strings.
| | 05:33 | If I wanted to print both string one
and string two, I could say message($s1 .
| | 05:40 | $s2 );, and it will concatenate them.
| | 05:45 | It will put one right after the
other and so there won't be a space.
| | 05:49 | So, after this quote, there will be
just the word 'this' without a space there.
| | 05:54 | So, I'll Save and Run, and
I have a third line here.
| | 05:59 | This is a string:
| | 06:01 | '$n'this is a string, and it's got
the two strings concatenated with the
| | 06:04 | concatenation operator.
| | 06:06 | So, if I wanted actually to have a space
there, I would have to put one in. So, I
| | 06:11 | have a quote and a space and another
concatenate operator with a dot there, and
| | 06:17 | that will put a space in between them.
| | 06:20 | So, I could run that and, of course,
you can concatenate literal strings.
| | 06:25 | This s1 could become something like
Eclipse is trying to figure out what I
| | 06:30 | mean instead what I say, this is a
string, and I'll say dot this is another
| | 06:41 | string. And, of course, in between them,
there's not going to be a space, so
| | 06:45 | let's just go ahead and put one in, and
we'll maybe use a colon character. And
| | 06:51 | you'll notice that in this case, I've
used the single quotes instead, and I'll
| | 06:55 | go ahead and Save and Run.
| | 07:00 | Now the first string has this.
| | 07:01 | This is a string colon. This is
another string, and there it gets all
| | 07:06 | concatenated down here.
| | 07:08 | So, that's how strings work in Perl.
| | 07:11 | Strings are scalar values.
| | 07:13 | They are one-dimensional, and they can
be concatenated using the dot operator.
| | 07:18 | Single quotes do not interpolate
variables; double quotes do. And you can, of
| | 07:24 | course, use a single q or a double q
with any delimiter to get around having to
| | 07:30 | escape your quote marks.
| | 07:32 | So, that's how strings work in Perl.
| | Collapse this transcript |
| Declaring lists and arrays| 00:00 | We're going to talk about
lists and arrays now. And in your 03
| | 00:03 | Variables directory,
| | 00:05 | we are going to make a copy of the
variables.pl file, and we'll call it
| | 00:12 | lists.pl, and we'll go ahead and
open that, and we'll talk about lists
| | 00:19 | and arrays.
| | 00:20 | A list is simply a list of
values or variables or list of things.
| | 00:26 | And so we can say my @array = - now an
at sign, like a dollar sign, specifies a scalar
| | 00:36 | variable and an at sign specifies an array
or a list variable. Then in parentheses, I
| | 00:43 | can put a number of different values
(1, 2, 3, 4) like that, and now I
| | 00:49 | have an array that has four scalar
values in it, and those values are numbers.
| | 00:56 | And they're 1, 2, 3 and 4.
| | 00:59 | A simple way to look at the values of
an array is to use the join function.
| | 01:05 | It's a built in function. And so I can
say message join, and I'll put a colon
| | 01:11 | for the separator and
@array. And what this will
| | 01:16 | do is it will display these values with
in a colon in between them, so I'll go
| | 01:21 | ahead, and I'll save, and I'll run and
we have 1, 2, 3, 4, with colons
| | 01:28 | in between them.
| | 01:29 | These values in the list can be anything.
| | 01:32 | I could put a string in here, put the
word two, and I can Save that and Run it,
| | 01:41 | and you'll see we now have the
word "two" in the middle layer.
| | 01:44 | And in fact, if I want to, I could
assign this to a list of scalars instead
| | 01:52 | of to an array.
| | 01:53 | I can say my $one, $two, $three, $four
| | 02:01 | and so those are four scalars
that are named one, two, three and four,
| | 02:06 | and I can say = ( 1,2, 3, 4 )
like that, and then I can
| | 02:12 | just print them out like normal
variables, one, and I'll just go
| | 02:20 | ahead and copy and paste that four
times and name them two, three and four. And
| | 02:29 | so now I'm printing out those four
variables one at a time but you see I've
| | 02:32 | assigned them all at once because I've
simply assigned one list to another, like
| | 02:39 | that. And so we'll Save and Run, and
we'll see that it prints them 1, 2, 3, 4, on the
| | 02:47 | line like that, just as
they are here 1, 2, 3, 4.
| | 02:50 | I'm going to go ahead and get
that out of the way and delete these.
| | 02:58 | We have here our array, and
the array has four values in it.
| | 03:05 | If I want to just look at one of them,
I can say $array with a subscript,
| | 03:11 | and inside of that subscript I
put a number. If I say subscript 1,
| | 03:15 | I will get the second element.
I'll get the one here that says
| | 03:20 | two because these subscripts are zero-
based, so 0 would be the first one and
| | 03:25 | one is this one and two is that one,
and three is that one. And so $array
| | 03:30 | subscripted to 1, like that, will give
me the second one, which says two, and so
| | 03:37 | there we have two.
| | 03:39 | So, there's a couple of
things to notice about this.
| | 03:41 | One, of course, is the subscript is in
square brackets, like that, and that we do
| | 03:48 | not use the at sign when
we're subscripting an array.
| | 03:51 | And the reason for this is that what we are
extracting from the array here is one
| | 03:57 | scalar value, so the scalar indicator is
the dollar sign and so even though the array
| | 04:04 | variable is an array variable,
and it was declared with the at sign,
| | 04:09 | the dollar sign is used when we want to
subscript and get one value out of it, using
| | 04:15 | the subscript operator, the square brackets.
| | 04:19 | So, in this case, we've gotten the second value.
| | 04:21 | We could, of course, get the first
value with a 0. And we'll run that and we
| | 04:27 | see we get the number 1 there, or let's
say we wanted to know how many elements
| | 04:33 | there are in the array.
| | 04:35 | I can say there, I get a little
flummoxed by the way that Eclipse interprets my
| | 04:41 | quote marks there, and it did it again,
so I need to delete first and then okay,
| | 04:48 | there are, and I can say . scalar @array.
array, like that, elements in the array
| | 04:59 | and what this does - you'll notice the string
| | 05:03 | concatenation operator
| | 05:04 | there and there and so there are space -
and then this will give us the number of
| | 05:12 | elements in the array, the count of
them, which in this case is four, because
| | 05:15 | there's one, two, three,
four elements in the array.
| | 05:18 | So, what scalar does, it's like a cast -
if you know what casts are in other
| | 05:22 | languages - what it does is it
gives this array a scalar context.
| | 05:28 | And in a scalar context, what this
@array syntax returns is not the list of
| | 05:34 | elements in the array, but the number of
elements in the array. And this is just
| | 05:39 | a little Perl-ism, and so in the scalar
context, what you get from an array like
| | 05:45 | this is the number of elements. And so
this will say there are four elements in
| | 05:50 | the array when we run it. And so we'll go
ahead and save and run and we get there
| | 05:55 | are four elements in the array.
| | 05:58 | So, that's how you declare an array.
| | 06:00 | That's how you subscript an array,
and that's how you use an array and how
| | 06:04 | you would assign a list of values to
an array. And if you're interested in how
| | 06:08 | you slice out combinations of values out
of an array, I'm going to talk about
| | 06:13 | that in the next movie.
| | Collapse this transcript |
| Pulling slices from arrays| 00:00 | So, now we are going to talk about a
technique called slices, and this is a very
| | 00:04 | useful, very cool technique that Perl has.
| | 00:08 | A lot of modern languages have
slices, or something like them.
| | 00:11 | It's not something you'll use everyday,
probably not anyway, but when you do
| | 00:15 | need them, you'll be really
happy that they're available.
| | 00:18 | So, let's go ahead and make a copy
of the variables.pl, and we'll call
| | 00:22 | this slices.pl.
| | 00:26 | And we'll go ahead and open that up. And I
am going to start out by declaring an array.
| | 00:32 | And we will give it all the
numbers from 1 to 10, using this -
| | 00:40 | you haven't seen this before -
| | 00:42 | this is the range operator. And what
that does is it gives us a list of numbers, 1
| | 00:47 | through 10, just as if we have typed 1,
2, 3, 4 all the way up to 10. Instead,
| | 00:53 | it's done with this nice little
range operator 1..10, like that.
| | 00:57 | So, if we take a look at the array at
this point, we use the join operator, like
| | 01:02 | we did in the last movie,
array and take a look at it,
| | 01:08 | you'll see that just as if we have
typed all those numbers, it comes out
| | 01:12 | 1:2:3:4:5:6:7:8:9:10, just like that.
| | 01:16 | And so, this will be useful
for looking at our slices.
| | 01:20 | Let's say that instead of all of the
numbers, I just wanted to see, say the
| | 01:26 | first and the forth and the
seventh element of the array.
| | 01:31 | So, I can just type in, 1,4,7
like that, separated by comas.
| | 01:37 | What this will do is this will give us
the elements numbered 1, 4, and 7, which
| | 01:43 | is actually the second,
fifth and eighth elements.
| | 01:47 | So, we will get, 2, 5 and 8.
| | 01:51 | Go ahead and Save that and Run,
and there we have 2, 5 and 8.
| | 01:57 | So, these are the elements numbered one,
you'll remember that we start at zero.
| | 02:02 | So, the element number one is actually
got a 2 in it, and the element number
| | 02:06 | four has actually got a 5 in it, and
the element number seven has actually
| | 02:09 | got eight in it.
| | 02:10 | We can use the range
operator here too, if we want to.
| | 02:13 | We could say, 0..
| | 02:14 | 2, 7 and so we will get a 1, 2,
3 and 8. Go ahead and Save that.
| | 02:25 | And there we have 1, 2, 3 and 8.
| | 02:27 | So, we use the range operator.
| | 02:28 | So, this is actually - what this is
is it's a list, and it's a list of the
| | 02:33 | elements that we want.
| | 02:34 | They don't even have to be in order.
| | 02:35 | We can say 9, 3, 5, and that should be a
comma, not a period, and go ahead and Save and Run.
| | 02:46 | And you'll see that we get
these out of order now 10:4:6.
| | 02:50 | So, this is called slices, and as
you can see, you can do a lot of neat
| | 02:55 | things with them.
| | 02:56 | Notice that I'm using the at sign here.
| | 02:59 | When you've subscripted array before,
to get one element you use the dollar sign.
| | 03:04 | And that's because your result was
a scalar, one, 1-dimensional value.
| | 03:09 | Because our result now is a list,
it is the slices of the array,
| | 03:14 | we use the at sign because our result is
now going to be a list and not a scalar.
| | 03:21 | So, that is how you do slices.
| | 03:23 | They can be very useful.
| | 03:24 | You probably won't use them every day,
but when you do use them, you'll be
| | 03:28 | really glad that they are available.
| | Collapse this transcript |
| Understanding hashes and associative arrays| 00:00 | Now we are going to talk about hashes,
which are a special type of array.
| | 00:04 | Sometimes they are called associative arrays.
| | 00:08 | These are arrays that have keys and
values, rather than just scalar elements.
| | 00:13 | And so we'll start by making
a copy of the variables.pl.
| | 00:18 | We'll call it hashes.pl, like that.
| | 00:23 | And open that up and start by declaring -
move this out a little bit - start by
| | 00:31 | declaring a hash variable.
| | 00:35 | So, hash is with the percent sign, like that,
and then it is very much like a list.
| | 00:43 | And in fact, it is a list.
| | 00:45 | It's a special case of a list.
| | 00:47 | So, we'll say this and that, and you'll
notice the Equal sign with the right
| | 00:54 | angle bracket, and that declares that
this is a key, and that is a value, and
| | 01:01 | then I can say comma.
| | 01:03 | Actually, instead of that, let's
just make that something else like foo.
| | 01:07 | And that can be bar, and the other
can be baz, like that and a semicolon.
| | 01:21 | And so we have now a hash array, or an
associative array with three keys, and
| | 01:29 | they are associated with three values.
| | 01:31 | So, this is foo.
| | 01:32 | That is bar and other is baz.
| | 01:36 | We can look at any one of these
elements using something like this, $hash and
| | 01:45 | say, that, like that.
| | 01:50 | And so this syntax here - again, we
use the dollar sign because our result is
| | 01:55 | a scalar.
| | 01:56 | Hash is the name of our hash variable
and in curly braces, we have the key that
| | 02:03 | we are looking for, which is that.
| | 02:07 | So, what we will get is the value bar.
| | 02:09 | That's what will display down here.
| | 02:11 | So, let me go ahead and Save and Run,
and there we have bar because that is the
| | 02:18 | value that is keyed on that.
| | 02:20 | If I were to put other here and Save
and Run, we get baz, which is the value that
| | 02:30 | is associated with other.
| | 02:33 | So, there you have the basic way
that you declare and read the value of a
| | 02:41 | keyed element in hash.
| | 02:44 | Of course, there are some other
things that we can learn about this.
| | 02:47 | In fact, this is a special case of an
array, and so these arrows and the equal sign
| | 02:53 | with the right angle brackets
were simply shortcuts for commas.
| | 02:57 | And the reason that they are there is
to enforce the even numbered element rule
| | 03:02 | that in a hash you must have an even
number of elements, but this syntax here
| | 03:09 | will actually work exactly the same.
| | 03:12 | I'll go ahead and Run that and
you see we get the same value.
| | 03:16 | It's much harder to read, and it's not
anywhere near as easy to write, and so we
| | 03:23 | will go ahead, and we'll use that great
little hash key value shortcut with the
| | 03:29 | equal sign and the right angle bracket.
| | 03:33 | And so we can see that that's an
important syntactic element, but like so many
| | 03:37 | things in Perl, there is really more
that one way to do it. And occasionally, you
| | 03:41 | will see lists in arrays cast into hashes.
| | 03:45 | And it's important for you to know
that they are technically the same
| | 03:49 | data structure.
| | 03:50 | They're just used in different ways.
| | 03:52 | So, I'll go ahead here, and I'll Save
this, and we will go ahead and Run and
| | 03:55 | make sure that we're back where we meant to be.
| | 03:59 | There is some other things that you
can do with hashes. You can get a list
| | 04:03 | of all of the keys.
| | 04:05 | A list of the keys would be with the
keys functions, so we can say keys and
| | 04:12 | %hash, and that will return a list of the keys.
| | 04:16 | Now because it's a list, I
can't just display it this.
| | 04:19 | I have to use something like join with
a colon, and we will go ahead and put in
| | 04:25 | a comma there and make sure that we have
the right number of closed parentheses.
| | 04:29 | And I'll Save that and Run.
| | 04:34 | So, there is a list of the keys, and
you'll notice that they are out of order:
| | 04:37 | other, that, this, actually in reverse order.
| | 04:40 | But that's not a guaranteed order at all.
| | 04:43 | If you want a guaranteed order, you need to sort it.
| | 04:46 | So, you could say sort, and again, we will
move that parenthesis out there, so we
| | 04:53 | have the right number of them.
| | 04:55 | And so this will put them in
alphabetical order, Save and Run, and other and
| | 05:02 | that and this, and so that
is the alphabetical order.
| | 05:05 | In fact, you can get the values as well,
let's say values instead of keys, and
| | 05:10 | you'll get foo, bar and baz.
| | 05:12 | And those would be in the order bar,
baz and foo probably because they are
| | 05:17 | alphabetical. Go ahead
and Run that, bar:baz:foo.
| | 05:23 | So, those are the important
elements of how hashes work.
| | 05:27 | It's very common to read a
hash in some sort of a loop.
| | 05:31 | And I am just going to give you an
example of that. You use a foreach loop. And
| | 05:34 | we will talk more about these loops later.
| | 05:36 | I will give you an example of how this works.
| | 05:41 | So, this is a very common
construct that you will see,
| | 05:47 | something like this foreach my $k for
the keys and get a list of the keys, and
| | 05:54 | they will come out sorted.
| | 05:55 | And then I can say something like
$v = $hash {$k}, like that. For each one of
| | 06:03 | these, that will give me the value,
and then I can print something like this.
| | 06:08 | I can say $k is $v, and we'll get a
list that will say for each of these in
| | 06:15 | sorted order of the keys
and its associated values.
| | 06:20 | We will Save and Run, and
there we have other is baz.
| | 06:24 | That is bar, and this is foo.
| | 06:28 | So, this is commonly how you will see
hashes used, in some sort of a loop like
| | 06:32 | this that pulls out the keys and
gets the values based on the keys.
| | 06:37 | So, that is how associative
arrays or hashes work in Perl.
| | 06:42 | That's how you declare them using the
list of key value pairs with the commas
| | 06:48 | and these lovely equal and angle bracket syntax.
| | Collapse this transcript |
| Getting value pairs using the each function| 00:00 | If you watched the movie on hashes,
you'll remember that we stepped through the
| | 00:03 | hash using the sort keys in a foreach loop.
| | 00:08 | I want to talk about another way to
do this, and this is a way that you
| | 00:11 | probably won't use often, and we'll talk
about why and why I didn't show in this
| | 00:15 | way in the first place.
| | 00:17 | We will start with the each-begin.pl
file, out of the 03 Variables folder, in the
| | 00:23 | Exercise Files folder.
| | 00:24 | We will make a copy of that, and
we will just call this each.pl.
| | 00:31 | Open that, and we'll see that this
kind of where we left off in hashes movie.
| | 00:37 | We have a hash defined, and
it has these elements in it.
| | 00:40 | And we have this foreach loop that
loops through using sort keys, loops through
| | 00:46 | the hash and prints out the key and the value.
| | 00:49 | So, we go ahead and Run it.
| | 00:51 | We will see that this is our result.
| | 00:54 | So, I want to show you another way to do this.
| | 00:57 | And this other way uses a simpler-
looking structure. We use a while loop,
| | 01:09 | like this.
| | 01:10 | And in this case, we no longer
even need this line because we have
| | 01:15 | already assigned it.
| | 01:16 | The way that this works is it declares
this list of variables, $k and $v, and it
| | 01:23 | uses the each function to get each
key and value pair from each element of
| | 01:28 | the array.
| | 01:29 | And it assigns those as a list and so
this list pick out up in to these two
| | 01:34 | elements, and then you can just print
them out in one fell swoop, as it were.
| | 01:38 | And while this might look a lot
simpler on the surface, it has one
| | 01:43 | key disadvantage.
| | 01:44 | Let me go ahead and Save this and Run it,
and you'll see we get exactly the same
| | 01:49 | result, in this case.
| | 01:51 | The problem is is that the order
that these come out, the order that the
| | 01:56 | elements are traversed out of
the hash, is actually random.
| | 02:01 | And in this case, it
appears to be the same order.
| | 02:04 | It is the same order.
| | 02:05 | The problem is that we only
have three elements in this hash.
| | 02:08 | And if we do it in a longer hash,
we will see that it's not in order.
| | 02:13 | So, by way of comparison, I am going
to go ahead and print a divider here.
| | 02:20 | And then we will put the other loop back after
this one the way it was before the foreach loop.
| | 02:27 | We will assign the value like
that and print out the message.
| | 02:48 | And when we run this, we'll see that
we have two lists that look exactly
| | 02:52 | the same, in this case.
| | 02:55 | There they are.
| | 02:57 | If we start with a larger set of data,
instead of this hash that we declaring
| | 03:02 | ourselves, with just the three elements -
we will go ahead and use one of the
| | 03:05 | system variables and what this one is
is this is the system environment, all of
| | 03:10 | the environment variables.
| | 03:11 | It's a rather long list.
| | 03:14 | And it's something that you might
to look at every once in a while.
| | 03:16 | We will go ahead and Save this and Run
it, and you will see that we have two
| | 03:21 | copies of this list.
| | 03:22 | The first copy of the list
has all of this stuff in it.
| | 03:27 | You'll see it's in no particular order.
| | 03:28 | The second copy of the list is
actually in alphabetical order, and that's
| | 03:33 | because here we have sort keys.
| | 03:35 | So, we are ordering it by the keys,
and this is the problem with each.
| | 03:40 | Because each returns each element as a
list of key value as an array with two
| | 03:47 | elements, there is no easy way to sort it.
| | 03:50 | So, it's always going to come out in
some random order, some guaranteed to be
| | 03:55 | not in alphabetical order.
| | 03:58 | Obviously, with a short list, it
sometimes may end up being alphabetical,
| | 04:02 | but it's not guaranteed to be in
any particular order, whatsoever.
| | 04:05 | And with foreach, for just a little bit
more of work, not really very much more
| | 04:10 | work, you can sort in
whatever order that you want to.
| | 04:14 | And so that's the advantage of
sort keys instead of using each.
| | 04:19 | That's why you'll see it done this way
more often, when you need to traverse a
| | 04:23 | hash and get the keys and values.
| | Collapse this transcript |
| Understanding the undef value| 00:00 | In our discussion of variables and
values in Perl, we need to talk about a special
| | 00:05 | value called Undef or undefined.
| | 00:09 | It's the undefined value, and
it's represented by the word Undef.
| | 00:13 | So, let's go ahead and make a Copy of
our variables.pl and call it undef.pl and
| | 00:19 | learn about the undef value.
| | 00:21 | So, go ahead here and declare a variable.
And we'll give it the undef value, and
| | 00:31 | then we'll print it out.
| | 00:34 | We need to put my in here, and message,
x is $x. Now this is going to give us an
| | 00:44 | error, and the reason for that is that
we have included an undefined value in a
| | 00:49 | string, and Perl doesn't like that.
| | 00:51 | We'll go ahead and run it, and see the error;
| | 00:54 | Use of uninitialized
value $x in concatenation (.)
| | 00:58 | or string at line 13, and that's
right here, and you'll notice it tried to
| | 01:04 | print it anyway.
| | 01:06 | So, the reason for that is that
$x is the special, undefined value.
| | 01:11 | We simply made in an empty string like that.
| | 01:14 | So, if we simply give it empty string and
Save and Run, and we get no error, and we
| | 01:23 | have x is, and it's still nothingness.
| | 01:26 | So, there's a difference between an
empty string and this special undef value.
| | 01:31 | So, the undef value is the undefined value.
| | 01:35 | If we take x and give it
a value, like say value,
| | 01:41 | and then go ahead and copy these lines,
and take out the my for the second time,
| | 01:49 | because it's already given a scope,
and take this out, and make it undef,
| | 01:56 | now we will get our error again.
| | 01:58 | Our error will be for
line 15 but not for line 13.
| | 02:03 | Because what we've done here is we've
taken $x, and we've made it undefined
| | 02:07 | again. We've uninitialized it.
| | 02:08 | So, go ahead and Run it, and there we
have that error message, and this time,
| | 02:13 | it's for line 15, which is right there.
And the first time we Run this it says
| | 02:18 | x is value.
| | 02:19 | That's x string value.
| | 02:20 | And the second time we run it,
it is that undefined thing.
| | 02:26 | Where else is this useful,
besides for creating errors?
| | 02:28 | I'm glad you asked.
| | 02:30 | Let's go ahead and create a function,
a subroutine, and we'll say, sub isnum,
| | 02:37 | and so this is going to be a function
that's going to tell us whether or not
| | 02:41 | a value is numeric.
| | 02:43 | And it's simple to do, $n=shift,
and that will give us the argument.
| | 02:49 | And we're going to test for the
undefined value with the defined function, and
| | 02:58 | in this case, were checking to see if
the variable that's been passed to us
| | 03:03 | through shift is defined.
| | 03:05 | And we'll talk about subroutines and
functions and how they work later on.
| | 03:09 | But for now, I'll just explain as I go along.
| | 03:12 | We're going to test if this number is
numeric, using a regular expression.
| | 03:21 | So, it's testing that it has any
characters that are not a zero through nine
| | 03:25 | character, and if it does,
then we will return the undef value.
| | 03:34 | We don't want to return zero or empty
string, which would be considered false
| | 03:38 | in Perl.
| | 03:39 | We want to return this undefined value
so that we can return the value, otherwise
| | 03:45 | actually return the value of the number.
And that number may be a zero, which
| | 03:50 | would be understood by Perl as being false.
| | 03:53 | And so we can't just test and
say is it true or is it false?
| | 04:01 | We have to use the defined
function to test what is num return.
| | 04:05 | So, we can say if(defined isnum $x,)) and
we know that it is a number, and we can
| | 04:15 | say, message("x is a number")
and we'll even show the number.
| | 04:23 | Else message x is most definitely not a number.
| | 04:30 | And so we'll Save this, and Run it and
we see, in this case, x is not a number
| | 04:40 | because we have the word value in there.
| | 04:41 | If I put a number in there, and Save
that and Run it, then it says x is a
| | 04:47 | number, and it gives us the number 42.
| | 04:48 | If I put a zero in there, zero is
considered false, and so here we actually
| | 04:56 | get it.
| | 04:57 | But if I had not used this defined
function, if I just said, if, is numx like
| | 05:02 | that, then it would say that it's not
a number, and it actually is a number.
| | 05:07 | And so that's why we use the undef
value for not a number, instead of just
| | 05:14 | returning a zero or false is a number.
| | 05:19 | So, that's the usefulness of undef,
and, of course, you can also use it to
| | 05:25 | uninitialize variables that have
previously been declared and defined and
| | 05:30 | given a value.
| | 05:31 | We saw that.
| | 05:31 | And it's useful to know that when
you get that error message of using
| | 05:37 | uninitialized variable in a string or
concatenation, that the undef value is
| | 05:42 | what's going on there.
| | Collapse this transcript |
|
|
4. Statements and BlocksAssigning values to variables| 00:00 | We're going to start our discussion
of statements in Perl by looking at the
| | 00:04 | assignment statement, and we'll start
by taking a Copy of our statements.pl
| | 00:10 | file, and we'll name this assignments.pl.
| | 00:16 | We'll go ahead and open it up, and
we'll just come in here and we'll make an
| | 00:23 | assignment. And we'll change this
message to print out the variable, and we'll
| | 00:33 | go ahead and Save that and Run it.
| | 00:36 | Now this is, if you've been going in
order you have seen this in different
| | 00:41 | chapters and in different contexts,
| | 00:43 | you've seen exactly this construction
where we assign a value to a variable.
| | 00:48 | What this is, this is a statement.
| | 00:50 | A statement is a set of instructions to
the Perl language, and it's terminated by
| | 00:56 | a semicolon. And in this case, the
statement is an assignment where it assigns
| | 01:01 | this value, 42, to the variable $var, using the
assignment operator, which is the equal sign.
| | 01:10 | And then we are able to print out
the value and we see the result.
| | 01:14 | We can, of course, assign
different types of things.
| | 01:16 | We can assign a string instead, and
we can Save that and Run it, and there
| | 01:23 | we have that result.
| | 01:24 | Of course, we can assign
different kinds of things.
| | 01:27 | We can assign a list to an array, and
we want to put that list in parentheses.
| | 01:40 | And here we have assigned three
different things: the string 42, the number, the
| | 01:45 | literal number 42, and the
results of an expression 40+2.
| | 01:51 | Alter that list, and now what we want
to do is we want to print that as a join,
| | 01:55 | because that's how we print arrays,
and we'll Save that and Run it.
| | 02:06 | And there we have our list.
| | 02:08 | So, this is, in a nutshell, how assignments work.
| | 02:11 | They use the assignment operator, and
when we get into the operator section,
| | 02:17 | we'll see some different variations on this.
| | 02:19 | But this is the basic structure
of an assignment statement in Perl.
| | Collapse this transcript |
| Using statements and expressions| 00:00 | So, let's talk about Expressions.
| | 00:02 | To get started, we'll take our statements.pl,
and we'll go ahead, and we'll make
| | 00:06 | a copy of that, and we'll call
it expressions.pl, and open that.
| | 00:13 | And we'll go ahead, and we'll start by
making an expression, so we'll say
| | 00:15 | my$x = 42;, and we'll print that value
out here in this message function. We'll just
| | 00:24 | say $x, and we'll Save that and Run it,
and you'll see we get a 42 down here.
| | 00:32 | So, in this case, we can see that 42
is an expression that has a value of 42,
| | 00:37 | and it's getting assigned to this variable,
and that variable is getting printed out here.
| | 00:40 | And that all seems very obvious.
| | 00:42 | What's not obvious here is that this
entire statement is an expression, and in
| | 00:47 | fact, this entire statement is an
expression, and in fact, every statement in
| | 00:53 | Perl is an expression, and there
are expressions within expressions.
| | 00:58 | Everything is an expression.
| | 01:01 | So, if we do, instead of this 42, if we were
to do something like 3+7 * 10; and Save that,
| | 01:08 | now what do we expect to get printed out here?
| | 01:12 | Well, this would depend really on
whether the addition happens first or the
| | 01:17 | multiplication happens first.
| | 01:19 | So, if the 3+7 happens first, 3+
7 is 10, and 10 times 10 is 100.
| | 01:24 | We would expect 100 to be the result.
| | 01:27 | If the 7 times 10 is evaluated first,
then that's 70, and the 3 then gets
| | 01:33 | added to 70 for 73.
| | 01:36 | So, our result would really depend on that.
| | 01:39 | I happen to know the answer.
| | 01:40 | I want to go ahead and Run this, and
we can see that 73 is the answer, so the
| | 01:45 | multiplication is happening before the addition.
| | 01:48 | That's not what we intended to have happen.
| | 01:50 | We can use parentheses to force the
expression to get evaluated in a certain order.
| | 01:56 | And now the 3+7 will be evaluated
first, and our result will be 100.
| | 02:02 | So, we'll go ahead and Run
this, and there's our result.
| | 02:06 | What controls this is something called
operator precedence, and that's covered
| | 02:10 | later in the course. And there are
Charts and Tables, and you'll really get to
| | 02:15 | understand that in some detail if
you want to, but the important thing to
| | 02:19 | remember - and I don't have the
operator precedence chart committed to memory,
| | 02:23 | and I don't think very many
people actually bother with that -
| | 02:26 | the important thing to remember is if
you need for things to be evaluated in a
| | 02:29 | particular order, then use the parentheses.
| | 02:32 | Otherwise, if you have it like this,
then whoever comes along to read it later,
| | 02:39 | you may know, and you may actually mean
for this multiplication to happen first.
| | 02:44 | But whoever comes along to
read it later may not know that.
| | 02:47 | And in fact, you may not
remember that a year from now.
| | 02:50 | And you're going to come,
and you're going to look at it.
| | 02:52 | And you're going to go, um-huh, and you
are going to have to pull out the table,
| | 02:54 | and look at it and figure it out.
| | 02:56 | It's so much easier to just use the
parentheses, even if the parentheses are the
| | 03:00 | default order anyway.
| | 03:02 | So, let's just take a look at one
other aspect of expressions in Perl.
| | 03:08 | Because everything is an
expression, you can do things like this.
| | 03:14 | And so if this expression evaluates to
false, which in Perl means it would be a
| | 03:21 | zero or an empty string, then
this message will be displayed.
| | 03:26 | But since this does not evaluate to
false, this expression here never gets
| | 03:31 | evaluated, and so this
message will never get displayed.
| | 03:34 | We'll go ahead and Save that and Run it, and
you can see that we just get the message, 73.
| | 03:39 | We do not get the message, foo.
| | 03:42 | If on the other hand, this were a
zero and we Save that and Run it,
| | 03:49 | now we get the message foo, and we get
the message 1, because message happens to
| | 03:56 | return true. It happens to return a 1
value, and so that's getting assigned to
| | 04:01 | the x, and so we're
printing out both of those values.
| | 04:05 | This is an important feature, and you
will see this kind of thing done all the
| | 04:10 | time in Perl code that you
run across and that you read.
| | 04:13 | So, it's important to
understand a couple of things.
| | 04:16 | One, true and false - all expressions
can be evaluated as either true or false,
| | 04:22 | zero is false, empty string is false,
and the undefined value are all considered
| | 04:27 | false. Anything else is true.
| | 04:30 | And we'll cover all of the
different types of operators later.
| | 04:33 | You can use logical operators to
actually decide what code gets executed, and
| | 04:38 | you can even do side effect
things, like printing out a message.
| | 04:42 | I don't recommend that you do that in code
that anybody else is ever going to have to read.
| | 04:46 | But you need to understand that it
works that way, because you will see code
| | 04:50 | like that and occasionally, something
like this might come in handy for you.
| | 04:54 | So, that is Expressions in a nutshell.
Just to remember that all statements in
| | 04:59 | Perl are considered expressions.
| | 05:01 | They get evaluated in a particular order.
| | 05:03 | You can control that order with
parentheses, and you can even use that order to
| | 05:09 | selectively execute code.
| | Collapse this transcript |
| Combining statements into blocks| 00:00 | Blocks of code, in Perl, are enclosed in
curly braces, and they consist of one
| | 00:07 | or more statements.
| | 00:08 | So, let's take a look at how that works.
| | 00:11 | We'll go ahead and copy the
statements.pl, and call it blocks.pl, so we don't
| | 00:17 | overwrite the statements.pl.
| | 00:19 | You'll find all of these in the 04
Statements folder, inside the Exercise Files folder.
| | 00:23 | We'll go ahead, and we'll open that up.
| | 00:27 | We'll take a look. Well, here we have a block
of code, and here we have a block of code.
| | 00:33 | So, anything inside of curly
braces is considered a block of code.
| | 00:39 | If we wanted to, we could
create our own little blocks here.
| | 00:42 | We can say something like if (1) and
message ("this"); and message ("that"); or else{.
| | 01:00 | We can put this one here
in the else, if we want to.
| | 01:04 | Go ahead and Save that.
| | 01:06 | Of course, because we have this
conditional (1) - and we'll talk
| | 01:08 | about conditionals later -
| | 01:10 | only this block here was the only one that
will be executed, and this block will not.
| | 01:17 | So, we'll go ahead and Run this, and
we'll see that this and that prints, and
| | 01:22 | the other one doesn't.
| | 01:22 | If we make this condition false (0),
then we'll see that this other one runs.
| | 01:30 | So, in a nutshell, that's what a block
of code does is it allows you to treat
| | 01:35 | several lines of code as a unit.
| | 01:38 | We will be using blocks a lot
throughout the rest of this course.
| | 01:43 | We'll be talking about them in our
lesson on Scope, which is coming up, and all
| | 01:47 | the conditionals and the
loops that are coming up.
| | 01:50 | So, it's good to understand.
| | 01:52 | It's a very simple concept.
| | 01:53 | Anything inside of these curly
braces is considered a block, and it has
| | 01:59 | more than one statement.
| | 02:00 | If you remember, the last
semicolon in a blog is optional.
| | 02:05 | I strongly recommend that you use it,
but you'll see that the code runs just
| | 02:09 | fine without it there.
| | 02:11 | Let's put a (1) in here,
so that code actually executes.
| | 02:16 | We'll Run that so we can see this and that.
| | 02:19 | I strongly recommend that you include
the semi olon, even though it's optional,
| | 02:24 | because when you have more than one
line of code inside of a block, you might
| | 02:29 | find that you want to add another one.
| | 02:33 | If you had neglected the semicolon on
the line before you might end up with an
| | 02:37 | error, if you forget to put it in later.
| | 02:40 | So, that's what a block is.
| | 02:42 | Coming up, we are going to talk
about Scope, and that has a lot to do
| | 02:45 | with blocks, as well.
| | 02:46 | So, if you are interested in
understanding blocks thoroughly, then be sure to
| | 02:51 | also watch the movie on Scope.
| | Collapse this transcript |
| Understanding scope| 00:00 | Scope, in Perl, is the concept of
where things are visible from,
| | 00:06 | and this is best illustrated by looking at it.
| | 00:09 | So, let's go ahead and make a copy of
the statements.pl file. We'll call it
| | 00:14 | scope.pl and go ahead and open that.
| | 00:19 | So, here we see that we have some blocks.
| | 00:22 | We have this block here.
| | 00:24 | We have this block here.
| | 00:27 | You'll notice that in some of these
blocks there are variables declared.
| | 00:30 | So, here is a my $e, and that's inside that
error block and my $m inside the message block.
| | 00:39 | If we were to try to access
m from inside of the error block,
| | 00:44 | if I were to just put an m here,
you'll notice that I get an error.
| | 00:48 | The error says, "Global symbol
"$m" requires explicit package name."
| | 00:54 | That is one very creative error
message, and you'll see that a lot.
| | 00:58 | What that means is that you are
trying to access a variable that does not
| | 01:02 | exist in this scope.
| | 01:03 | Of course, if they were to just
say that, it would be too easy.
| | 01:07 | If I Save this and Run it, which will
then give me a run time error version of
| | 01:13 | that, it says it's got errors.
| | 01:15 | Go ahead and proceed.
| | 01:16 | Global symbol $m requires explicit
package name, and it gives the line 24.
| | 01:24 | This is just how the interpreter
interprets this, that you are trying to access
| | 01:29 | a global symbol, because it
doesn't have a my declaration.
| | 01:32 | So, it considers this
trying to access a global symbol.
| | 01:35 | What that's means is if it were
declared in a global scope, if I were to put a
| | 01:39 | $m here, "This is variable \$m";,
| | 01:44 | now we don't have an error anymore.
| | 01:50 | I Save. It forces it to.
| | 01:54 | Now we don't have the little red x.
| | 01:55 | If we go ahead and Run,
| | 01:58 | we'll see that everything works.
| | 02:01 | If I call error from up here error
("message"), and I'll just say ("this is
| | 02:09 | an error message").
| | 02:11 | So, because we are using $m dollar
down here instead of $e, it will actually
| | 02:19 | print out this message from the global scope.
| | 02:23 | Let's Save that and Run it.
| | 02:28 | Here is 'this is variable $m.' That's
this one up here in the global scope,
| | 02:34 | because we used the global $m down here.
| | 02:38 | This is interesting.
We actually have a $m in message,
| | 02:43 | but that's in its own little local scope.
| | 02:45 | Because it's declared with my inside of
this block, that only exists in there.
| | 02:52 | When I go to access $m in this
other scope, it's using the global one,
| | 02:58 | because it doesn't have one declared
here, and there is one declared globally.
| | 03:03 | If instead, I were to call this $m
instead, now we have a local $m. This $m
| | 03:13 | will now be this $m using its
local scope, because we have that verbal
| | 03:19 | declared in local scope.
| | 03:21 | Now, this error message will
do what we expect it to do.
| | 03:24 | It will say this is an error message,
because that's what's getting passed to it.
| | 03:28 | That's what's getting read in through
the shift here, into this local version of
| | 03:33 | $m. So, we'll Save that and Run it.
| | 03:37 | There is our error message.
This says, "this is an error message."
| | 03:40 | That's the one that we passed with.
| | 03:42 | That's what we expect to have happen.
| | 03:44 | So, normally, I just have $e down here,
and $e over here, so we are using the
| | 03:51 | local variable, and $m is used for message.
| | 03:56 | There is no global in here, and
that does what we expect it to do.
| | 04:03 | So, the message is the template.pl
file. That gets passed to message.
| | 04:10 | It gets put in this $m, which is local to
this block, and it gets printed out there.
| | 04:16 | The error message, this is an error message
that gets passed here into the error function.
| | 04:23 | It gets read into this local
version of $e, locally scoped.
| | 04:30 | It gets printed out like this.
| | 04:32 | So, scope refers to where a
variable is accessible from.
| | 04:38 | Locally scoped variables exist in
the local scope in which they are
| | 04:42 | declared, usually in a block.
| | 04:45 | Globally scoped variables are outside
of any block, and those are accessible
| | 04:51 | from all of the enclosing blocks.
| | 04:54 | In fact, if I were to have another block
inside of this block here - and we'll see
| | 05:00 | examples of this later on when
we do loops and conditionals -
| | 05:04 | then the variables in this scope will
still be accessible from those inner
| | 05:09 | blocks, unless they are overwritten by
having a variable of the same name declared locally
| | 05:15 | inside of those blocks.
| | 05:17 | So, that's how scope works.
| | 05:20 | Scope is about where variables are
accessible from and where things are
| | 05:25 | visible from, and they tend to be
visible within the blocks and enclosing
| | 05:31 | blocks where they are declared.
| | Collapse this transcript |
|
|
5. Conditional StatementsUnderstanding conditional statements| 00:00 | Before we look at Conditional
statements in detail, let's look generally at
| | 00:04 | conditional statements in Perl.
| | 00:06 | Conditionals execute code selectively.
| | 00:09 | For example, in this statement, if
the condition is found to be true, the
| | 00:14 | statement will be run.
| | 00:16 | If the condition is not found to be
true, the statement will not be run.
| | 00:21 | Conditionals may also provide alternative code,
| | 00:23 | as in this example. Statement 1 will
be run if the condition is found to be
| | 00:28 | true, and statement 2 will be run if
the condition is not found to be true.
| | 00:35 | Statement 1 and statement 2 will never both run.
| | 00:38 | It will always be one or the other,
depending on the status of the condition.
| | 00:43 | There are, of course, more complex
examples, and we'll get into that as we get
| | 00:47 | into the details of
conditionals in Perl, in this chapter.
| | 00:51 | One note about Perl 5.10: Perl 5.10 is as of
this writing, the very latest version of Perl.
| | 00:58 | It has a very few feature that are
incompatible with earlier versions.
| | 01:02 | So, they are covered separately in a
Perl 5.10 New Features chapter,
| | 01:08 | later in this course.
| | 01:10 | Beginning in Perl 5.10, Perl
also provides a switch statement.
| | 01:14 | Switch statement uses given and when,
and it is only available in version 5.10
| | 01:20 | or later versions of Perl.
| | 01:23 | So, it's covered separately in the
separate chapter on these new features, and
| | 01:27 | is not covered in the body of this course.
| | 01:30 | The reason for that, of course, is that it's
incompatible with earlier versions, and
| | 01:33 | if you use it, and you try to run your
code on a system that doesn't have Perl
| | 01:38 | 5.10, which is most deployed systems at
this point, then it will not work and it
| | 01:44 | will break the code.
| | 01:46 | So, this feature, along with other
5.10 features, is covered separately in
| | 01:50 | the Chapter on 5.10.
| | 01:52 | So, that's an overview of Conditionals in Perl.
| | 01:54 | Now, let's get into the details.
| | Collapse this transcript |
| Creating conditionals with if| 00:00 | The first of the conditionals that
we'll talk about is the if conditional, and
| | 00:07 | we'll start by making a copy of the
conditionals.pl file, and we'll call it
| | 00:11 | if.pl, so that we can experiment without
overwriting the Exercise File. And I am
| | 00:22 | just going to show you how if works.
| | 00:24 | You've seen it before, but we're
going to talk in detail about it here.
| | 00:27 | So, we're just going to say if (1),
and message ("this is true"), and so what
| | 00:38 | this does, this is if.
| | 00:39 | If is a keyword in Perl, and
its purpose is to select code for
| | 00:45 | conditional evaluation.
| | 00:48 | And the argument to if always goes in
parentheses, and if the argument evaluates
| | 00:52 | to true, then the code inside the block
will evaluate, and in this case, it is
| | 00:59 | message (this is true).
| | 01:01 | Since message is the only statement in
the block, the semicolon is optional, but
| | 01:04 | I like to put it there when I
remember to. Sometimes I do forget.
| | 01:08 | And if we save this and run it,
we'll see that the message displays.
| | 01:16 | If I put a 0 here instead of a 1 and
save it and run it, we'll see that the
| | 01:23 | message does not display.
| | 01:26 | So, we know that 0 is false and 1 is true.
| | 01:30 | In actuality, anything besides a 0, or an
empty string, or undef is considered true.
| | 01:38 | So, if I put in here 142 and Save that
and Run it, we'll see that this is true.
| | 01:47 | If I put in here a string and say
"this is a string" and Save it and Run it,
| | 01:56 | it's still considered true.
| | 01:58 | If I put in an empty string - that is a
string with nothing in it, and Save that
| | 02:04 | and Run it - we do not get the message,
because that is considered false, or if I
| | 02:10 | put in the special undef value, that
is also considered false. Then Run that.
| | 02:17 | Any expression, at all, can go here.
| | 02:19 | It can be the result of a function call.
| | 02:22 | It can be any expression.
| | 02:24 | It could be a mathematical expression,
5+5, and that will evaluate to true.
| | 02:31 | If I say, 5-5, of course,
that will evaluate to false.
| | 02:37 | And so it can be anything that resolves
to a value, and any value that is 0, or
| | 02:44 | an empty string, or the
undef value will be false.
| | 02:49 | Anything else, whatsoever,
will be considered true.
| | 02:53 | And so that is how if works.
| | 02:55 | I just want to cover one
other thing that I find useful.
| | 03:00 | This is certainly not required, and
most Perl programmers do not bother to do this,
| | 03:05 | but I find it useful, is to use the constant
module to define constants for true and false.
| | 03:12 | These can be useful for return values,
or for anything where you want to just
| | 03:17 | have something that not only evaluates
to true or false, but actuality looks
| | 03:22 | like true and false to the
person reading the codes.
| | 03:24 | So, I'm going to say use constant and
this allows you to define constants in Perl.
| | 03:30 | Constants are special values that
cannot be modified and will always be
| | 03:36 | guaranteed to be what you define them to be.
| | 03:38 | So, I'm going to make 1 for
True, and make one for False.
| | 03:41 | And the format of the
constants here is just like a hash.
| | 03:48 | And so you can define however
many things you want to here.
| | 03:52 | And then if I put True in this, the
syntax is just like that, it doesn't need
| | 03:57 | quotes, or anything like that.
| | 03:59 | I need a semicolon here, and then we
can Save and Run, and we'll see that
| | 04:07 | that evaluates, of course, true. And I
can put False in here, and Save that and
| | 04:12 | Run it, and that will evaluate to false.
| | 04:17 | Using the constants, I find it useful.
| | 04:19 | I'll use them for return values from
functions, and things like that where I
| | 04:23 | want something to
actually say true, and say false.
| | 04:26 | Perl is lacking keywords for this. Many
languages have them. Many languages don't.
| | 04:32 | In languages that don't, I'll tend to
define constants, or macros, or whatever
| | 04:37 | facility that language has.
| | 04:38 | I like to have the word True and the
word False in some circumstances, and so
| | 04:43 | this is a convenient way to get that.
| | 04:45 | So, again, this is the if statement.
| | 04:47 | This is the if conditional, and it
selectively evaluates code based on a
| | 04:54 | condition that can evaluate to true or false.
| | 04:56 | 0, the empty string, and the undef
value are considered false. Anything else
| | 05:01 | is considered true.
| | Collapse this transcript |
| Extending conditionals with else| 00:00 | Continuing our discussion of conditionals
in Perl, we're going to talk about else.
| | 00:05 | Else works with If.
| | 00:07 | Just make a copy of conditionals.pl.
| | 00:10 | We call it else.pl. And just as a
starting place here, we'll go ahead and define
| | 00:17 | a pair of constants.
| | 00:24 | These are just for convenience.
| | 00:30 | I like to do this when I am writing code,
and we'll come in here, and we'll say
| | 00:38 | if (TRUE), message ("this is true");
| | 00:47 | and put in an else.
| | 00:48 | Now what the else does is it executes
in the case that the if clause is False.
| | 00:59 | So, in this case, we know that it's
true because we are using our constant for
| | 01:03 | TRUE, and we'll get the TRUE part here.
| | 01:06 | This is true and if I change this
to FALSE, we'll get the False part.
| | 01:11 | Run and this is false.
| | 01:16 | What else does is it allows you to have
a block of code that will execute in the
| | 01:22 | event that the condition for the if
statement is found to be FALSE. And so the
| | 01:28 | TRUE part is this first block, and
that's from there to there, and the FALSE
| | 01:33 | part is in the else block.
| | 01:35 | Now notice that the formatting here,
where I put the else on the same line
| | 01:40 | as these curly braces,
| | 01:41 | this is the matter of style.
| | 01:42 | It doesn't have to be there.
Not everybody likes to do it that way. I like it.
| | 01:46 | It's concise and it's clear. But you
don't have to do it that way, as you know
| | 01:50 | whitespace is ignored in Perl, so
you can format this however you like.
| | 01:54 | The important thing to know here is
that this allows you to have an alternate
| | 02:00 | execution path for your conditional in
the event that your expression is False.
| | 02:06 | So, that is how Else works.
| | Collapse this transcript |
| Extending conditionals with elsif| 00:00 | In addition to If and Else, there
is one more conditional that we have.
| | 00:05 | It's called elsif and it's really
incredibly useful, and you'll be glad that we
| | 00:10 | have it when you learn what it does.
| | 00:11 | So, let's go ahead and make a copy of
conditionals.pl and call it elsif.pl, and
| | 00:17 | elsif is spelled like that without the
second e. It's e-l-s-i-f-.-p-l, and we'll go
| | 00:26 | ahead and open that.
| | 00:27 | We are going to forgo the constant that
we've done in some of the other lessons,
| | 00:32 | because I think you understand that at
this point, and we'll just go ahead and
| | 00:36 | say if(1), and we know that one is true.
| | 00:39 | So, we'll say message ("this is true")
and else message ("this is not true"),
| | 00:49 | and we'll go ahead and Save that and Run that.
| | 00:51 | And you'll see that, of course, the one
is true and if we were to put a zero in
| | 00:56 | here and Save and Run that, that is not true.
| | 01:01 | Now what if you wanted to have
another conditional in there?
| | 01:05 | What if you wanted a third choice?
| | 01:09 | You can say elsif(1) and
message, ("but this one is true!").
| | 01:24 | And when we Run that, that's what we get.
| | 01:26 | We get that third one and so these are
all mutually exclusive, which means that
| | 01:31 | you're only going to get one of
these. And in fact, you could have as many
| | 01:36 | elsif's as you want to.
| | 01:37 | I am just copy this and paste it.
| | 01:40 | It will always select the first one
that's true, and it won't bother to run
| | 01:45 | anything else. So, if this one were
false - now let's make one of these
| | 01:50 | different than the others.
| | 01:57 | And so we'll go ahead and make this one
false, and now you're going to get the
| | 02:02 | second one, which doesn't look
any different, but this one is true.
| | 02:07 | But if you make this one false,
now you'll get this one here.
| | 02:12 | This one is different than the others.
| | 02:14 | So, this will always select the first
one that's true and, of course, if none of
| | 02:19 | them turn out to be true than you get
the else at the bottom: this is not true.
| | 02:26 | So, elsif is great for selecting among a
number of different conditions. And just
| | 02:33 | to show you a working example of this,
| | 02:35 | go in your Exercise Files folder to
a folder called RandomQuote and in there
| | 02:40 | we have a script, and this
script is called RandomQuote.
| | 02:44 | We'll open that, and let's just Run that
real quick so you can see what it does.
| | 02:50 | What it does is it gives you a
RandomQuote out of a quote file, and this one is
| | 02:56 | Sid Ceaser, and the quote file is here,
quotes.txt, and you can see that in here
| | 03:03 | is a bunch of quotes from different people.
| | 03:07 | Some of them are silly. Some of them
are meaningful. And the way that this
| | 03:12 | works is, in a nutshell,
| | 03:15 | it reads through this quotes file, and
every time it sees a token surrounded in
| | 03:22 | dollar signs like that,
| | 03:24 | it looks at what that token is and it
decides what to do based on that token.
| | 03:27 | If the token says quote, then it starts a
new structure to store a whole quote in byline.
| | 03:34 | If the tokens says byline, it
stores the byline in that structure.
| | 03:38 | If it is just the text, it'll store the
text in the structure based on the token
| | 03:43 | that is read before.
| | 03:45 | So, it will read a token.
| | 03:47 | If it's a quote token, it'll
start a new structure. It reads a line.
| | 03:51 | It puts it into the space for that token.
| | 03:54 | So, it's either the quote or the byline
and it does all of this with a if, elsif.
| | 04:02 | So, there's a loop here, this while loop
for getting the line of text, and we'll
| | 04:07 | cover loops in another chapter.
| | 04:10 | But here is the if, elsif and
else, and so it's getting the token.
| | 04:15 | It's checking it as a token and it's
doing its default, which in this case is to
| | 04:20 | give an error message if there wasn't a
token, but it's got some blank text line
| | 04:23 | and it doesn't know where to put it.
| | 04:26 | So, that's an example of if,
elsif and else in a real world script.
| | 04:31 | We'll go ahead and Run it
again, get a different quote.
| | 04:33 | Everytime we Run it, we get a different quote.
| | 04:35 | Now we'll look at the script
throughout the course, and we'll be looking at
| | 04:40 | different aspects of it as we learn
different parts of the Perl language.
| | 04:43 | So, don't expect to understand
the whole thing at this point.
| | 04:46 | I am just showing you that this is
the sort of the thing you can use if,
| | 04:50 | elsif and else for.
| | Collapse this transcript |
| Building if statements with postfix conditionals| 00:00 | One of my favorite features of the Perl
language is the ability to use control
| | 00:04 | structures in a Postfix notation, and
this is best illustrated by showing you.
| | 00:10 | You may not completely understand what I
mean from just explaining it like that.
| | 00:15 | So, we'll go ahead and make a copy of
conditionals.pl, and we'll call this one
| | 00:19 | postfix.pl. That's post,
p-o-s-t-f-i-x, postfix and open that up.
| | 00:26 | We'll start out by just
writing a little if statement, and we'll
| | 00:33 | declare a variable, and if ($x > 25 and
$x < 100), we'll just display a message,
| | 00:47 | ("number is in range!").
| | 00:50 | We'll go ahead and put in the semicolon first.
| | 00:54 | We'll Save that and Run it. And because
the number is greater than 25 and it is
| | 01:01 | less than 100, number is in range.
| | 01:04 | This is perfectly reasonable.
| | 01:06 | You can write it like this.
| | 01:08 | Everybody will know exactly what you
mean, but there is another way to do it.
| | 01:13 | And this other way to do it, I think,
is pretty cool, and it looks like that.
| | 01:20 | What we've done here is we have taken
the conditional, and we've put it at the
| | 01:26 | end of the line, and we've put the
actual action that is taking place at the
| | 01:31 | beginning of a line where I think it's
more obvious, and so this is more
| | 01:36 | succinct and in my mind, it's more readable.
| | 01:40 | Its do this, if that.
| | 01:42 | It's kind of the way we speak sometimes too.
| | 01:45 | I don't know of any other languages, at
least not any other modern languages in
| | 01:50 | wide deployment, that have
this postfix notation available.
| | 01:54 | I think it's really, really cool.
| | 01:56 | We Save this and Run it, and you'll see that
it works exactly the same as the other way.
| | 02:01 | This is most useful if
your action is one statement.
| | 02:05 | If it needs to be more than one
statement you can need to wrap it in a
| | 02:08 | block, and you need to put a do before that
block and it just loses its elegance entirely.
| | 02:13 | You can use pretty much any control
structure in Perl, and as we go through the
| | 02:17 | rest of the course, when we learn some of
the other control structures, even the
| | 02:20 | loops, you'll see that this is a
useful notation in those circumstances.
| | 02:26 | And another thing is that these
parentheses are not necessary anymore, and
| | 02:31 | they are often omitted.
| | 02:33 | So, we can save this like this and Run
it again, and indeed, the parentheses are
| | 02:39 | required in the more normal prefix notation
| | 02:43 | that we learned before. But in this
case, with the postfix notation, those
| | 02:47 | parentheses were not required and you
often see them omitted just like this.
| | 02:52 | So, that is the postfix notation.
| | 02:54 | I think it's very elegant and very useful.
| | 02:57 | It allows you to put the action at
the beginning of the block, and it's most
| | 03:01 | useful where your action is one
statement and not more complicated than that.
| | Collapse this transcript |
| Creating negative conditionals with unless| 00:00 | Another nice feature that Perl has that
a lot of other languages are lacking is
| | 00:05 | an Unless conditional.
| | 00:07 | Unless is the opposite of If, and
it works just exactly like that.
| | 00:13 | So, we'll make a copy of conditionals.pl,
and we'll call it unless.pl, and
| | 00:18 | we'll open that up.
| | 00:20 | And we'll go ahead and declare a
variable. And we'll say, unless ( $x > 25 and
| | 00:35 | $x < 100 ), message ("number is out of
range"), and we'll Save that and Run it.
| | 00:50 | And, of course, we don't get a
message, because the number is in range.
| | 00:54 | And if we make this 24 instead of 42,
we'll see that we get number is out of range.
| | 01:02 | So, this is exactly the same as If,
only the condition is reversed.
| | 01:07 | Unless will only run this statement
if the condition is false, and if the
| | 01:12 | condition is true, it will not.
| | 01:15 | You'll most often see
Unless in the postfix notation.
| | 01:19 | So, if I take this and paste it in
there, and we can go ahead and get rid of
| | 01:27 | the parentheses, so we don't need
those anymore in the postfix way.
| | 01:32 | You'll often see this like this: number
is out of range unless this condition.
| | 01:39 | We'll go ahead and Save that and Run it,
and you see that that works exactly the same.
| | 01:44 | I'm going to show you a couple of examples.
| | 01:46 | If you look in the RandomQuote folder
again at the RandomQuote.pl, you'll see
| | 01:51 | that this is a very, very common
structure when initializing variables in a
| | 01:57 | function or at the beginning of program.
| | 01:58 | I'm going to initialize this file name
as quotes.txt, unless one has already
| | 02:03 | been provided, unless we
already have the file name.
| | 02:06 | We can see it in some other places down here.
| | 02:09 | Here is a little error message,
("No quotes") unless $count.
| | 02:15 | So, we're trying to display a random
quote, but we find that we don't have any,
| | 02:20 | so we just display a message
that says we don't have any.
| | 02:23 | It's a very common structure, and this is
the way that you'll most often see unless.
| | 02:28 | Doing it with the braces {}, in
the structure of a normal If condition is
| | 02:33 | not as common, because it looks a
little bit backwards to most people, and most
| | 02:37 | people don't think that way.
| | 02:38 | But this is the way that you'll most
commonly see it, in the postfix notation for
| | 02:44 | just doing something
unless it's already been done.
| | Collapse this transcript |
| Using the ternary conditional operator| 00:00 | One final conditional that we
want to talk about in our chapter on
| | 00:04 | conditionals is the
ternary conditional operator.
| | 00:08 | That is also a very long
and cumbersome name for it.
| | 00:14 | So, let's go ahead and make a copy of
conditionals.pl, and we'll name it ternary.pl.
| | 00:22 | This one is called a ternary
operator because it is an operator that has
| | 00:27 | three expressions to it.
| | 00:30 | So, we'll start by declaring a
variable, and we'll call this n, and we'll
| | 00:36 | give it a value of 24.
| | 00:39 | We'll next declare another variable, and
this one going to have some text in it.
| | 00:43 | So, we'll call it $s, for string.
| | 00:45 | I am going to use the
ternary conditional operator here.
| | 00:49 | I'm going to say ($n > 25 and $n < 100),
and we'll put in a question mark.
| | 01:03 | We'll say that it's in range if the
condition is true and out of range if
| | 01:09 | the condition is false.
| | 01:12 | Then we can display this in the message.
| | 01:14 | It looks like I made a typo there.
| | 01:16 | It says it has syntax errors. Oh!
| | 01:19 | missing the equal sign. There we go.
| | 01:22 | See, even the teacher
makes mistakes now and then.
| | 01:24 | The message number is and concatenate
that with this string and $s, like that.
| | 01:34 | Now, what we've done here is we have
declared a number, and the number is 24.
| | 01:38 | So, the number is out of range.
| | 01:40 | It is not greater than 25.
| | 01:44 | This is the ternary operator here.
| | 01:47 | So, it has a condition followed by a
question mark, and then it has 2 values.
| | 01:53 | Now, the first value will be returned
if the condition is true, and the second
| | 01:59 | volume will be returned
if the condition is false.
| | 02:02 | Then that will, of course, get
assigned because this has an assignment
| | 02:06 | expression here to $s. So, if $n
satisfies the condition, in other words, if it's
| | 02:14 | in range, then this string will be
assigned to $s. If the condition is not met,
| | 02:21 | if $n is outside of this range,
then this condition will be met.
| | 02:25 | So, $n is outside of the range, so what we
expect is for to say number is out of range.
| | 02:32 | So, we'll Save, and we'll
Run and number is out of range.
| | 02:37 | If we change the number and put it in range,
Save and Run, this says number is in range.
| | 02:46 | So, that's the ternary conditional operator.
| | 02:51 | It's called an operator because
it's using an expression like this.
| | 02:55 | It is not a built-in
function and it has three operands:
| | 02:59 | it has the condition and the expression
for true and the expression for false.
| | 03:06 | So, that's why it's called
ternary, as opposed to unary or binary.
| | 03:11 | That's how it works.
| | 03:12 | Now, this is the carry over from the C language.
| | 03:14 | It looks a little bit complicated. When you are
reading the code and you run up against this,
| | 03:20 | you always have to stop and say, "Wait a minute.
| | 03:22 | It's a ternary operator."
| | 03:25 | Remember how it works.
| | 03:26 | So, people don't tend to use it very often.
| | 03:29 | More often than not, you'll see
people use straight conditionals:
| | 03:33 | if, else, unless, things like that.
| | 03:36 | But now and then, you run into it,
and now and then, it's just the elegant
| | 03:39 | way to do something.
| | 03:40 | So, it's a good thing to know, and
that's the ternary conditional operator.
| | Collapse this transcript |
|
|
6. LoopsUnderstanding loops| 00:00 | Let's take a look at loops and
how they are implemented in Perl.
| | 00:04 | Loops are used for repeating code. For
example, in this while loop, as long as
| | 00:10 | the condition remains true, statement1 and
statement2 will execute over and over again.
| | 00:16 | The Perl language provides
two basic types of loops.
| | 00:19 | While/until loops are loops
like the one you see above there.
| | 00:25 | The while loop will execute while the
condition is true, and the until loop will
| | 00:30 | execute while the condition is false
or until the condition becomes true.
| | 00:35 | For/foreach loops are called iterator
loops, and they are used for iterating
| | 00:41 | over a series of things or conditions.
| | 00:45 | So, these are the types of
loops that Perl provides.
| | 00:48 | So, let's go ahead and get into the details.
| | Collapse this transcript |
| Creating loops with while| 00:00 | The most common kind of loop that you'll
probably see in your work in Perl is the while loop.
| | 00:07 | So, let's take the loops.pl script, then
make a copy of it, and we'll call it while.pl.
| | 00:15 | We'll go ahead and open that up.
| | 00:18 | In this example, we have a file
that we have opened, linesfile.txt.
| | 00:24 | If we take a look at that file, we
see it has 5 lines of text in it.
| | 00:28 | We are going to use a while loop to
read this file. And we'll just say while my
| | 00:36 | line =, and we will use this
notation to read one line from the file.
| | 00:44 | We'll use a loop, and
we'll print $line, like that.
| | 00:49 | Now, I am not using message.
| | 00:51 | I am using print because these lines
will already be terminated with a new line,
| | 00:56 | because of the way that this way of
reading a line at a time from a file works.
| | 01:01 | We'll save this, and we'll Run it.
| | 01:05 | We see, we have all five lines of the
file being printed there in this while loop.
| | 01:11 | What while does is it
executes the code inside the block.
| | 01:16 | It has this block here.
| | 01:17 | It executes that code over and over,
as long as this condition remains true.
| | 01:23 | So, the structure of it is the keyword,
while, and then in parentheses, you have
| | 01:27 | the condition and in this case, we have
an assignment where we are assigning one
| | 01:32 | line at a time from this
file handle to the variable,, $line.
| | 01:38 | We have declared it is a my variable,
so it will be local inside the scope.
| | 01:43 | Then the code inside is
this one statement print $line.
| | 01:48 | So, that will happen over and over as
each line of the file is assigned to
| | 01:53 | it, until it reaches the end of the file, at
which time, this variable will become false.
| | 01:58 | It will actually become undefined,
the undef value, and the loop will end.
| | 02:03 | It will close the file handle and be done.
| | 02:06 | So, when we run it, we will Run it
one more time, you can see we print all
| | 02:11 | five lines of the file.
| | 02:12 | So, that is the basic while loop.
| | Collapse this transcript |
| Creating loops with until| 00:00 | Perl also has an until loop because the
Perl language likes to have things that
| | 00:05 | are inverse from other more common things.
| | 00:07 | So, the while loop, as we learned,
continues to execute while the condition is true
| | 00:12 | and until loop executes while the
condition is false, or until it becomes true.
| | 00:19 | So, let's take a look at how this works.
| | 00:21 | We'll go ahead and copy a loops.pl to until.pl.
| | 00:28 | We'll open that up, and we'll take a
look at the rather-backwards until loop.
| | 00:36 | In this case, we are going
to declare the variable first.
| | 00:41 | Because everything is so backwards,
this actually works out to be very
| | 00:46 | convenient to do it that way.
| | 00:49 | We'll type until. I am going to
need another pair of parentheses here.
| | 00:53 | We are going to go ahead and
assign $line = a line from the file.
| | 01:00 | Then we are going to test it
using a regular expression.
| | 01:03 | Regular expressions are
covered later in the course.
| | 01:05 | What we're looking for is simply
whenever there is a 3 in the line of text,
| | 01:11 | then this will become true
and it will end the loop.
| | 01:17 | So, I'll go ahead and print the line.
We'll Save that and go ahead and Run it.
| | 01:25 | Here, we have line 1 and
line 2 and oh! We got a line 3.
| | 01:30 | This condition became true and the loop ended.
| | 01:34 | So, we did not print all
the five lines of the file.
| | 01:37 | Remember, this file here has five lines in it.
| | 01:40 | So, this is how an until loop works, and
it looks backwards because it is backwards.
| | 01:46 | In fact, the more common way that
you'll see them used is more or like this,
| | 01:53 | print line until line=<FH>,
file handle, has this 3 in it.
| | 02:05 | You'll most often see it used postfix
like this, because it just kind of makes
| | 02:11 | more sense that way,
| | 02:13 | because it is a backward sort of a loop
and it's not how our brains normally think.
| | 02:20 | So, this actually tends
to make a lot more sense.
| | 02:23 | This the way that you'll see it most
often, especially because it's such
| | 02:27 | a simple thing. We are only doing one thing,
print line and then until this condition.
| | 02:32 | So, we'll go ahead.
| | 02:33 | We'll Save this and Run it.
| | 02:35 | You'll see that it does exactly the same thing.
| | 02:39 | But this is the way that you'll see it
most often with this postfix notation.
| | 02:43 | Again, because the until is the backwards
condition from how you normally think of a loop,
| | 02:49 | this might look a little odd the
first few times you see it, until you get
| | 02:53 | used to it.
| | Collapse this transcript |
| Iterating lists with foreach| 00:00 | The foreach loop in Perl is an iterator
loop, and it's used for stepping through
| | 00:06 | a list of somethings.
| | 00:07 | So, we'll go ahead and look at how that works.
| | 00:09 | We'll make a copy of Loops.pl,
and we'll call foreach.pl, f-o-r-e-a-c-h-.-p-l.
| | 00:17 | We'll go ahead and open that up.
| | 00:19 | In this example, we are not
going to use our linesfile.txt;
| | 00:23 | we'll just delete that.
| | 00:24 | We declare a variable, my @list=, and
we'll list out some strings "one", "two",
| | 00:36 | "three", "four" and "five".
| | 00:42 | We'll say foreach my
string and we'll put in the (@list).
| | 00:51 | We will message it, message ($s).
| | 00:56 | So, what this does is foreach will
step through every element of this list.
| | 01:03 | It will assign them, one at a time, to this
variable, my $s. Then using that, it will
| | 01:10 | go ahead and run this block of code,
which in this case, has just one the
| | 01:15 | statement, the message function call.
| | 01:18 | So, what it should do, it should
print one on a line, all of these strings
| | 01:22 | "one", "two", "three", "four", "five".
| | 01:24 | So, we'll Save that and Run it, and
there it is: one, two, three, four, five.
| | 01:30 | So, that's how foreach works.
| | 01:33 | There is a couple of interesting
things that you can do with this.
| | 01:36 | If you don't have the
variable declaration there,
| | 01:39 | then foreach will use the special built
-in variable, $_, which looks like that.
| | 01:46 | So, we'll Save that and Run it. We'll see that
| | 01:49 | that works exactly the same.
| | 01:50 | Foreach is very often used in
its postfix notation, instead.
| | 01:56 | So, this is what you will very
commonly see, something like this.
| | 02:04 | You can see that's very succinct, and its
easy to read. message this foreach of that.
| | 02:10 | You simply Save that and
Run it, and there you go:
| | 02:15 | one, two, three, four, five.
| | 02:17 | So, that's the foreach loop.
| | 02:19 | It's very convenient.
| | 02:20 | It's very useful, and you'll
find that you use it quite a bit.
| | 02:24 | It's an iterator, which means that it's
used for iterating through a list of things.
| | 02:30 | It's such a convenient
construct that before Perl,
| | 02:34 | there weren't very many languages
that had anything like it. And in modern
| | 02:37 | languages that have come after Perl or
languages that have been revised since Perl,
| | 02:42 | they been starting to put something
like this in them, languages like PHP and
| | 02:48 | Python have a foreach loop.
| | 02:50 | So, that's the foreach loop.
| | 02:51 | It iterates through a list.
| | Collapse this transcript |
| Iterating lists with for| 00:00 | The for loop is another type of
iterator loop, like the foreach loop,
| | 00:06 | only it's different.
| | 00:07 | It's a little bit more complicated in
its syntax, and it's a little bit more
| | 00:11 | flexible in some other ways.
| | 00:13 | So, let's make a copy of the Loops.pl,
call it for.pl, that's f-o-r, for. We'll open that.
| | 00:23 | We are not going to be using the
linesfiles.txt again, so we'll close there.
| | 00:30 | We'll create our list.
| | 00:32 | We are going to need an iterator
variable here, or an index variable depending
| | 00:38 | on how you look at it.
| | 00:40 | We'll go ahead and write the for loop.
| | 00:43 | That's for $i=0, our list($i) and our $i++.
| | 00:50 | That should be a semicolon.
| | 00:53 | Each of these are separated by a
semicolon. I'll get into the details in a moment here.
| | 00:57 | We'll message($list, left bracket, $i, right bracket), and we'll
go ahead and Save that and Run it.
| | 01:10 | You'll see we have our list of one,
two, three, four, five, "one, "two",
| | 01:15 | "three", "four", "five.
| | 01:16 | It's exactly what it's supposed to do.
| | 01:18 | The for loop is a little bit more
complicated than the other loops we've been seeing.
| | 01:24 | It has three different expressions:
| | 01:26 | the first one is an initializer
expression, the second one is the actual
| | 01:31 | conditions - this is just like a while
loop as long as this expression is true,
| | 01:37 | the loop will continue -
the third one is the iterator;
| | 01:41 | this is the thing that you change in
order to step through, whatever it is you
| | 01:46 | are here stepping through.
| | 01:47 | This construction is familiar to C
programmers, because the for loop is actually
| | 01:52 | borrowed directly from C. This is
exactly how the C for loop works.
| | 01:58 | Each of these expressions
is separated by a semicolon.
| | 02:04 | Then we have the body of the
loop, which is in this block here.
| | 02:07 | In this case, it just has this one
statement the message function call.
| | 02:12 | It is calling this list element.
| | 02:15 | What we have done here - this is the
common way to use for loops, especially
| | 02:19 | for C programmers - is we've initializes this
variable as an iterator or an index variable.
| | 02:25 | It's used to index the array.
| | 02:28 | You'll remember, when we
learned about arrays in Lists,
| | 02:31 | that this is how you index an array
using the dollar sign to get the scalar variable.
| | 02:36 | You use the square brackets to indicate which
member of the array you wanted to get.
| | 02:44 | The $i, in this case, is a number.
| | 02:47 | It's stepping from 0. It's incrementing.
| | 02:51 | This is the increment operator,
which simply adds 1 to the variable.
| | 02:56 | So, this steps from zero, one, two,
three, four, as does this one.
| | 03:01 | You remember that arrays
are indexed starting at 0.
| | 03:04 | So, this is element number 0.
| | 03:06 | This is element number 1,
and this is element number 2.
| | 03:10 | So, we could actually
print out our iterator as well.
| | 03:14 | Our index, "index is $i", and put a
space there and then the list element.
| | 03:21 | Well, this is the string
concatenation operator here.
| | 03:24 | Let's save that and Run it, so you can
see index is 0 and we number 1; index is 1.
| | 03:31 | We have number 2.
| | 03:33 | Then the increment, this
happens after the loop is done.
| | 03:37 | It's as if there was another block here,
and this code is inside of the other block.
| | 03:43 | So, it happens after the block
is run and before the test happens.
| | 03:51 | So, it happens that in
little window of time there.
| | 03:55 | In this case, all that does is
increments the index variable by 1.
| | 04:01 | So, you can see that this is a lot more
complicated than the foreach operator,
| | 04:06 | which is why you'll see the
foreach operator used a lot more,
| | 04:09 | because usually what you need an
iterator loop for is just to step through an
| | 04:15 | array, or step through a list of things.
| | 04:18 | So, the foreach loop is
very convenient for that.
| | 04:21 | The for loop, which was the traditional
way to do this before the Perl language,
| | 04:26 | back in the days when C was considered
a high level language and applications
| | 04:30 | were mostly written in Cm
| | 04:32 | The for loop was the common way to do
that and still a very common way to do
| | 04:35 | it for C programmers.
| | 04:38 | So, it comes out a little bit complicated.
| | 04:40 | It's actually very flexible and
powerful, and there are other ways that
| | 04:43 | it's occasionally used.
| | 04:45 | But you will more often see
the foreach loop used in Perl.
| | Collapse this transcript |
| Using loop control statements| 00:00 | Perl has a couple of statements that
are very useful for controlling the
| | 00:04 | execution of loops, and we're
going to look at those right now.
| | 00:07 | So, let's go ahead and make a copy of
loops.pl, and we'll call this one control.pl.
| | 00:16 | Open that up and go ahead and read our
linesfile.txt, and we'll use a while loop.
| | 00:22 | Say while my $line =, grab that from the
file handle, print $line, save that and run it.
| | 00:35 | And there we have our five
lines in the linesfile.txt.
| | 00:39 | There is the text file.
| | 00:41 | It's got the five lines in it.
| | 00:43 | Let's say that we wanted to print all
of these lines, except we really didn't
| | 00:47 | want to print line number 3.
| | 00:48 | So, what we can do is we can use the
next, you could say, next if line has a 3
| | 00:58 | in it, like that, and what that will do
is if this condition is met - the line has
| | 01:04 | a number 3 in it some place - then the
statement next will execute, and what next
| | 01:10 | does is it just skips everything else
in the block of code and goes around to
| | 01:15 | the next iteration of the loop.
| | 01:18 | So, save that and run it, and you'll
see that it prints every line except it
| | 01:22 | doesn't print the line with a 3 in it.
| | 01:28 | Along with next, there is also last,
which also skips the rest of the code in the
| | 01:36 | block, but it does not go around the loop again.
| | 01:39 | It actually exits all the way out of the loop.
| | 01:42 | So, in this case, it will just print
the first two, and then it will stop, save
| | 01:47 | this, and go ahead and run it, and that's last.
| | 01:52 | So, actually, next and last are very,
very useful, where you may have a loop
| | 01:57 | that you want to go through and at some point you
want to not bother with the rest of the code.
| | 02:03 | You need to go and reinitialize
something or you find that this is just not a
| | 02:06 | line of text that you need to process,
or you might find something conditioned
| | 02:12 | where you just need to exit the
loop altogether, and you'll use last.
| | 02:15 | So, those were the loop control
statements, next and last, and that's how
| | 02:19 | they work.
| | Collapse this transcript |
| Building loops with postfix statements| 00:00 | One of the really nice things about the
Perl language is that all of the control
| | 00:04 | structures can be written
postfix as well as prefix.
| | 00:08 | So, we've seen a little bit of
this in our coverage of the individual
| | 00:12 | loop control structures.
| | 00:14 | Let's take a look at this
in a little bit more detail.
| | 00:16 | Go ahead and make a copy of loops.pl,
and we will call this postfix.pl.
| | 00:23 | We'll go ahead and open that,
and here you have the file open.
| | 00:28 | We'll go ahead and print all
the lines of the file, like this.
| | 00:36 | And that is all that's necessary.
| | 00:39 | That will print each line
of the file line-by-line.
| | 00:41 | I'll go ahead and save it and run it.
| | 00:43 | You see there is the lines
of our file: 1, 2, 3, 4, 5.
| | 00:47 | This is our linesfile here: 1, 2, 3, 4, 5.
| | 00:51 | You'll notice that this is probably
the simplest loop we've written so far.
| | 00:55 | It has the print statement, which is
the internal function for printing and
| | 01:01 | while, and just the file handle in angle
brackets, inside the parentheses for the
| | 01:06 | while, and that's really all there is to it.
| | 01:10 | The way this works is by using as a
temporary variable, Perl's default variable,
| | 01:15 | it actually is exactly the same
as if we'd written it like this.
| | 01:27 | Save that and run it.
| | 01:28 | You see, that works exactly the same.
| | 01:32 | This is Perl's default variable.
| | 01:34 | It's one of the special variables,
and we'll cover it in a little bit more
| | 01:37 | detail in the chapter on Special
Variables, but this is the most common of all
| | 01:41 | the special variables.
| | 01:42 | This is used as a default variable in
all kinds of places throughout the Perl
| | 01:47 | language. And so if you don't include
this assignment, then Perl just assumes that
| | 01:53 | that's what you are doing,
| | 01:53 | that you are assigning to the default variable.
| | 01:56 | And if you don't include it here, the
built-in print function actually assumes
| | 02:01 | that you are printing the default variable.
| | 02:03 | And so this just becomes a convenient
shorthand, and you'll see this a lot in
| | 02:07 | Perl, and save that and run it again,
and we see that that's how that works.
| | 02:14 | If we want to, we could use our own variable.
| | 02:16 | We'll have to declare it first, and we
can also give it a default value - that's
| | 02:22 | always a good idea - and you can say
print $line while $line = the file handle
| | 02:30 | and that will do exactly the
same thing, but it will use our variable
| | 02:34 | instead of the default variable.
| | 02:36 | Likewise, we can use until, and in this
case, we'll go ahead and assign over here
| | 02:43 | from the file handle, and we'll
test this for say the line with a number
| | 02:51 | three in it and go ahead and save that
and run it, and you'll see this also is
| | 03:00 | rather common construction.
| | 03:02 | We say we're printing each of the lines,
and we're loading the line here because
| | 03:07 | we're using the postfix notation, and
we're using until and just printing until
| | 03:12 | we get to the line that says number
three. And in this case, we've already printed
| | 03:15 | it, and so it gets printed out that way.
| | 03:19 | So, that's while and until in postfix notation.
| | 03:22 | You can also foreach in postfix
notation, and what I'm going to do here is I'm
| | 03:27 | going to go ahead and delete all of
this because we're not going to use our
| | 03:31 | linesfile.txt anymore, and we'll go
ahead and declare a list, my list = ('one',
| | 03:38 | 'two', 'three'). I am using the
single quotes here because we're not
| | 03:45 | interpolating. We don't need to. 'four' and
'five' and say print, and in this case,
| | 03:53 | I'm going to use the default like this,
because I want the new lines, and there
| | 03:59 | aren't any new lines in
our strings, foreach list.
| | 04:05 | And that is a very simple way to do that.
| | 04:08 | We'll go ahead and save that and run
it, and there we have 1, 2, 3, 4, 5.
| | 04:14 | Any of the control structures in Perl
can be used postfix. These are some of the
| | 04:20 | ones that are very common that you'll
see commonly and are very powerful in this
| | 04:25 | structure,.aAd you see that the action
is at the beginning of the line and a lot
| | 04:30 | of people like that.
| | 04:32 | When you get used to these Perl-isms,
they become very powerful, and of course,
| | 04:37 | you'll see them very commonly
when you look at existing Perl code.
| | Collapse this transcript |
|
|
7. Special VariablesUnderstanding special variables| 00:00 | The Perl language has a number of
special variables that it uses for a number
| | 00:05 | of special purposes.
| | 00:08 | They are used for default
values for functions and loops.
| | 00:11 | They are used for error codes and
messages, used for information about the
| | 00:15 | system and its environment.
| | 00:18 | There are over 70 special variables,
last time I counted. Each new version of
| | 00:23 | Perl seems to add a few more.
| | 00:25 | You can see a full list of the
special variables in the Perl documentation
| | 00:30 | page called perlvar.
| | 00:33 | We're going to cover just a few of the more
popular ones, the more commonly used ones.
| | 00:37 | But keep in mind that there is a lot
more and it's not a bad idea to go ahead
| | 00:42 | and review that page, just to
get an idea of what's available.
| | 00:46 | Probably the most common special
variable is the Default Input Variable.
| | 00:51 | This is used for default values
from loops and things like that.
| | 00:57 | Many of Perl's built-in functions use
the dollar underscore variable as a default
| | 01:03 | input, so that these functions
can be used in shorthand notation.
| | 01:08 | When we get into regular expressions,
you'll see that Pattern results are
| | 01:12 | available in the numbered
variables, $1, $2, $3, et cetera.
| | 01:18 | The System error variable, dollar, exclamation point,
| | 01:21 | or $bang is very commonly used for
system errors and messages. And we've seen
| | 01:27 | that already in some of our examples.
| | 01:31 | The errors from eval are passed in dollar, at sign.
| | 01:36 | The Process ID number is passed in
dollar, dollar, and the Program name is passed in
| | 01:43 | $dollar 0. Lists of arguments for subroutines
are in the array underscore, the at sign, underscore.
| | 01:53 | Command-line arguments are in the ARGV array.
| | 01:58 | Lists of paths that Perl searches for
libraries and modules is available in the
| | 02:04 | INC, short for include array, and the
environment variables are available in a
| | 02:11 | special hash variable called percent ENV.
| | 02:15 | Again, these are just the most common ones.
| | 02:17 | There is a very complete list in
the Perl documentation, in the perlvar
| | 02:22 | page. And in the next couple of movies,
we'll look at some examples of how
| | 02:27 | to use these.
| | Collapse this transcript |
| Using special variables| 00:00 | Let's take a look at some examples of how
special variables are used in Perl programs.
| | 00:05 | The point of this exercise is to see,
in general, how the special variables are
| | 00:11 | used, not necessary to learn how
to use specific special variables.
| | 00:15 | There is a lot of the specific special
variables, and it's a good idea for you to
| | 00:19 | familiarize yourself at least with
the list of them and then do some
| | 00:23 | experimenting to learn how
to use them individually.
| | 00:26 | But we're going to give you some guidance here,
generally, how they are used in Perl program.
| | 00:31 | So, let's take a look. Go ahead and
make copy of the specials.pl file.
| | 00:36 | I'm going to just call this one examples.pl,
and we'll go ahead and open that.
| | 00:43 | And the first thing we see here is that a
special variable is being used, right off the bat.
| | 00:47 | This @ARGV is a special variable that
gives you the command-line arguments.
| | 00:52 | Let's take a look here at an example of
that. And if I use message and put a join
| | 00:59 | in there and @ARGV, we'll save that.
| | 01:06 | Now if we run this here in the IDE,
nothing will happen. And that's because there
| | 01:10 | aren't any command-line arguments,
and it's a little bit of a pain to get
| | 01:13 | command-line arguments into the IDE.
| | 01:15 | So, we're just going to do
this on the command-line.
| | 01:18 | How you get to your command-line will
depend on what operating system you are using.
| | 01:22 | So, here I am on the command-line, and I
am in the 07 Special Variables folder.
| | 01:27 | And I'm just going to type perl
examples.pl and put some arguments on the
| | 01:32 | command-line: one, two, three, four, five.
And we'll run that and we see that we
| | 01:38 | get one two:three:four:five, separated by colons.
| | 01:42 | So, what's happened here is we've used
this join function and what join does is
| | 01:47 | it takes a list and it puts separators
in them and returns it as a string, and so
| | 01:51 | message is printing that string with
those colons in between the elements of the
| | 01:56 | array. And so this list here is called
ARGV with the at sign, and that's the
| | 02:02 | special variable that contains all of
the arguments from the command-line.
| | 02:06 | And so these five arguments from the
command-line, one, two, three, four and five
| | 02:12 | got past that special list, got joined
together with the colons and printed out
| | 02:17 | from our message function.
| | 02:19 | So, that's how a special variable is used.
| | 02:23 | Now there is another special variable,
which is at, underscore, and this used for passing
| | 02:29 | arguments into a function. And we've
actually used it here because we've passed
| | 02:33 | these arguments into the function.
| | 02:35 | This is a list of arguments, @ARGV,
and it's getting passed into this main
| | 02:39 | function. And so inside of the
function, the special variable at, underscore will have
| | 02:45 | that list that's been passed in, which in
this case, will be the same as the ARGV list.
| | 02:49 | So, if I save this again and run it on
the command-line, we'll see that we get
| | 02:54 | exactly the same result, because those
same elements of the array are getting
| | 03:00 | passed into this function, and they
are being received in a special variable
| | 03:05 | at, underscore. If I were to, instead, pass a
different list of strings, say six, seven and
| | 03:14 | eight, like this, and save that, even
though these arguments here are getting
| | 03:23 | passed in, I now get six, seven and
eight because I'm not using @ARGV anymore.
| | 03:31 | Now I'm just using this at, underscore, which is
getting this list of arguments from the
| | 03:37 | function call inside of the function.
| | 03:40 | So, that's the at, underscore. It's very useful.
| | 03:43 | It's used very commonly for
passing arguments into a function.
| | 03:49 | Likewise the dollar, underscore is very common.
| | 03:53 | That's the so-called default variable
and so it's used, for example, if we
| | 03:59 | had it for each loop, and we're
looking at that at, underscore, and we go ahead and just
| | 04:07 | say print, like this,
| | 04:09 | what will happen is that each of
the elements in this array will get
| | 04:15 | printed one-by-one.
| | 04:17 | This is using the
special dollar, underscore, but it's invisible.
| | 04:21 | We don't actually need to type it,
because it's a default variable.
| | 04:24 | So, if I save this and run it, you
will notice that we get them without any
| | 04:28 | punctuation in them here, because this
is just printing them one-by-one without
| | 04:33 | joining them, or anything like that.
| | 04:35 | But this is exactly the same as if we had specified,
explicitly, the dollar, underscore in both of these places.
| | 04:43 | So, we save this and run it, and
we still have that same result.
| | 04:49 | So, in a nutshell, that's how the
special variables are used, the dollar, underscore special
| | 04:54 | variable is oftentimes not typed
because it's a default variable, at, underscore is used
| | 05:01 | for passing arguments into a
function from a function call.
| | 05:06 | And that, of course, is a
list because it's got the at sign.
| | 05:10 | And the dollar, underscore is a scaler.
| | 05:12 | It's a one-dimensional
variable because it's got the dollar sign.
| | 05:16 | And so these are the most common
special variables, and that's how they
| | 05:20 | are commonly used.
| | Collapse this transcript |
| Working with special file handles| 00:00 | Along with these special variables
there are some other special tokens. They
| | 00:05 | are actually a type of variable called
File Handles, and there is a few of them
| | 00:11 | that are used for different purposes in
Perl. And so why don't we go ahead and
| | 00:16 | make a copy of this specials.pl, and
we'll call this filehandles.pl, and we'll
| | 00:24 | go ahead and open that.
| | 00:27 | When we run this - I'll just go ahead and run it -
| | 00:30 | you see this message comes out.
| | 00:32 | It says, "This is the template.pl
exercise file," blah, blah, blah.
| | 00:36 | And this happens with this print
function down here in the message subroutine.
| | 00:44 | And the print function, by default,
prints out to the standard output stream.
| | 00:50 | And this is a UNIX-ism, which happens
to also be present in most operating
| | 00:55 | systems, including Microsoft Windows,
because so much of modern programming and
| | 01:01 | modern operating systems is derived
from concepts that come from UNIX way back
| | 01:06 | in the 70's and 80's. And so there is a
special output stream called Standard Out.
| | 01:12 | And if I just type here Standard Out,
like that, and save and run, you'll
| | 01:19 | see that nothing changes.
| | 01:21 | It does exactly that.
| | 01:22 | So, the Standard Output stream is the
default, and you don't have to specify it.
| | 01:26 | But this is how a File Handle was
specified in the print statement here.
| | 01:33 | And if, instead, we were to use one of the
other ones like Standard Error,
| | 01:39 | like that, you'll see that here in
Eclipse this will print in red because that's
| | 01:45 | the way the standard error
stream is printed here in Eclipse.
| | 01:50 | If we were to do this in the command-
line and if I run this script here, we see
| | 01:59 | it looks exactly the same.
| | 02:01 | The difference is, and where this whole
Standard Output and Standard Error comes
| | 02:06 | from, is that the default output
stream in the console is Standard Output.
| | 02:12 | So, if I put this here back to Standard
Output, Standard Out, like that
| | 02:18 | and save it and run it, then
we have still what we expect.
| | 02:22 | If I were to redirect the output to a
file using the redirect operator in the
| | 02:30 | shell on the command-line here in UNIX,
and that's the same in Windows and in
| | 02:33 | most operating systems,
| | 02:35 | you'll notice that nothing came out
to the screen, but there is now a file
| | 02:40 | called tempfile.txt and
it has that output in it.
| | 02:45 | Go ahead and remove that file. And so
it's not there anymore. See, all we have now
| | 02:53 | is the filehandles and specials.
| | 02:56 | If I change this, again, to
Standard Error and try that little
| | 03:02 | experiment again, I'll save it here
switch to the terminal, and we will go ahead
| | 03:07 | and redirect the output to the tempfile.txt,
| | 03:10 | you'll see that this still comes to the
screen now, because the standard error
| | 03:15 | is not getting redirected. All that's
getting redirected is the standard output.
| | 03:19 | So, this is the purpose of Standard
Error, because often times when you are
| | 03:23 | running command-line programs or in
programs that their output goes some place
| | 03:27 | other than the screen, like
for instance on a Web Server,
| | 03:30 | if you are running CGI on a Web
Server, the standard output stream does not
| | 03:35 | always go to the screen. It sometimes
going to a file, like it was in this case,
| | 03:39 | but you want that error
message displayed to the user.
| | 03:42 | So, in this case, it always will
because we're going to the standard error.
| | 03:46 | And if you are in another environment,
like, for instance, this clips environment
| | 03:50 | or a Web Server, your error will go some
place where it will look like an error.
| | 03:55 | In this case, it gets displayed in red.
| | 03:57 | If you are on a Web Server, it might go
to the error log, so your error stream
| | 04:01 | is actually important.
| | 04:03 | And what I will often do is in my error
function I'll have that go to standard error.
| | 04:10 | So, that if we actually have an error
instead of a message and I type error
| | 04:15 | here and we'll make this one go to standard out,
which is the default, so I'll just delete that.
| | 04:21 | And if I save this and run it, now our
error message comes out here and it's in
| | 04:27 | red because it's going to the error
stream, or if I was running this from the
| | 04:33 | command-line, or I am redirecting to a
file or redirecting to a pipe or some
| | 04:37 | place else, I still get this
error message out to the screen.
| | 04:41 | So, that's the function of the
standard output and standard error.
| | 04:46 | A couple of other special
filehandles that I want to talk about.
| | 04:49 | I want to talk about a special
input filehandle, and this one is a
| | 04:56 | little bit different-looking.
| | 04:57 | It looks like this.
| | 04:59 | It's a special input handle.
| | 05:05 | What it does is it takes any file names
that you specify on the command-line, it
| | 05:11 | opens the files and reads them.
| | 05:13 | And so if I were to just make this
whole script a whole lot shorter, just take
| | 05:19 | out all those other stuff so the script
is very, very short and save that, and
| | 05:25 | then on the command-line, I'll just
run this from itself, I'll just write perl
| | 05:31 | filehandles.pl filehandles.pl.
| | 05:34 | And so what we have here is we are running
the script filehandles.pl and its argument,
| | 05:40 | on the command-line, is just its own file name.
| | 05:42 | And so when I do that it will
actually print out the file, just like that.
| | 05:48 | And so this special file handle, what it
does is it takes all of the files that
| | 05:53 | are specified on the command-line
and it just reads them line-by-line.
| | 05:58 | It's using the special dollar, underscore, like that,
and so if I save that and run it, you'll
| | 06:07 | see that it does exactly the same
thing. It prints out the whole file.
| | 06:13 | And in fact, if we wanted to, we could
do something like this. That's another
| | 06:21 | special variable that
will print out a line number.
| | 06:24 | And we'll save that and run that, and now
you see we get the file with line numbers.
| | 06:29 | So, that's yet one more special file
handle, and this one, it's the empty file
| | 06:34 | handle and it's used for getting
file names from the command-line.
| | Collapse this transcript |
| Using special constants| 00:00 | There's one final category of Special
Variables that I want to share with you,
| | 00:03 | and these are the special constants.
| | 00:05 | They are not actually
variables. They are constants.
| | 00:08 | And so let's take a look at them.
| | 00:11 | You can see what those look
like and how you use them.
| | 00:13 | Make a copy of specials.pl, and we'll
call this one constants.pl, and we'll go
| | 00:20 | ahead and open that. And we'll
come down here to our little message.
| | 00:25 | Now let's say that you wanted to
just print the name of the file that
| | 00:28 | contains the Perl script.
| | 00:30 | You could say the file name is, and use
the special constant called File, and
| | 00:37 | the special constants, you can always
tell them. They start with two underscores.
| | 00:41 | They end with two underscores.
| | 00:43 | And in this case, it's called File, and
they are usually all in capital letters.
| | 00:48 | And they don't need a dollar sign before
them, or anything like that, because they
| | 00:51 | are really not variables.
| | 00:52 | They are constants.
| | 00:53 | So, it's like a literal value.
| | 00:55 | So, we'll go ahead, and we'll
save this, and we'll run it.
| | 01:01 | We'll see it says the file name is,
and then this part here is all from
| | 01:06 | the special constant.
| | 01:07 | Now let's say we wanted to know the
line number that this line of code is on.
| | 01:13 | Again, let's say The line
number is, and save that and run it.
| | 01:19 | See, the line number is 12 and indeed,
this is on line 12 in the source file.
| | 01:25 | There is a couple of other ones.
| | 01:27 | There is one called Package, and this
is used in modules, when you are writing
| | 01:33 | modules. And it's actually not used very often.
| | 01:35 | It's just used if you happen to need
the name of the module for some reason.
| | 01:40 | And this one will say main because we
are not running in an external module, and
| | 01:44 | the main program is always called main
and this says the line number is main. We can
| | 01:49 | say the package is main
and save that and run it.
| | 01:56 | And finally, there is one that's called End.
| | 01:59 | And what I am going to do here is I am going
to put that in here, and we will get an error.
| | 02:03 | And the error that we will get is that
the message subroutine cannot be found.
| | 02:11 | The reason for that is that this
actually ends the script, and everything after
| | 02:15 | this is ignored as if it were a comment.
| | 02:18 | The reason for this, and the way
that it's used is when you're doing pod
| | 02:21 | documentation, which we'll
talk about in another chapter.
| | 02:25 | So, if I go ahead and save this and
run it, you'll see we get this undefined
| | 02:29 | subroutine main message, and you'll
notice there is that package name again.
| | 02:34 | And the reason for that, of course,
is that it can't find this subroutine
| | 02:39 | because the end of the
script is considered to be there.
| | 02:44 | And so these are the special
constants, and that's how they are used.
| | 02:49 | So, if you are looking for any of
those usages, that's where you find them.
| | Collapse this transcript |
|
|
8. OperatorsPerforming basic math with arithmetic operators| 00:00 | Arithmetic operators in Perl are very
simple and very much like arithmetic
| | 00:05 | operators in other languages.
| | 00:07 | Let's go ahead and make a copy of our
operators.pl file and we'll name this
| | 00:13 | arithmetic.pl and we'll go ahead and
open that and let's define a variable, my n
| | 00:26 | equals and we'll just say 42 +
12 and we'll print out the answer.
| | 00:37 | The number is and we'll just
concatenate the $n there with the string
| | 00:44 | concatenation operator,
which we'll learn about later.
| | 00:46 | Click save and run and voila, the number is 54.
| | 00:53 | So that's the Addition operator.
| | 00:55 | Addition is a binary operator,
which means it has two operands.
| | 00:59 | In this case the one on the left is 42
and the one on the right is 12 and the
| | 01:04 | result of that operator, what it returns,
is the sum of the two numbers, which in
| | 01:11 | this case gets assigned to
this $n and printed out over here.
| | 01:17 | Likewise, we have the Subtraction
operator, also a binary operator with two
| | 01:21 | operands, and it returns the difference
between the numbers subtracting from left to right.
| | 01:27 | And if I save that I'll
run it, the answer is 30.
| | 01:33 | Likewise, we have the Multiplication
operator, which is an asterisk like it is
| | 01:38 | in most computer languages.
| | 01:41 | And if I save that and run it
we see that the answer is 504.
| | 01:46 | And finally the Division
operator. Save that and run it.
| | 01:52 | We see 42 divided by 12 is 3.5.
| | 01:57 | Now if we just go ahead and assign the
number 42 we can see a couple of other
| | 02:02 | interesting arithmetic operators here.
| | 02:05 | And I'll just save that and run it,
and you can see that the number is 42.
| | 02:10 | Now if we take this $n and put the ++
operator after it, now this is a Unary
| | 02:17 | Increment operator. This will
actually increment n by one.
| | 02:21 | So that now the number will
be 43. I'll save that and run it.
| | 02:26 | Now it's 43 and likewise there
is a Unary Decrement operator.
| | 02:32 | It's unary because it has one operand.
| | 02:34 | And I'll save that and run it and we have 41.
| | 02:39 | Now a couple of interesting things
about these unary operators. You can put them
| | 02:44 | before, or prefix, or after, postfix.
| | 02:48 | And they do pretty much the
same thing but not exactly.
| | 02:52 | Here you see that it
incremented and made it a 43.
| | 02:55 | Now if I take out this line entirely and
I put that increment operator over here
| | 03:03 | and save it and run it we do get 43
but if I take it and I put it here at the end,
| | 03:11 | after the n in postfix
notation, we'll get a different response.
| | 03:17 | Now it says 42. In fact,
| | 03:19 | it's still working. We saw
that worked on the line before.
| | 03:22 | But what it's doing is when it's
postfix, it increments after taking the value;
| | 03:28 | when it's prefix it
increments before taking the value.
| | 03:31 | So if I print this line again you'll
see that I'll get 42 and then I'll get 43,
| | 03:43 | because the increment is
happening after this first value is taken.
| | 03:47 | If I put it beforehand and save and run,
we'll see that the increment happens before.
| | 03:54 | So the numbers are the same in both places.
| | 03:56 | So that's the interesting
thing about the unary increment and
| | 03:59 | decrement operators.
| | 04:01 | In the prefix notation the increment
or decrement happens before the value is
| | 04:05 | taken and in the postfix notation
the increment or decrement happens after
| | 04:10 | the value is taken.
| | 04:12 | So those are the arithmetic
operators. We have the simple four function
| | 04:16 | arithmetic: addition,
subtraction, multiplication and division.
| | 04:20 | And we also have the unary increment
and decrement operators that can operate
| | 04:24 | either prefix by incrementing or
decrementing before the value is taken or
| | 04:29 | postfix by operating after the value is taken.
| | Collapse this transcript |
| Creating conditionals with comparison operators| 00:00 | Comparison operators are often
used in conditional statements.
| | 00:04 | So, we'll take a look at the
comparison operators in Perl now.
| | 00:08 | We'll make a copy of operators.pl.
| | 00:11 | We'll call this comparison.pl.
| | 00:14 | We'll go ahead and open that up, right here.
| | 00:17 | We'll just delete this and create a conditional.
| | 00:20 | Say if, I'll just put a 1 in there for now.
We'll put something else there in a moment.
| | 00:26 | Message, this is true, and we'll
have an else here and we'll say
| | 00:33 | message("this is NOT true").
| | 00:37 | Now we'll take a look at
some conditional operators.
| | 00:40 | If I were to say, for example, 5 == 5,
and you'll notice that double equal sign.
| | 00:46 | That is the equality operator for numbers.
| | 00:51 | I save that and run it
and we'll see that is true.
| | 00:56 | If I test for inequality with the not
equals and save that and run it,
| | 01:03 | we'll see this is not true.
| | 01:06 | So the double equals sign or the bang
equals, exclamation point equals, these
| | 01:12 | are equality and inequality, or equals
and not equals, for numeric operators.
| | 01:18 | Likewise, we can test for less than or
greater than, the less than operator, and Eclipse
| | 01:25 | goes and tries to match that bracket there.
| | 01:27 | So 5 is not less than 5.
| | 01:29 | If I save that and run it,
we'll see that is not true.
| | 01:34 | Now 5 is less than 6, so I save that
and run it, we'll see that is true.
| | 01:42 | Likewise with the greater than,
5 is not greater than 6.
| | 01:47 | So I save and run and see this is not true.
| | 01:50 | Or if I make it 6 is greater than 5,
we'll see that of course is true.
| | 01:58 | See if I run that,
that is true.
| | 02:03 | We can check for greater or equal to,
like that, and that of course will be true.
| | 02:11 | We can check for less than or equals to
and Eclipse will do all kinds of things
| | 02:17 | with that, <=, like that. save and run.
| | 02:23 | That is not true.
| | 02:25 | Likewise, there are separate
comparison operators for strings.
| | 02:28 | I have six equal to five,
so we see the eq letters.
| | 02:39 | So for strings, we use letters, and you
can remember it like strings are made of
| | 02:43 | letters and numbers are made of symbols.
| | 02:47 | So we use the letters eq for equals to.
| | 02:50 | Six, of course, is not equal to five.
| | 02:52 | So if I save and run that, see that's not true.
| | 02:55 | But if I put the same string on both
sides, save and run, we see that is true.
| | 03:02 | If I try four and five, this is interesting.
| | 03:09 | I say four is greater than five.
| | 03:14 | We'll see that, in fact, this is true
because four is after five alphabetically.
| | 03:22 | Four is less than five, like that.
| | 03:26 | That of course is not true,
because this is comparing alphabetically.
| | 03:33 | So, we also have the not
equals, ne for not equals.
| | 03:38 | Of course those are not
equal, so we have this is true.
| | 03:43 | So in a nutshell, those are how
the comparison operators work in a
| | 03:49 | conditional statement in Perl.
| | 03:50 | There are a few other comparison operators.
| | 03:54 | If you would like to see the
complete list, you can look in the
| | 03:57 | Perl documentation.
| | 03:58 | I'm just going to press the right
mouse button here, anywhere on the screen,
| | 04:03 | and Eclipse with EPIC installed, just click on
Perldoc and you get a little search thing here.
| | 04:08 | So you search for perlop and you get a
complete page on all of the Perl operators.
| | 04:14 | Let me just maximize this
here, so you can see it.
| | 04:18 | It's actually very long and thorough.
| | 04:22 | It has all of the operators listed.
| | 04:24 | We're going to go through
most of them in this chapter.
| | 04:26 | We'll go through a lot of them anyway,
the major ones, the ones that you'll
| | 04:29 | use the most often.
| | 04:30 | But if you want the complete list and
complete documentation, that's where
| | 04:34 | you can find it.
| | Collapse this transcript |
| Combining conditionals with logical operators| 00:00 | Logical operators in Perl are
also used in conditional statements.
| | 00:05 | So we're going to take a look at that here.
| | 00:08 | So we'll make a copy of the operators
.pl file. I'm going to call this one
| | 00:13 | logical.pl and we'll open that up, go
ahead and set up our conditional again
| | 00:23 | here, if, I'll just put a 1 in there for
now and message this is true, and else,
| | 00:35 | message this is not true.
| | 00:38 | Now I'm going to be putting some
expressions in here. I'll make a little bit of space.
| | 00:45 | First thing I want to do is even
before we do that is I want to show you the
| | 00:53 | results of a logical expression and we
were looking at comparison operators here
| | 00:58 | in another lesson here in this chapter.
So if I say $x = 5 = 5-- So now I'm
| | 01:06 | going to put parenthesis around that
and see if the parser likes that a little better.
| | 01:13 | Oh I need a "my" there, there we go.
| | 01:17 | So what we have here is a logical
expression, this is testing for equality, and
| | 01:21 | the result of that expression is true.
| | 01:25 | So if we print the result, x is $x,
we'll also get that this is true because we
| | 01:33 | have that if(1) there.
We'll just save this and run it.
| | 01:36 | x is 1 because that is the result of this true.
| | 01:42 | Perl uses 1 for true and if I say 5 is
not equal to 5, this is now going to be
| | 01:47 | false and we'll get a different result.
| | 01:51 | And it says x is blank and so what
that is, is that's the empty string.
| | 01:55 | If I go ahead and put single quotes
around that, you'll see that is an
| | 02:01 | empty string. Run that again.
| | 02:03 | Of course, there's several different
possible values for false, a lot of
| | 02:07 | even possible values for true in
Perl, but when it takes the result of a
| | 02:12 | logical expression, it uses this one
and the empty string as the defaults
| | 02:19 | for logical values.
| | 02:22 | So we're going to take some logical
values here and I'm just going to go ahead
| | 02:26 | and use expressions at first.
| | 02:27 | So we'll say 5 equals 5, so we
know that that is true or 6 equals 6.
| | 02:38 | So if either of those are true then
the result will be true, all right.
| | 02:40 | So I'm going to save that and run
it and in fact we get this is true.
| | 02:47 | If I say 5 equals 6 and we know that this
is not true but I still have this OR 6 equals 6.
| | 02:53 | We'll run that and we see that it's still true.
| | 02:57 | If I say 5 equals 6 or 6 equals 7,
now we know neither of these is true, so the
| | 03:04 | whole or expression is not going to be
true, so we'll save that and run it and
| | 03:10 | we have this is not true.
| | 03:12 | So this is a logical operator.
| | 03:16 | The common logical operators are OR and AND.
| | 03:20 | So I'm going to try the AND
version. That's two ampersands.
| | 03:24 | The two vertical bars is OR;
| | 03:26 | the two ampersands is AND.
| | 03:30 | So we'll save that and run it and
of course that is also not true.
| | 03:35 | So OR and AND are binary operators.
| | 03:38 | For OR to be true either the left-
hand or the right-hand has to be true.
| | 03:45 | For AND to be true, both the left-hand
and the right-hand have to be true and
| | 03:53 | there's two forms of OR and AND in Perl.
| | 03:57 | There's the high
precedence and the low precedence.
| | 04:01 | This is the high precedence
version and so when you're using the high
| | 04:05 | precedence version and this is the one
you'll see the most often because it's
| | 04:08 | been around the longest,
| | 04:10 | you really need to use parenthesis
on your expressions on either side.
| | 04:15 | If you don't use the parenthesis on
either side, you might end up binding things
| | 04:20 | differently than you expect to bind them.
| | 04:23 | And so generally I recommend for when
you're using it in a purely logical state
| | 04:30 | that you use the low precedence
version and those are the words AND and OR.
| | 04:36 | And so if I use an AND here, I get exactly the
same result, which says that this is not true.
| | 04:42 | If I go ahead and make this true,
6 equals 6 and 7 equals 7, we know that that's true,
| | 04:50 | so we save and run and we get
that that is true or use simply the word "or"
| | 04:56 | and save that and run it and
we see that that is also true.
| | 05:03 | Now, when would you want to use the other kind?
| | 05:06 | There is a special case here. The
high precedence logical operators can be
| | 05:12 | used in a different way.
| | 05:14 | I can say something like my $s equals
and let's just give it a variable.
| | 05:23 | We'll say ARGV sub 1 or default.
| | 05:31 | Now this is a very common construction here.
| | 05:34 | We'll go ahead and message this out
and what this will do is it looks at
| | 05:40 | ARGV sub 1 and if it is not true, in other
words, if it's an empty string or if it's
| | 05:48 | an undef value or if it's anything that
doesn't evaluate to true, then the other
| | 05:53 | side of this OR will be evaluated and
the result is returned to the string.
| | 06:01 | This was done this way because it was
a very common way to do things in shell
| | 06:04 | scripts before Perl was around.
| | 06:06 | It's kind of a shorthand that was
very common for programmers back then and
| | 06:11 | you'll still see it today. In fact
I use it quite a bit in my scripts.
| | 06:16 | In cases where what you mean is
actually "do a logical operation," I will tend
| | 06:23 | to use the word versions. They're a
lower precedence, they're a lot more
| | 06:26 | reliable in those kinds of situations,
and in situations where I actually want
| | 06:32 | to take the value and do something
with it, I'll use the symbolic versions
| | 06:36 | because they're high precedence, they
bind tightly and they tend to work really
| | 06:41 | well in these kinds of situations.
| | 06:44 | So it's good to know about.
Now let's take a look at an example.
| | 06:48 | If you look in the RandomQuote script
down here in the RandomQuote folder,
| | 06:51 | I'll just go ahead and open this up
and we'll look in some of these and
| | 07:00 | sure enough there we have one.
| | 07:01 | If we look at the error function down
here, my $e = shift || 'unknown error' and
| | 07:08 | so what this will do is if error is
called without an argument then this shift
| | 07:13 | will turn undef and the OR will be
invoked and this side of the expression will
| | 07:18 | be evaluated and it'll be returned to
the e and so what will happen is this
| | 07:22 | becomes like a default value.
| | 07:25 | You'll see this kind of a
construction very often when you're looking at
| | 07:29 | other people's code and you'll find
when you get comfortable with it that
| | 07:32 | you'll use it a lot too.
| | 07:33 | It's a nice shorthand.
| | 07:34 | It's very quick and easy and it's
really clear once you understand the
| | 07:38 | paradigms involved.
| | 07:40 | So I'll go ahead and close that and we'll
close that and coming back here to logical.pl.
| | 07:49 | Just in summary.
| | 07:50 | You have two different ways
of doing the logical operators.
| | 07:53 | You have the letter version, which
are commonly used for the pure logical
| | 07:59 | evaluation and you have symbol version,
which can be used in that context as
| | 08:03 | long as you're using parentheses or it
can be used for an alternate value, in
| | 08:09 | the case where you may not know if
a variable has a value in it or not.
| | 08:14 | So these are the common ways
that the logical operators are used.
| | Collapse this transcript |
| Reading file metadata with file test operators| 00:00 | File test operators are a little bit different.
| | 00:03 | They look a little bit different than
some of the other operators and they might
| | 00:05 | be unfamiliar to you unless you've done
some shell programming in UNIX in which
| | 00:09 | case they would be familiar
because that's where they come from.
| | 00:13 | Let's go ahead and take a look.
| | 00:15 | We'll make a copy of the operators.pl
file and call it filetest.pl and we'll
| | 00:24 | open that up and I'm going to go ahead
and start by grabbing a listing of the
| | 00:33 | current directory, all the files in the
current directory. Do that like this and
| | 00:41 | this is another little special operator.
| | 00:44 | This is the directory listing
operator. Actually that asterisk in there
| | 00:47 | is just a wild card. I could, if
I want everything that starts with a D,
| | 00:50 | I could do that.
| | 00:52 | We just do this and this is
a very common thing in Perl.
| | 00:55 | It just gets a quick little directory listing.
| | 00:58 | There are other ways to do with modules
and object oriented programming but this
| | 01:02 | is quick and easy and it's a
special variable that's also an operator.
| | 01:07 | You'll see that construct now and then.
| | 01:10 | So we'll go ahead and step through
this for each, my file name, of that dirlist
| | 01:17 | and message($fn). Go ahead and save
that and run it and you'll see we have a
| | 01:24 | list of this directory and it's got all the
files you can see them also listed over here.
| | 01:29 | I've created a directory and this directory
| | 01:32 | is just an empty directory.
| | 01:33 | We'll look at this in the Finder on this Mac.
| | 01:37 | You'll see there is an empty
directory with little folder symbols.
| | 01:40 | Some people call them folders,
some people call them the directories.
| | 01:43 | Older folks who have been around for a
long time tend to call them directories
| | 01:46 | and the newer folks who have been just
using computers for a few years tend to
| | 01:49 | call them folders because that's a
relatively new way to refer to them but we
| | 01:54 | have three plain files
in here and one directory.
| | 01:57 | So we'll close that and
we can see that listing here.
| | 02:00 | Now in our program we want to know that
which of these are plain files, which of
| | 02:04 | them are directories, maybe how big are
the plain files, maybe the permissions
| | 02:08 | of the files, things like that.
| | 02:10 | We use the file test operators for that.
| | 02:12 | So what I can do here is I can say if
dash f, fn and that will tell me if it's a
| | 02:19 | plain file and fn is a plain file and
else message $fn is something else.
| | 02:33 | If I go up to the screen there and
save that and run it, you'll see that a
| | 02:40 | directory is something else and
the other one is our plain files.
| | 02:44 | So I want to also test maybe for directories.
| | 02:47 | So I can do an elsif( -d $fn ) and
that will tell me if it's a directory.
| | 02:53 | I'll just go ahead and copy this line,
because a lazy programmer is a good programmer.
| | 03:02 | I'll call that a directory, save and run.
| | 03:09 | So directory is a directory, filetest.pl
is a plain file, and those are all like that.
| | 03:16 | There's something else of course. In a
UNIX file system and a Mac is a UNIX file
| | 03:20 | system, you might find symbolic links,
you might find named pipes, you might
| | 03:24 | find other types of things
and there are tests for those.
| | 03:28 | We don't use them very often and we're just
going to cover the really common ones here.
| | 03:32 | If you want to see a complete list just
highlight this -f here and bring up the
| | 03:37 | Perldoc and we'll maximize this and
you can see an entire list of all of the
| | 03:43 | different file test operators and you
can see there are quite a lot of them.
| | 03:47 | A lot of them refer mostly to UNIX file
systems and some of them apply in other
| | 03:52 | places as well and it doesn't
hurt to use them in other places.
| | 03:56 | You can get things like permissions.
| | 03:57 | You can get things like symbolic links and such.
| | 04:01 | For example, if we wanted to get the
size of a file here we have in the plain
| | 04:05 | file section, we already know because
we've tested here that inside of this
| | 04:09 | block we have plain files only.
| | 04:12 | So I can say my $size =-s $fn like that
and that will give me the size and I can
| | 04:20 | put it in parentheses here and I can say
$size bytes and if we save that and run it
| | 04:28 | here, we'll see that we have a
number of bytes for plain files.
| | 04:34 | So those are the file test operators and
that's how they work and how you use them.
| | 04:39 | Again these are the really common ones
and if you want a complete list then you
| | 04:43 | can look in the Perl documentation for a
complete list of all the different file
| | 04:47 | test operators that are available in Perl.
| | Collapse this transcript |
| Creating a sequence with the range operator| 00:00 | The range operator is very useful and
a little bit unique to Perl and so we'll
| | 00:06 | go ahead and take a look at how that works.
| | 00:09 | We will make a copy of the operators.pl
file, we will name it range.pl, and
| | 00:15 | we will go ahead and open that and
there we will put a print foreach 1 through 9.
| | 00:26 | And you'll notice the
dot dot is a range operator.
| | 00:30 | It's a binary operator, so it has an
operand on the left and an operand on the right.
| | 00:35 | And what it does is it returns a list
of all the elements, starting with the
| | 00:40 | operand on the left and ending
with the operand on the right.
| | 00:43 | So in this case, it will
return this list of numbers 1 through 9
| | 00:46 | and it will print them all, all on one line.
| | 00:50 | We'll save that and run it and
there is our numbers of 1 through 9.
| | 00:57 | The range operator can be used for
letters as well as numbers. If I say
| | 01:02 | 'a' through 'z' and save that and run it,
| | 01:08 | you will see we get all the letters a through z.
| | 01:11 | In fact, because it's a list, we can say
comma and then another range. And use 'A'
| | 01:18 | through 'Z' and save that and
run it and we get all of those.
| | 01:26 | If we wanted just all of the hexadecimal
digits, we could say 0 through 9 and 'a'
| | 01:34 | through 'f' and save that and run it.
| | 01:41 | Now we have all the hexadecimal digits.
Let's say we wanted all the two digit
| | 01:47 | numbers, for use say in
dates. Let's say '00'
| | 01:51 | through '31' and let's go ahead and
print a join of this instead of a foreach.
| | 02:02 | Join with a comma and a space and
we'll go ahead and put that inside up there.
| | 02:12 | And we need a comma there, don't we?
| | 02:13 | We'll save that and run it and
now we have all of the numbers.
| | 02:20 | And you'll notice that they have their
leading zeros because Perl was able to
| | 02:24 | figure out that that's what we wanted,
from the way we set this, '00' through '31'.
| | 02:31 | So that's how the range operator works.
| | 02:33 | It's incredibly useful.
| | 02:35 | You'll see it a lot in Perl.
| | 02:37 | You won't see it anywhere else.
| | 02:38 | I am not sure if there are any other
languages that actually have a range
| | 02:42 | operator or one that works like this.
| | 02:44 | If they do, I haven't seen it, but in a
nutshell that's the range operator and
| | 02:50 | that's how it works in Perl.
| | Collapse this transcript |
| Combining strings with the concatenation operator| 00:00 | We are going to talk about the
string concatenation operator.
| | 00:03 | We've used this quite a bit throughout
the course already, but I just wanted to
| | 00:07 | pay a little bit of attention, and make
sure that we understand exactly what it
| | 00:11 | is and how it works.
| | 00:13 | So we'll start by making a copy of the
operators.pl file, and we'll just call
| | 00:17 | this concatenation.pl.
| | 00:21 | And we'll open that up and in a nutshell,
if I were to take this string just say
| | 00:29 | "string one" and dot and "string two,"
| | 00:34 | these two strings, a dot is
the concatenation operator.
| | 00:38 | Let's save that and run it. These two
strings will be run together and you'll
| | 00:43 | notice there is no space at the beginning
there and there is no space at the end there.
| | 00:47 | So there will be no space between those
two words, string one and string two, and
| | 00:56 | so that's what the string
concatenation operator does.
| | 00:59 | Now of course, if we put a space here and
we can even put another type of a thing here.
| | 01:06 | We can just put the number 2.
| | 01:07 | The string concatenation operator will
still work, because it will simply use
| | 01:13 | this 2 as if it were a number.
| | 01:14 | We'll run that and we have
string one and had a space and a 2.
| | 01:20 | So the string concatenation
operator is one dot. Of course it will work
| | 01:24 | with variables as well.
| | 01:26 | my $s1 = "string 1" and my $s2 =
"string 2" and then we can here and simply
| | 01:33 | say ($s1 . $s2).
| | 01:46 | And if we save that and run it, again
there will be no space between them.
| | 01:51 | String 1 and string 2.
| | 01:54 | A lot of times you'll see
something like this, where they will just
| | 01:58 | concatenate in a space.
| | 02:00 | You can string as many of
these together as you want.
| | 02:03 | And we'll run that and then that
will have a space next to it as well.
| | 02:09 | So that is the string
concatenation operator. It's very simple.
| | 02:12 | But it's worth learning about and
it's worth understanding because you will
| | 02:16 | use it quite a bit.
| | Collapse this transcript |
| Streamlining strings with quote operators| 00:00 | There are some special operators
called quote operators in Perl and again I
| | 00:04 | don't know of any other
languages that have anything like this.
| | 00:07 | We covered them briefly in the
chapter on strings, in Chapter 3.
| | 00:13 | We are going to cover them here
again a little bit more focused like.
| | 00:16 | And we'll start by copying the
operators.pl file to quoteop.pl.
| | 00:22 | Open that up.
| | 00:26 | We will take a look at this string here.
| | 00:29 | You will notice that it has these quote
marks at the beginning and the end.
| | 00:32 | If we wanted to, say, put
this template.pl in quotes,
| | 00:37 | if I were to just put in a quote mark there and
a quote mark there, this is not going to work.
| | 00:43 | You'll notice we'll get a syntax error.
| | 00:45 | We got one right there.
| | 00:46 | It says Bareword found where operator expected.
| | 00:49 | And that would be this Bareword here.
Because these quote marks are actually
| | 00:54 | terminating the string.
| | 00:56 | One solution, and this is a solution
that most languages use, is to put a
| | 01:00 | backslash before each of the quote marks.
| | 01:02 | This is called quoting the quote marks.
| | 01:05 | And what that does is that tells the
Perl interpreter to not consider that the
| | 01:11 | symbolic quote mark, but simply
consider it the character the quote mark.
| | 01:17 | In other words not to interpolate it,
not to interpret it, but rather just to
| | 01:21 | put in the literal quote mark.
| | 01:22 | And if I save that and run
it, we get the quotes there.
| | 01:28 | But Perl has what I consider a more
elegant solution and that would be to
| | 01:32 | replace these quote marks with a
quote operator, and that would be the q
| | 01:38 | followed by any symbol whatsoever.
| | 01:40 | So I'll just use a square bracket and
I'll put another square bracket over
| | 01:45 | here, and what this does is this q is the
quote operator and it's followed by a symbol.
| | 01:53 | You can use a slash.
| | 01:54 | A lot of people would use a slash.
| | 01:55 | I'll often use curly braces.
| | 01:57 | You can really use anything that you want.
| | 01:59 | I'll show it to you with a slash.
| | 02:01 | That's a very common one to use.
| | 02:04 | And if I save that and run it, you'll notice
that we get the string exactly as we expect.
| | 02:11 | I often use curly braces.
| | 02:14 | The idea is to use something that
you're not likely to use in a string.
| | 02:19 | Save that and run it and that's how that works.
| | 02:22 | Now just like there is two
different kinds of quotes, there are single
| | 02:25 | quotes and double quotes, there are single
quote operators and the double quote operators.
| | 02:30 | And the difference is exactly the same.
| | 02:32 | The single quote operator
with a single q does not interpolate.
| | 02:36 | So if I have a variable up here, $x = 42,
semicolon, and I go ahead and put that in here,
| | 02:46 | I put it in parenthesis, $x, like that.
| | 02:51 | And I save this and run it.
| | 02:54 | With the single quote, we'll actually
get the letters $x. With the double quote,
| | 02:59 | and that would be having two qs,
qq for the quote operator.
| | 03:04 | Save that and run it.
| | 03:05 | It actually interpolates
that $x and puts in the $42.
| | 03:09 | This is the same as using double
quotes versus using the single quote marks.
| | 03:14 | So the quote operator is very useful
and you'll see it used a lot in Perl.
| | 03:19 | Some people just go ahead and use
them all the time for all their strings.
| | 03:22 | I just use it where it seems to be necessary.
| | 03:24 | A lot of times if I want to include
some HTML, you know if I wanted to say p
| | 03:29 | class= "example" and put this
all in a paragraph like that.
| | 03:36 | You know then I can get that and I get
those quote marks, and I'll often times
| | 03:40 | use this qq for something like that.
| | 03:43 | And when I run it you'll notice I get
that full HTML and I get the quotes just
| | 03:48 | where they belong and everything
and it just works out really great.
| | 03:52 | So that's the common usage of the
quote operator and you see you have got the
| | 03:58 | double quote operator and the single
quote operator, just like you have double
| | 04:02 | quotes and single quotes in Perl.
| | Collapse this transcript |
| Understanding operator precedence| 00:00 | We're talking about operators.
We need to talk about operator precedence.
| | 00:04 | And we've covered this a little bit in
some of the other parts of the course,
| | 00:08 | but I want to talk about
it very specifically here.
| | 00:10 | So let's go ahead and make a
copy of the operators.pl file.
| | 00:14 | I'll call this precedence and you get
extra points for spelling on this one.
| | 00:22 | Go ahead and open that up and we are
just going to make an expression here, say
| | 00:29 | my $n equals and we'll say 3+7*5.
| | 00:38 | And we will print out $n. Now the
point here when we save this and run this
| | 00:45 | which we'll do in a moment.
| | 00:46 | Let's go ahead and save it now.
| | 00:49 | The result will depend on if you do
the multiplication first or if you do the
| | 00:53 | addition first. If you do the
addition first, 3+7= 10*5=50. If you do the
| | 01:00 | multiplication firs,t 7*5 is 35+3 is 38.
| | 01:06 | So which is the answer going to be?
| | 01:08 | I'll go ahead and run this and
we'll see that the answer is 38.
| | 01:11 | So the multiplication
happens before the addition.
| | 01:15 | If you want it to be the other way
around you need to use parentheses.
| | 01:23 | And now you'll get the number 50.
| | 01:27 | Obviously it's a good idea to use
parentheses any way, but now and then you'll
| | 01:31 | see code that doesn't use the
parentheses and you are going to want to know by
| | 01:35 | looking at it and not
having to experiment which is it.
| | 01:39 | Operator precedence is how you know which it is.
| | 01:44 | Here we have a chart of operator
precedence in Perl and you'll notice as we look
| | 01:51 | down the chart here,
| | 01:53 | first you have terms and list operators.
| | 01:56 | Lot of these operators you haven't seen
because a lot of them are a little bit
| | 02:00 | obscure or about subjects we haven't
covered yet. If you look down towards the
| | 02:04 | bottom, the last two, you'll see in the
last one of course is the plus and the
| | 02:09 | minus and the dot, which is the
concatenation operator, and above that is the
| | 02:15 | asterisk which is multiplication and
the slash which is division and the
| | 02:19 | string repeat operator.
| | 02:21 | So you can see from this that the
multiplication is indeed a higher precedence
| | 02:27 | than the addition, and as a result
that multiplication is going to happen
| | 02:31 | before the addition.
| | 02:32 | So what we have here is a list of
all of the different precedents.
| | 02:38 | We are not going to stop and talk about
all these. We haven't actually covered
| | 02:41 | all of these operators and some of them
we're simply not going to go into detail
| | 02:45 | on because they are just not used often enough.
| | 02:49 | But in the Exercise Files Chapter 8
operators directory you'll see this entire
| | 02:55 | table in Operator Precedence.txt.
| | 02:57 | I'll go ahead and I'll maximize this.
| | 03:00 | And you have this entire file in a nice
little text file that you can look at on
| | 03:05 | your computer. You can print it out
and set it down next to your computer.
| | 03:08 | It also exists in the Perl Op
documentation page that we looked at earlier in
| | 03:15 | the Perl manual, and so this is
a useful little table to have.
| | 03:21 | You'll notice in this table in the
first column you have the associativity,
| | 03:25 | which is left, right, or non-associative.
| | 03:29 | And in the right column you have
what the name of the operator is or the
| | 03:33 | symbol of the operator.
| | 03:34 | The associativity is which
direction the operands are evaluated.
| | 03:41 | Like for example if we go down here to
the multiplication and addition, these
| | 03:46 | arithmetic operators, you'll see
that their associativity is left.
| | 03:51 | And what that means is that the left
operand, they're binary operators, and so the
| | 03:56 | left operand is always
evaluated before the right operand.
| | 04:00 | In most cases that isn't really going
to matter to you, but sometimes it will,
| | 04:06 | and so it's just good to know.
| | 04:08 | So operator precedence is an
important thing to understand.
| | 04:12 | Most of the time it's a really good
idea to simply use the parentheses when you
| | 04:20 | care about the order in
which things are evaluated.
| | Collapse this transcript |
|
|
9. Regular ExpressionsUnderstanding regular expressions| 00:00 | Regular expressions represent a very
powerful pattern matching language, often
| | 00:06 | called regex or in the plural regexes.
| | 00:09 | Regular expressions are usually used
within other languages like Perl or PHP or
| | 00:16 | Python or C or some other language
which will control the flow of the program,
| | 00:23 | while the regular expressions
themselves are commonly used for
| | 00:27 | search-and-replace
operations within the language.
| | 00:31 | Regular expressions can be very
simple or they can be very complex.
| | 00:35 | The language itself is compact, so it
can look very intimidating even when
| | 00:42 | it doesn't need to.
| | 00:43 | For example this regular expression
here is used to extract the hours, minutes,
| | 00:49 | and seconds from a time string, or
this regular expression here is used to
| | 00:55 | insert commas into a string of
numbers so that it can be read in thousands,
| | 01:00 | millions, billions, with the commas.
| | 01:03 | So the regular expressions themselves
are actually very simple but because the
| | 01:08 | language is so compact they
can look fairly intimidating.
| | 01:12 | So let's go ahead and learn more about
how regular expressions are used in Perl.
| | Collapse this transcript |
| Searching and replacing text with regular expressions| 00:00 | The most common use for regular
expressions is to do search and replace operation.
| | 00:05 | So let's take a look at how that's done in Perl.
| | 00:09 | We'll start by making a working copy
of the regex.pl file and we'll name this
| | 00:15 | search.pl. Go ahead and
open it up and take a look.
| | 00:19 | You'll notice that what this file does
is it opens a file called short.txt and
| | 00:26 | it reads it and prints it and then closes it.
| | 00:29 | And that gives us something to
work with as we experiment with the
| | 00:33 | regular expressions.
| | 00:35 | So let's go ahead and run this and you'll
see that it prints out this short.txt file.
| | 00:42 | And what this short.txt file is, is
it's just a few paragraphs that have been
| | 00:46 | taken out of the Perl regular
expression manual, which the entire text of it is
| | 00:52 | here also in the same folder, and this
is the Perl regular expression manual.
| | 00:57 | It's quite long, and so I thought I
just take a few paragraphs from it and we
| | 01:01 | could use that to work with our
experiments here in this lesson.
| | 01:05 | So for example if we just wanted to
look at all the lines that had the word
| | 01:08 | regular in them, we could say print if and put
in a pattern match here for the word regular.
| | 01:15 | And this is what a pattern match looks
like in Perl and if we save this and run it,
| | 01:21 | we see we just get these five
lines that have the word regular in them.
| | 01:26 | So the two slashes here on either side
of the word regular are the pattern match
| | 01:31 | operator and what's between them is
called the pattern, and the pattern is
| | 01:35 | expressed in regular expressions and
the simplest form of regular expression is
| | 01:39 | just to put the text in there
that you are looking to search for.
| | 01:42 | The pattern match operator with the
two slashes is actually a shorthand.
| | 01:47 | The full version has the letter M at
the beginning to indicate that it's a
| | 01:52 | pattern match operator.
| | 01:54 | When you don't have the M then you have to
use slashes if you want to use this operator.
| | 02:00 | If you do use the M, then instead of
the slashes you can use any matching
| | 02:05 | character that you want. For
example we could use vertical bar.
| | 02:09 | The reason we would want to use a
vertical bar instead of the slashes is in case
| | 02:15 | we wanted to search for slashes.
| | 02:16 | So if I had a slash in the middle
here, now I am searching for slashes.
| | 02:21 | If I would use the slashes instead and
put a slash in the middle, then you see
| | 02:27 | we get an error, because Perl doesn't
really know what that means. The three
| | 02:33 | slashes is not something
that it's familiar with.
| | 02:36 | We can do what's called escape the
middle slash by putting a backslash before it
| | 02:41 | and then Perl understands it.
| | 02:43 | The backslash simply means don't
interpret the next letter. Use it literally.
| | 02:48 | And so that allows us to search for a
literal backslash inside the slashes.
| | 02:52 | But the more common way to do that if
you want to search for slashes is to use
| | 02:56 | the M and use a vertical bar or one of
the other available delimiters and then
| | 03:01 | search for the slash like this.
| | 03:04 | If we save that and run it, you'll see
we find the two lines that have slashes
| | 03:09 | on them in this file.
| | 03:11 | So that's how we do the basic pattern
matching for the regular expressions in Perl.
| | 03:17 | One other thing that you need to be
aware of here is that we're actually
| | 03:21 | matching the default variable, the $_.
So this is the equivalent of doing this.
| | 03:31 | So the dollar underscore, if we don't
specify a variable, the dollar underscore
| | 03:36 | is presumed, and if we are
presuming the dollar underscore, we are also
| | 03:40 | presuming the pattern match operator
because we are using pattern matching here.
| | 03:45 | This is the pattern match operator and
if you're specifying your own variable
| | 03:49 | like for example if I were to say my
$line, then I would have to test for line,
| | 03:55 | matches this pattern, and then
we use the pattern matching here.
| | 04:00 | So the pattern match operator is this
equals with the tilde, the little character
| | 04:06 | to the left of the 1 on your keyboard
with a Shift, and that's the tilde,
| | 04:11 | the little curly line there.
| | 04:13 | And that's used with the match
operator or it's used with the substitute
| | 04:18 | operator that we are
going to look at in a moment.
| | 04:20 | So if we save this and run it, you'll
see that it does exactly the same thing.
| | 04:25 | We got that error because we're
printing the dollar underscore.
| | 04:29 | So I have to also print the line
there instead, save that and run it.
| | 04:35 | Now we get just lines with the slash in them.
| | 04:38 | So let's take a look at how we would do a
replace operation instead, so from print line.
| | 04:45 | And then I want to do a replace, I
would say line and use the pattern match
| | 04:49 | operator and then I use the substitute
operator, and the substitute operator has
| | 04:54 | two operands and they are all
separated by the slashes or a vertical line or
| | 04:58 | whatever match character you want to use there.
| | 05:00 | And let's say I want to substitute or
replace where the word regular is and
| | 05:05 | call it Bob instead.
| | 05:06 | I am going to use all caps for Bob,
so that it's really easy to see.
| | 05:10 | So if I save that and run it,
| | 05:13 | now we get the entire file and every place
where it said regular before we have BOB.
| | 05:18 | So this is now Bob expressions,
Bob expressions, Bob expressions.
| | 05:25 | So that's how you do a search and replace.
| | 05:28 | So the S is for substitute and this is
the pattern to match and this is what it
| | 05:33 | will get replaced with.
| | 05:35 | So for example if we wanted to replace
every occurrence of the letter A with
| | 05:38 | Bob, then we could do it this way and
if we save that and run it, we'll see it
| | 05:43 | doesn't do quite what we expect.
| | 05:46 | We still have a number of As.
| | 05:49 | So there's an A and there is an A.
| | 05:53 | So what it did is it basically got the
first A on every line, but it didn't get
| | 05:58 | all the rest of them.
| | 06:00 | That's because when Perl does a search over
or a substitute, which is the word for replace,
| | 06:07 | it really just searches for the first
one and then it goes on to the next line.
| | 06:11 | If you want it to actually match and
replace every occurrence on a line, you
| | 06:17 | have to use the modifier at the end of
the expression and the G, which stands
| | 06:23 | for global, will allow you to do it
for all of the occurrences in a line.
| | 06:28 | So we'll save this and run it.
| | 06:31 | Now we see we have Bobs everywhere
where there had been an A before.
| | 06:36 | So this is the basic function
of regular expressions in Perl.
| | 06:41 | We know how to match lines using
the match operator and we know how to
| | 06:48 | substitute using the substitute
operator and we know about the pattern match
| | 06:53 | operator, which is used to bind a
regular expression to a variable.
| | 06:59 | That's how regular expressions are used in Perl.
| | Collapse this transcript |
| Extracting matches with parentheses| 00:00 | Sometimes when you are using regular
expressions, you'll want to extract
| | 00:03 | individual matches out of the regular
expressions, sometimes more than one match
| | 00:08 | from the same expression.
| | 00:10 | And we are going to look at how
you do this using parenthesis in the
| | 00:13 | regular expressions.
| | 00:14 | So we will start by making a working copy of
regex.pl and we will call this one extract.pl.
| | 00:25 | And we will open that and we are not
going to use this file in this movie.
| | 00:30 | Instead we are going to use a pattern
of time, 05:24:37, and so we have
| | 00:39 | got hours and minutes and seconds
and we'll be extracting that.
| | 00:47 | First we will match it in a regular
expression with the header match operator
| | 00:53 | and the match itself will be (..):(..):(..).
| | 01:02 | Now the dot matches any
character. It's a wildcard.
| | 01:05 | We will talk about that in another
movie in this chapter, and the colon
| | 01:09 | matches this colon here, and this
colon matches this colon here, and what we
| | 01:14 | have is a very simple regular
expression that will match two characters on
| | 01:19 | either side of these colons.
| | 01:22 | We have the parenthesis here so
that we can extract these matches.
| | 01:27 | When you put the parenthesis around
there that allows you to extract that part
| | 01:31 | of the match from the regular expression.
| | 01:34 | And there are two ways that we can do this.
| | 01:36 | One is to say my $hour = $1,
my $min = $2, my $sec = $3.
| | 01:48 | So $1 holds the value from the first
parenthesis, $2 holds the value from the
| | 01:54 | second parenthesis, and $3 holds
the value from the third parenthesis.
| | 02:00 | So we go ahead and print this out with
message and say hour is $hour, and minute
| | 02:07 | is $min, and second is $sec.
| | 02:12 | And when we run this, save and run,
we will see that we have successfully
| | 02:19 | extracted all of these values.
| | 02:21 | The hour is 05, the minute is 24, and the second 37.
| | 02:27 | In some cases, this is really going to
be the best way to do it because of the
| | 02:31 | way your code is constructed where
you'll be extracting these different values.
| | 02:36 | But a lot of times, it's easier just
to do it directly without even using
| | 02:40 | the $1, $2 and $3 notation by
declaring your variables like this in a list,
| | 02:52 | and simply assigning it this way,
and that's because this whole pattern
| | 02:58 | match construction returns a list
of the values in these different
| | 03:03 | parenthesis in order.
| | 03:04 | So in this case it will return a list with
05 in the first element, 24 in the second
| | 03:09 | element, and 37 in the third element and
those will get assigned respectively to
| | 03:14 | hour, minute, and second and
our printout will work just fine.
| | 03:18 | So we'll save this and run it, and we
see that we get exactly the same result.
| | 03:23 | So those are two ways to extract.
Use the parenthesis to indicate which
| | 03:30 | parts of the pattern you want to extract,
and they will be returned as a list so
| | 03:36 | that you can assign them directly to
a list of variables, or they will be
| | 03:40 | returned in the $1, $2, and $3
variables so that you can use those instead.
| | Collapse this transcript |
| Matching wildcards| 00:00 | Sometimes when you are matching things
with regular expressions for searches
| | 00:04 | and replaces, what you are matching
might not be entirely specific and for this
| | 00:09 | we have what are called metacharacters or
wildcards in the regular expression language.
| | 00:15 | So lets take a look at how that works.
| | 00:17 | We'll start by making a working copy
of regex.pl and we will call wild.pl.
| | 00:24 | Go ahead and open that.
| | 00:26 | And we will be going
through the short.txt file again.
| | 00:29 | Let's just say we want to print out
the first four characters of every line.
| | 00:35 | So we will start with our matching
operator and we will match the beginning of
| | 00:39 | the line, which is matched
with the caret metacharacter.
| | 00:44 | The little up arrow there. And that
matches the beginning of the line and then
| | 00:49 | the dot character matches any character.
| | 00:52 | So we will just match the first
four of those and we will put that in
| | 00:56 | parenthesis so that we extract it, and
we will print this a message and we'll
| | 01:02 | say $1 if $1, and that's because there are
maybe some lines where there aren't four
| | 01:08 | characters at the beginning of the line.
| | 01:10 | So we'll go ahead and
save that and we will run it.
| | 01:15 | And there's the first four characters
of the lines that have four characters at
| | 01:19 | the beginning of them.
| | 01:21 | Maybe we want to look at the last four
characters of the line and we can do that
| | 01:26 | with the dollar sign character,
and again dollar sign character is a
| | 01:31 | metacharacter and this one
matches the end of the line.
| | 01:33 | This time I put it outside of the parenthesis.
| | 01:35 | You can put it inside or outside.
| | 01:37 | These particular metacharacters, the
beginning and ending of the line, they
| | 01:40 | don't actually match a
character so they won't return anything.
| | 01:44 | They just match a position.
| | 01:46 | So it can go inside or outside of
the parenthesis. It really makes a little
| | 01:49 | more sense outside.
| | 01:51 | And I will go ahead and save and run that.
| | 01:53 | And these are the last four characters of
the lines that have four characters on them.
| | 01:59 | Perhaps what you want to do is you want
to get every match of a letter A that's
| | 02:05 | followed by four characters, or
followed by three characters in this case.
| | 02:11 | And so we will get the A and
the three characters after it.
| | 02:13 | And we will save that and run it.
| | 02:16 | And here we have the places where
there's a letter A and four characters after,
| | 02:23 | in this case it's a space character.
| | 02:26 | And what we are finding here is that in
fact there's some places where there is
| | 02:30 | more than one match, where we are
printing out $1 for each of them.
| | 02:34 | And so we might want to get all of
those matches on those lines and so in that
| | 02:39 | case we can assign this to in an array.
| | 02:41 | We can say list equals and we will put
a g at the end of this so we are sure
| | 02:47 | that we get all of the matches on that line.
| | 02:50 | And then we'll message the join of that list.
| | 02:52 | We'll say join with a colon, and the list.
| | 02:58 | I will say if list.
| | 03:02 | And so in this case, we need to put
a line here so that works and we will
| | 03:09 | save that and run it.
| | 03:11 | And so now we have get all of these
cases of an A followed by four characters
| | 03:16 | and we get all of the occurrences of
that on each line separated by colons
| | 03:21 | line-by-line like that, and that gives us
exactly what it is that we are looking for.
| | 03:26 | Now this dot character can be repeated.
| | 03:30 | We can specify exactly three of them by
saying dot three like that and so we'll get
| | 03:37 | occurrences of three just like we did before.
| | 03:40 | That works exactly like it did before.
| | 03:42 | We can use a large number here
and not have to count them out.
| | 03:46 | So putting a number inside of curly
braces. That will repeat the match character
| | 03:52 | before it that many times.
| | 03:54 | And so we say save, run, and we get all
of the ten characters with A. it's ten.
| | 04:01 | All of the A plus nine character
occurrences, and that works like that.
| | 04:06 | Or we can use the asterisk,
which means zero or more of these.
| | 04:12 | And we will go ahead and terminate
that with an S. so what this will give us
| | 04:15 | will be all of the occurrences of an A
and all the letters after that up to an S.
| | 04:20 | So you will see that this does not
do exactly what we expect it to do here.
| | 04:26 | Go ahead and run this.
| | 04:28 | And we will see that in some of
these cases there's an S here.
| | 04:31 | It ends in an S but it's the last S on
the line and not the first S after the
| | 04:37 | first A, and the reason for that is
that the asterisk wildcard, which means
| | 04:42 | repeat this as many times as
you can, is what's called greedy.
| | 04:46 | That means it will repeat all of them up
until the last possible match after it.
| | 04:52 | If you want it to be not greedy and
to match up to the first possible match
| | 04:55 | after it, you put a question mark
after the asterisk and that makes the
| | 04:59 | asterisk not greedy.
| | 05:01 | So we'll save this and run it.
| | 05:04 | And now we will get the shorter ones
and a colon, and then the next one because
| | 05:12 | we have this going into the list.
| | 05:13 | So this is actually giving us the A up
to the S, up to the first occurrence of
| | 05:18 | the S, and then a colon because there's
more than one match on a line and then
| | 05:22 | the A up to the next occurrence of the S. So
this is doing exactly what it is we want it to do.
| | 05:27 | We have made the asterisks not greedy
by putting a question mark after it.
| | 05:32 | So these are some of the metacharacters.
| | 05:34 | Again the regular expression language
is extremely rich and you can get a lot
| | 05:39 | of more out of it by looking at the
documentation of the regular expressions
| | 05:45 | that comes in the Perl.
| | 05:46 | We have it here in this perlre.txt file
or you can use the online documentation
| | 05:51 | and look for perlre.
| | Collapse this transcript |
| Matching classes of characters| 00:00 | Sometimes with regular expressions
you are going to want to match a class of
| | 00:03 | character, say all digits or all
punctuation marks or something like that.
| | 00:09 | There is a few different
ways to do that in Perl.
| | 00:12 | And so we are going to look at some of those.
| | 00:14 | We will start making a working copy of regex.pl.
| | 00:17 | We call this classes.pl and we'll open
that and start by declaring an array,
| | 00:29 | some place to put our matches.
| | 00:31 | We will put equals, and it will be
like that and we'll message out at the join
| | 00:38 | of the list, if list.
| | 00:44 | And we will probably use the larger
file for this, perlre.txt, because we'll be
| | 00:54 | looking things like digits and
punctuation marks and there will be more
| | 00:57 | examples of them in there.
| | 00:58 | So we'll start by declaring a class
for digits and we can do this in a
| | 01:03 | few different ways.
| | 01:04 | We can say 0 through 9.
| | 01:06 | Classes, typically, in the
square brackets like this.
| | 01:10 | And we can declare a range of characters.
| | 01:13 | So we can say something like 0 through 9,
we can something like A through Z, or
| | 01:18 | we can say A through Z, and capital A
to Z, or we can use slash I for that.
| | 01:25 | You can simply list things.
| | 01:26 | You can say match all occurrences
of 1, and 7, and 3 and these sorts of
| | 01:32 | things work just fine.
| | 01:33 | In this case, we will put a plus
sign out there to say match one or more
| | 01:37 | occurrence of this together.
| | 01:39 | I'll put this in parenthesis and
we'll go ahead and save it and run it.
| | 01:48 | And there we have some examples here.
| | 01:50 | We have 3 and 1 together, and we have a 7.
| | 01:56 | This is just matching those characters.
| | 01:58 | We want to match all the digits, of course.
| | 02:00 | We can 0 through 9 and save that and run it.
| | 02:05 | And we have got some examples with
0's in them and we have some longer
| | 02:10 | numbers like 2029, 2028.
| | 02:11 | Of course there is other simpler ways
to match classes like digits, common
| | 02:18 | classes like that, for example, the
backslash d. We have seen that before in one
| | 02:24 | of our other movies.
| | 02:25 | That matches all digits.
| | 02:27 | That's a shorthand for digits and
we see that works exactly the same.
| | 02:35 | Or we can use what are called Posix classes.
| | 02:38 | And Posix classes are actually also
enclosed in square brackets and so we
| | 02:43 | need two sets of the square brackets,
which Eclipse is giving us a little
| | 02:49 | bit of hard time with.
| | 02:51 | And the name of the class goes
inside the pair of colons like this.
| | 02:57 | And so if I say digit like that, this
is using the Posix class for all digits,
| | 03:05 | and so we save that and run it and then
see that we get exactly the same result
| | 03:11 | as we did with the other versions of this.
| | 03:14 | So there is a number of different
ways that we can specify classes.
| | 03:19 | The Posix classes are named like
this and they are all listed in the
| | 03:22 | documentation. For example, we can say
punct like that and that will get us all
| | 03:27 | different types of punctuation.
| | 03:29 | Save and run and there we have all
the different types of punctuation
| | 03:35 | listed there.
| | 03:37 | There are backslash classes for a lot of things.
| | 03:40 | There are things like any word
characters with the \W like that.
| | 03:47 | Word characters are any
alphanumeric characters, letters, numbers or
| | 03:53 | the underscore.
| | 03:54 | You can match a non-word
character with a capital version.
| | 03:58 | So the capital versions are typically
the inverse. For example, a capital D is
| | 04:02 | anything that's not a digit.
| | 04:05 | You can match white space with a
lowercase w or anything that's not white
| | 04:09 | spaced with a capital W. So
there's a number of these as well.
| | 04:14 | So these are the classes in Perl
and these are how they are specified.
| | 04:18 | Like I said there are a few
different ways to do that.
| | 04:21 | And the common ones, of course, the
digits and the white space character and the
| | 04:26 | alphanumeric characters we've looked at,
there are a lot more of them and they
| | 04:30 | are all listed in the documentation.
| | Collapse this transcript |
| Creating arrays with split| 00:00 | There's one final way of using regular
expressions that I'd like to cover here,
| | 00:05 | and that is a Perl specific thing called split.
| | 00:09 | It's a function that comes with Perl,
and it's used for splitting strings apart
| | 00:14 | on various boundaries and creating
lists out of strings and this uses regular
| | 00:19 | expressions and it's a little bit
unique and I'd like to cover it now.
| | 00:23 | So we'll start by making a working copy of
a regex.pl file and will call it split.pl.
| | 00:30 | Open that up, and we're not
going to use the file here.
| | 00:36 | Instead, we will create our time
variable that we used before, and we will say
| | 00:43 | 05:27:32 and this time we are
going to split it apart on the colons.
| | 00:50 | We're going to do this entirely differently.
| | 00:52 | We're going to say my ($hour, $minute,
and $sec) = split and there's a regular
| | 01:03 | expression match there and $time,
and then we will message, hour:
| | 01:08 | $hour, minute: $minute and sec:
$sec, and we'll save that and run it.
| | 01:24 | And you'll see that we got these
elements and we didn't have to do it the way we
| | 01:28 | did before with the matching the digits.
| | 01:31 | Instead, we just match the colon with
this very simple regular expression and we
| | 01:35 | split the string on those
boundaries into its three different parts.
| | 01:40 | There's another wonderful
example of how powerful this is.
| | 01:45 | You'll notice that when we print out
an error message, if we say here error,
| | 01:53 | "this is an error message".
| | 01:56 | Now we save that and run it. We get
this long file path here and that's
| | 02:06 | because that's actually the file name,
the way that Perl sees it when it's
| | 02:10 | passed into the program.
| | 02:12 | So if we don't want all of that,
| | 02:15 | we can split the string and say my
@me= split and we can split it on the
| | 02:22 | slash character.
| | 02:25 | So we'll use the vertical bar here to
match the slash character and comma $0.
| | 02:34 | And for those we either are on Windows
and you have backslashes instead, we can
| | 02:37 | use a character class instead of that.
We can say backslash which requires two
| | 02:43 | backslashes because we're coding it
and forward slash and the character class
| | 02:48 | just like that and that works just fine,
and then instead of this here we can
| | 02:53 | print out $me and the last element of
the Array which is indexed with a -1 like that.
| | 03:02 | And so if we save this and run it,
we see that we just get split.pl like
| | 03:08 | that for the error message.
| | 03:10 | Because we extracted all of the elements of
that long path name and just took the last one.
| | 03:17 | So split is very powerful.
| | 03:18 | It's very useful, you'll see it is used a
lot, and its syntax is very simple here.
| | 03:23 | It just uses a regular expression to
match what it's splitting on and the string
| | 03:29 | itself and it returns a list of the
elements after they're split apart.
| | Collapse this transcript |
|
|
10. SubroutinesUnderstanding subroutines| 00:00 | Subroutines are sometimes also
called functions and they're a means
| | 00:05 | of encapsulating code.
| | 00:07 | There are a number reasons you might want
to encapsulate your code in subroutines.
| | 00:12 | Some of them are that Subroutines are
reusable, so you can write a subroutine
| | 00:17 | once and you can use it over and over
within your program, or even put it in a
| | 00:22 | library and use it within
many different programs.
| | 00:27 | Subroutines can be useful for hiding complexity.
| | 00:31 | You want to keep your code clear, and
so as complexity builds you put the
| | 00:38 | complexity into different levels of subroutines.
| | 00:40 | It allows your code to be
clear and understandable.
| | 00:45 | Subroutines can call other subroutines,
and subroutines can even call themselves,
| | 00:51 | something that's called Recursion.
| | 00:53 | And this allows them to be extremely flexible.
| | 00:58 | So let's go ahead and get into the
details of using subroutines, sometimes
| | 01:02 | called functions, in Perl.
| | Collapse this transcript |
| Defining and calling subroutines| 00:00 | To begin our discussion of
subroutines in Perl, we are going to talk about
| | 00:04 | defining and calling subroutines.
| | 00:07 | So let's start by making a copy of
the subs.pl, and we'll call this one
| | 00:14 | defining.pl, and we'll open that up,
and we'll notice in here that the first
| | 00:22 | thing we do is we call a
subroutine, and this subroutine is main.
| | 00:26 | And inside of main we call
message, which is another subroutine.
| | 00:30 | And this is an example really of
reusable code and hiding complexity.
| | 00:35 | I like to have a way to display messages
that have a new line at the end of them.
| | 00:41 | And so I'll create this.
| | 00:43 | Pretty much in all of my command line
programs I'll have something called message.
| | 00:48 | And it will do what I want
it to do to display a message.
| | 00:52 | In this case it displays the message with
a newline character at the end of it.
| | 00:55 | This \n is a shortcut for a newline character.
| | 01:00 | So that as I display messages,
message one, message two, and
| | 01:09 | message three,
| | 01:13 | they're all on a separate line from each other.
| | 01:15 | I'll go ahead and save that and
run it and you can see what I mean.
| | 01:19 | If I were to do this with print, I
would have to put a new line at the end of
| | 01:22 | each of these, and while that's
not a terrible inconvenience,
| | 01:25 | to me it's just simpler to do it this way.
| | 01:28 | This is the subroutine that's
being called sub message like that.
| | 01:33 | A subroutine is declared, or defined by
using the word sub and then the body of
| | 01:40 | the subroutine is in a block,
which is limited by these braces.
| | 01:45 | And inside this subroutine you can get
arguments and other details and we'll
| | 01:50 | talk about these in later movies.
| | 01:52 | Right now we're talking about
defining and calling the subroutine.
| | 01:56 | So it gets defined with the
keyword sub and the name and the block.
| | 02:00 | And then it's called by using the
name of the subroutine followed by
| | 02:05 | parentheses, and any arguments to the
subroutine that you might have, in this
| | 02:10 | case we have a string.
| | 02:12 | Now if this subroutine were defined
before the place where it's called, if
| | 02:20 | it were defined up here, then in actuality
you wouldn't even need these parentheses.
| | 02:27 | You could call it like this.
| | 02:35 | And sometimes you'll see it done this way.
| | 02:37 | If I save this and run it, and in fact,
with Perl's built-in functions, it's
| | 02:45 | often common to have them
called without parentheses.
| | 02:52 | If you want to be able to call it that
way, but you don't want to have to define
| | 02:57 | it before the place where it's used,
you can actually-- you see we've got
| | 03:03 | errors here because now it's defined
after and there's no parentheses here.
| | 03:10 | You can declare it above by using the use subs.
| | 03:16 | This is called a pragma.
| | 03:19 | And use the qw operator, which allows
you to use a list of words and creates a
| | 03:26 | list from separate words, and you can
put a message up here and a semicolon.
| | 03:31 | And now, you can call that
function without the parentheses.
| | 03:39 | So we'll save and run, and you see there
are no errors, and it works like we expected.
| | 03:45 | You can even list several of them up
here, error up here too, and you'll notice
| | 03:50 | I have an error function down at the
bottom. And then if I wanted to, I could
| | 03:57 | make one of these an error instead.
| | 03:59 | And save that and run it and
you'll see that also can be called
| | 04:07 | without parentheses.
| | 04:08 | This is something that you'll see done
now and then in other people's code and
| | 04:12 | if you choose to do it, it's perfectly okay.
| | 04:14 | I will often use message and error this way,
so they can be called without the parentheses.
| | 04:20 | Again, it's just another convenience,
but it's one of those things that Perl
| | 04:23 | allows that a lot of people
use, that you'll see often.
| | 04:26 | So that's how you define
and call subroutines in Perl.
| | Collapse this transcript |
| Using arguments with subroutines| 00:00 | When you want to pass data and variables
and things to a subroutine it's called
| | 00:07 | calling it with arguments.
| | 00:08 | The variables that you pass
to it are called arguments.
| | 00:12 | So let's talk about how this is done
in Perl and we will make a copy of the
| | 00:17 | subs.pl and we'll call it arguments.pl.
| | 00:21 | I will open that up and again looking
at the message subroutine you'll see that
| | 00:31 | this string is passed to it and
throughout this course we've passed a number of
| | 00:36 | different things to message.
| | 00:39 | Inside of the subroutine we see this
line here, which is how that argument is
| | 00:44 | used inside of the subroutine itself.
| | 00:48 | So the way this works is the arguments
are placed inside the parentheses and
| | 00:55 | technically in Perl this is a list.
| | 00:58 | So you could, just to make this look a
little bit simpler, my $s = and we'll just
| | 01:04 | take this and move it up there, and
then we will call it the $s. If you wanted
| | 01:11 | to you could have several of them.
| | 01:13 | You could have $y and $z and we could--
$y is a number and $z is another string.
| | 01:27 | Then the subroutine is now
being called with three arguments.
| | 01:33 | So the arguments are in a list, which
in Perl is in parentheses, and they are separated
| | 01:38 | by each element of the
list is separated by a comma.
| | 01:42 | Those arguments are passed into the
subroutine with a special variable, which is
| | 01:49 | actually a list, and it looks like that.
| | 01:54 | So if you wanted to get all of these
arguments you could say my $m $y $z =
| | 02:04 | this list, like that.
| | 02:05 | We get rid of this one.
| | 02:09 | Now you have all three of these. We're
only printing one of them of course and
| | 02:13 | print $m with the new line at the end of it.
| | 02:17 | If we save this and run it
it'll do exactly what we expect.
| | 02:21 | We get just that first string which
is in $s and we're not doing anything
| | 02:26 | with the other ones.
| | 02:28 | If we wanted to we could also
display those, we could say display them in
| | 02:32 | parentheses like this.
| | 02:37 | We save that and run it.
| | 02:39 | We see that we're actually getting
all of those arguments, there they are
| | 02:43 | out there at the end.
| | 02:45 | So the arguments are getting passed in
this array. An array is simply a variable
| | 02:50 | that contains a list.
| | 02:52 | They get pass to the function as the
list inside of the parentheses and then
| | 02:58 | inside the function you can use them
just as you use any array or list, assigning
| | 03:02 | them this way and printing them out that way.
| | Collapse this transcript |
| Understanding scope and "my" variables| 00:00 | Subroutines in Perl are declared with a
block and that block has the code that
| | 00:06 | the subroutine executes when it's called.
| | 00:09 | So just like with any block in Perl you
need to be concerned with the scope of
| | 00:14 | the variables that you use inside that block.
| | 00:16 | So let's take a look at that.
| | 00:18 | We will make a copy of subs.pl for
our working space here and we'll call it
| | 00:25 | scope.pl and we will open that up.
| | 00:32 | One of the first things you'll notice is
in our subroutine message, we have this
| | 00:36 | variable here, $m, and it's declared with my.
| | 00:39 | What that does is it keeps the scope of
that variable inside of this block and
| | 00:46 | this block of course is
the body of the subroutine.
| | 00:49 | This is actually very important.
| | 00:52 | You want to make sure that the
variables that you're using that you are
| | 00:56 | declaring inside of your subroutine
are local to that subroutine or owned by
| | 01:00 | that subroutine, and cannot be
accessed or modified from outside.
| | 01:06 | There are ways to get around it,
but you really don't want to.
| | 01:10 | It's a lot cleaner to just make sure
that your variables are your variables.
| | 01:15 | For example if I were to have a $m
outside of here and have it be some other
| | 01:21 | string, I want to know that this $m
inside of this subroutine is the one that
| | 01:28 | I'm actually printing.
| | 01:29 | So this is the one that's having the
string passed from the function call there.
| | 01:34 | So when I save it and run it, you'll
see that is the $m that is being accessed.
| | 01:42 | If I had a global variable, say something
that's declared outside of the scope of
| | 01:47 | any of these blocks, then it's certainly
possible to reference that from inside.
| | 01:51 | If I say have a $g out here and yet
another string and I wanted to say print
| | 02:01 | that every time I print something here.
| | 02:03 | So I can say $g like that, and that
$g is not defined inside of my block.
| | 02:11 | So it's not local and so I am actually
accessing the next one that's found in a
| | 02:16 | scope that includes mine.
| | 02:19 | So it goes up the tree of the scope and
it finds the next $g. So if I save this
| | 02:24 | and run it, you'll see that I get
this yet another string and this here.
| | 02:31 | So every time I print out another
message, let's just print out this $m here.
| | 02:38 | So this will be the some other string.
| | 02:40 | It will put yet another
string at the beginning of it.
| | 02:42 | We hadn't run that and you see that we
get that yet another string every time.
| | 02:47 | So we can use that of course if we want
to just have say the name of the program
| | 02:51 | here, this is scope.pl, and you wanted
to maybe identify that everything that
| | 02:58 | gets printed out of here where it's coming from.
| | 03:00 | You can do something like that and in
that way you are using the scope as a
| | 03:07 | tool and rather than
having it just be an accident.
| | 03:10 | So this is scope and this is how it
affects functions, and this is how you use it.
| | 03:16 | You want to make sure that your local
variables are all scoped locally, and you
| | 03:21 | want to make sure that when you are
using variables outside of the scope that
| | 03:25 | you do that intentionally.
| | Collapse this transcript |
| Returning values from a subroutine| 00:00 | Often times when you are using a
subroutine or a function, not only do you want
| | 00:06 | to pass data into the subroutine but
you also want to pass data back when the
| | 00:11 | subroutine has done its job and this is
called the return value and that's what
| | 00:15 | we are going to talk about now.
| | 00:16 | So let's go ahead and start by making a
working copy of the subs.pl script and
| | 00:22 | we'll call this return.pl, so we are
going to learn about returning values.
| | 00:30 | For this example we will go ahead and
will write a new subroutine, something
| | 00:36 | that let's just say
just adds a couple of numbers.
| | 00:38 | So we'll call this addnum, and we'll
just pass it a couple of numbers here and
| | 00:46 | our results will be in numbers.
| | 00:48 | So I'll just say my num equals.
| | 00:50 | Often times when I am writing something new,
I'll start by saying how am I going to use this?
| | 00:57 | And I'll write an example of how I am
going to use it, and then I'll write the
| | 01:00 | subroutine to match.
| | 01:02 | So in this case,
subroutine, we'll call it addnum.
| | 01:06 | And in this case it takes two values,
and so I am going to say my $v1 and $v2,
| | 01:17 | equals like that, and so that will get me
the two values that have been passed in.
| | 01:23 | So these are arguments to the
subroutine and this is how we read them in inside
| | 01:29 | of the subroutine, by
reading them from the @_ list.
| | 01:34 | Then I will simply return the sum
of these two numbers, $v1 + $v2.
| | 01:41 | So return is a keyword in Perl and what
it does is it takes its argument and it
| | 01:48 | returns it from the subroutine so that
the subroutine can then be used in an
| | 01:55 | assignment context where that can be
assigned to a number like this or you could
| | 02:00 | use it as an argument to another subroutine.
| | 02:03 | We'll print this one out, we'll just
say $num like that, or we can say something
| | 02:09 | like this, message addendum, and give it
a couple of other numbers say 5 and 8.
| | 02:18 | So when we run this we'll get two lines,
one of them should be the sum of 2 and
| | 02:23 | 4, which would be 6, and the other one
would be the sum of 5 and 8, which would be 13.
| | 02:28 | I will save that and run it,
and there we have 6, we have a 13.
| | 02:35 | So what we've got here is two different
ways that we've called the subroutine.
| | 02:39 | One is by assigning it to a variable
and then printing out that variable.
| | 02:44 | The other is by doing it directly
without the intermediate variable.
| | 02:48 | So we can see that the subroutine
call syntactically it has the value of
| | 02:55 | whatever you return.
| | 02:57 | So you can call a subroutine in either
of these ways or in any context where its
| | 03:03 | value might be taken.
| | 03:05 | The return keyword is how you
declare what that return value would be.
| | 03:12 | So what return takes is as its argument
whatever the value is going to be that we're
| | 03:16 | returning, and the subroutine can then
be used in a context where that value is
| | 03:23 | read and that is the value
that will be used in that context.
| | Collapse this transcript |
|
|
11. References and Data StructuresUnderstanding values and references| 00:00 | In order to understand references,
you need to understand the distinction
| | 00:04 | between a variable that contains a
reference and a variable that contains a value.
| | 00:09 | A reference is a variable that
refers to a value as opposed to a variable
| | 00:14 | that contains a value.
| | 00:16 | So, this is a variable as we understand
them so far. The variable contains the
| | 00:22 | value, whereas a reference variable
contains a reference that refers to a value.
| | 00:29 | This is probably best explained by example.
| | 00:33 | In the Chapter 11 References folder in
your exercise folders, go ahead and make
| | 00:38 | a copy of refs.pl and we'll just
call this example.pl. Open that up.
| | 00:45 | Here we have a variable that contains a value.
| | 00:49 | In this case, the value is a string.
| | 00:52 | The string says, "This is
the Perl 5 references chapter."
| | 00:57 | Then we print out the
variable using the message function.
| | 01:01 | If we go ahead and run this, you'll
see that it prints out this nice string.
| | 01:06 | Now, a reference refers to the variable,
so it uses this reference operator and
| | 01:16 | then the variable itself.
| | 01:19 | So now what we have is a variable
called ref that refers to this variable here.
| | 01:28 | So the value of this is the reference.
| | 01:31 | If we print out ref, if we just
say $ref here, what you'll is Perl's
| | 01:36 | representation of the reference, which
looks like that, scalar and an address.
| | 01:42 | So this is referring to a scalar
variable, because var is a one-dimensional
| | 01:48 | scalar variable, whereas ref simply
points to the value in that variable.
| | 01:55 | So, I can actually look at the value
of it by using another dollar sign.
| | 02:00 | So what this first $, what that does
is it says show me the scalar that is
| | 02:06 | referred to by this reference.
| | 02:08 | If I then save that and run it,
then we get that value there.
| | 02:16 | The advantage of this is that as I
pass things around in my program and I
| | 02:20 | make copies of them and I pass them
into functions and return values and
| | 02:26 | such, I don't have to pass around a whole lot
of data. I can just pass around the reference.
| | 02:30 | So if I make a copy of the reference, I
now have another variable that contains
| | 02:39 | the reference of same place.
| | 02:40 | I can print out from that copy
and I still get the same value.
| | 02:46 | But all I've copied is the reference.
I haven't copied all of these bytes.
| | 02:49 | So let's say this is like a whole lot of
data or some very complicated structure of data.
| | 02:54 | I don't have to copy all of that.
| | 02:56 | I can just copy the reference.
| | 02:57 | I can pass the reference
to a function or subroutine.
| | 03:01 | I can do all kinds of things with it.
| | 03:03 | In fact, if I print out the value of
ref here and then I go down here and I
| | 03:09 | change variable to something else, and
then I message it again or message the
| | 03:18 | copy of it. Now I haven't changed the
reference. I haven't changed the copy.
| | 03:22 | All I've changed is this variable. When I print
it out the second time, we'll get the new value.
| | 03:26 | So the first time, it prints out the string
and the second time it prints out the number.
| | 03:34 | So, references are extremely powerful
and they are a little bit complicated,
| | 03:39 | because you have to think in this
indirect way about what's going on.
| | 03:45 | But what the reference is, is just a
pointer to the place where the data is,
| | 03:51 | whereas the variable actually contains the data.
| | 03:55 | So in this chapter, we're going to see
some example of how references are used
| | 04:00 | in a more powerful way, especially by
using array references and hash references,
| | 04:05 | which are the most common types of
references, and then later on in the course,
| | 04:10 | when we start talking about object
oriented programming, you'll see that Perl's
| | 04:15 | object model is built entirely on
top of this concept of references.
| | 04:20 | So where a variable contains a
value, a reference points to a value.
| | Collapse this transcript |
| Defining array references| 00:00 | References are not actually used very
often for scalar variables. They are much
| | 00:04 | more often used for arrays and
hashes and combinations thereof.
| | 00:09 | In this movie, we're going to
talk about array references.
| | 00:12 | I'll go ahead and will make a copy of
refs.pl and will call it arrayref.pl.
| | 00:18 | We'll go ahead and open that up.
| | 00:25 | In this case, we're going to
declare this as an array reference.
| | 00:31 | So, array references are
actually made with the square bracket.
| | 00:49 | They can be all in one line or they
can be moved across lines like that.
| | 00:55 | All those red dots will go
away when I save, here we go.
| | 00:58 | In this case, we have it on multiple
lines because oftentimes when you're
| | 01:03 | declaring an array or a list or a
reference to such, you've got so much of data
| | 01:07 | that you don't want to put it all in one line.
| | 01:09 | So it tends to be broken up across lines.
| | 01:12 | I like to indent them like this so that
I have some idea of what belongs to what.
| | 01:17 | Now as we run this what we'll get is
this result, which will say that it's
| | 01:22 | an array reference.
| | 01:24 | You'll see array and an address.
| | 01:28 | What's with that is an address.
| | 01:30 | So, when you print a reference, what
you get is a representation of that
| | 01:36 | reference so that you can see what the type is.
| | 01:39 | In this case, we have an array reference.
| | 01:42 | So we can use this array reference by
just putting an @ in front of the $.
| | 01:51 | So, for example, if we wanted to join
this, we've seen how we can do this,
| | 01:55 | join it with say colons, and we can
use this just like we can any other list or array.
| | 02:06 | Let me save that and run it.
| | 02:08 | We'll see that we get all
of these different values.
| | 02:12 | The first one is "This is the Perl 5
references chapter" and then there is a
| | 02:16 | colon, and then 42 and then another
colon, and "Another String" and a colon
| | 02:23 | and "one more value".
| | 02:25 | So we have dereferenced.
| | 02:27 | That's what this is called when you put
the other symbol in front of the variable.
| | 02:31 | We dereferenced it as an array or a list.
| | 02:35 | Array and list mean the same thing in Perl.
| | 02:37 | They are just two
different words for the same thing.
| | 02:40 | Then we used it in a join like we would with
any other array or list and printed it out.
| | 02:46 | So, that is how array references work in Perl.
| | 02:51 | Again, this is really powerful, because
if I want to make a copy of this, I can
| | 02:55 | just say my $copy = $var, and I have
copied the reference, but I haven't
| | 03:04 | actually copied all the data.
| | 03:06 | If I run this again with the copy here,
I can save and run and there we have
| | 03:12 | exactly the same result.
| | 03:14 | In fact, if I wanted to change one of the
values, I can change it through the dereference.
| | 03:20 | I can do this like this.
| | 03:21 | I can say copy and this is the way that
you dereference a member of an array or
| | 03:27 | hash with the arrow like that.
| | 03:29 | Then I can subscript and I can say a
number 2, which remember they are zero base.
| | 03:35 | This is number 0, this is element number
1, and this is number 2 and I can assign
| | 03:40 | it some other value like 187.
| | 03:42 | Now, when I go and print it
again, I'll just copy this line.
| | 03:48 | Again, I'm working on the copy of the
reference at this point, which is actually
| | 03:54 | getting me all the way
back here by dereferencing it.
| | 03:57 | I go ahead and save that and run it.
| | 04:00 | Then the first time it prints,
it prints "Another String" as the third
| | 04:04 | element there, right there.
| | 04:07 | The second time it prints instead,
it prints this 187, because we changed it.
| | 04:13 | So, we did all that
changing through the reference.
| | 04:16 | We didn't have to go all the
way back to the original variable.
| | 04:21 | The original variable of
course is a reference as well.
| | 04:24 | The array itself in this case is
anonymous, because I declared it this way with
| | 04:30 | the square brackets.
| | 04:31 | I could have, if I wanted to, done it this way.
| | 04:37 | I can say this is a list and
I can use regular old parentheses.
| | 04:43 | Eclipse just makes that a
little bit harder, just like that.
| | 04:48 | Now I can say my var equals-- and I can
reference that list, like that, and now
| | 04:58 | everything will work exactly as it did before.
| | 05:01 | Save this and run it and we
have exactly the same result.
| | 05:06 | So what we've done here is we
declared a list in exactly the same way as we
| | 05:09 | used to, using the list variable
itself and using the parentheses like we
| | 05:14 | normally do for a list.
| | 05:16 | Then we have created a reference to it
using the reference operator like we did
| | 05:22 | in the last movie with a scalar.
| | 05:24 | That reference operator works for any
type of object in Perl and we created a
| | 05:29 | reference and we called it var.
| | 05:30 | We made a copy of the reference and
we printed it out from the copy of the
| | 05:35 | reference and we got all the way
through to that original list, because there
| | 05:39 | it is, printed out.
| | 05:40 | Then we even went to and
changed an element of the original list.
| | 05:46 | And we got that result.
| | 05:49 | So, this is how array references
or list references work in Perl.
| | 05:55 | Again, you use the reference operator to
create the reference or instead you can
| | 06:01 | declare it directly using the square
brackets instead of the parentheses.
| | 06:06 | You can copy it, because a
reference is just another scalar.
| | 06:10 | You can make a copy of it and
it's just copying the reference.
| | 06:13 | You can dereference it this
way to get the entire array.
| | 06:18 | You can dereference it this way to get
an individual element of the array and
| | 06:22 | you can even assign to it and change it.
| | 06:26 | Then we print it out again and you
get that changed value, because you've
| | 06:31 | changed the original object and you're
still referring to it with the same reference.
| | 06:37 | So that's how array references work in Perl.
| | Collapse this transcript |
| Defining hash references| 00:00 | One of the more common uses of
references in Perl is for hash references.
| | 00:05 | So we'll go ahead and take
a look at how that is done.
| | 00:09 | And we'll start by making a working
copy of refs.pl. I'm going to call this
| | 00:13 | hashref.pl, like that.
| | 00:14 | And we'll open that up and we'll go
ahead and declare a hash, and a hash is
| | 00:24 | declared with the percent sign,
remember, and parentheses.
| | 00:30 | We have a few things in this, so we'll
go ahead and give us some space to do
| | 00:36 | these on separate lines.
| | 00:37 | This is again a common way to
declare a hash with multiple lines for the
| | 00:42 | elements, and we'll say name and "Jimi Hendrix".
| | 00:51 | It's very common when declaring
hashes to omit the quotes in the key.
| | 00:56 | And this is optional in Perl,
and you'll see it done both ways.
| | 01:00 | I tend to do it this way.
| | 01:02 | Again, you'll see it both ways and is
optional and you can just do whichever way
| | 01:07 | seems more comfortable for you.
| | 01:08 | But for this example we're going to
do without the quotes for the key.
| | 01:12 | Instrument is "Guitar", and lets say
album, "Are you experienced?", and we
| | 01:26 | can create a reference to that, like this here.
| | 01:29 | \%hash and with that does is it
takes the hash variable and it creates
| | 01:39 | a reference, and assigns that
reference to the scalar variable ref.
| | 01:43 | So if down here I print ref,
and we don't need this anymore.
| | 01:50 | I'll save that and run it.
| | 01:53 | We'll see that it prints hash and the address.
| | 01:57 | So ref is a reference to the hash of
that address, which is this hash here,
| | 02:03 | which is named hash.
| | 02:05 | And again this is even more common
than to declare that way is to do it
| | 02:10 | directly, and I'll call this hashref,
and we'll use curly braces instead of
| | 02:20 | the parentheses, and what that does is it
creates what's called an anonymous hashref.
| | 02:26 | So this here inside the curly braces
is as an anonymous hashref, and it's
| | 02:33 | assigned to this hashref variable, so that the way
that you get it is through the hashref variable.
| | 02:37 | And so we'll name this over here
hashref, and we'll print it. We'll get pretty
| | 02:42 | much the same thing
probably with a different address.
| | 02:45 | So it says hash, which is the type that the
reference is pointing to, and the address there.
| | 02:52 | And so that's our hashref.
| | 02:54 | Again, we can make a copy of it if we
want to and say copy equals hashref and
| | 02:59 | the copy will look exactly the same.
| | 03:03 | Save and run and if we want to we
can operate on it, just like we normally
| | 03:10 | would with a hash. We can say for each,
my key, sort keys, and again we're going
| | 03:18 | to refer to the hash.
| | 03:21 | We can do it through the
copy or through the hashref.
| | 03:23 | And value equals $copy $k. So this is
the dereference operator. We've seen this
| | 03:37 | before with the array refs and in this
case because it's a hashref, we've got
| | 03:42 | the curly braces for dereferencing the hash.
| | 03:45 | And so we're getting the value by using
the key in the dereference and then we
| | 03:53 | can message $k and $v like that, with
semicolon and we save this and run it.
| | 04:05 | Now this is a very very common use of
a hashref and again, we can pass around
| | 04:11 | the hashref itself. We can make copies of it.
| | 04:14 | If I want to I can add an element and
just say $copy and create a new key, say
| | 04:21 | song equals and name it here, Purple
Haze, and then when we print it out we'll see
| | 04:30 | that we have yet another element in our
hash. We have song in there. It comes out
| | 04:36 | at the end, sorted after name, and
we've done this through the copy. We didn't
| | 04:40 | even do it directly through the hashref,
because all of these point out exactly the same hash.
| | 04:47 | And so you've actually added an
element to the list by referencing it
| | 04:52 | through the hashref.
| | 04:54 | So this is very typical
of how hashrefs are used.
| | 04:58 | In fact, you'll often see them
declared just like this, as an empty
| | 05:07 | hashref and then things are added to them
throughout the program, so that's also not uncommon.
| | 05:15 | So this is a hashref. They're usually
declared anonymously like this with the
| | 05:20 | curly braces, although you can just
create a hash and use the reference
| | 05:25 | operator to get the reference and
assign that to a reference variable.
| | 05:29 | You can make copies of the reference,
you can, pass around it, you can pass it
| | 05:33 | to and from functions, you can add
elements to it, you can manipulate elements
| | 05:39 | in it, and you can use it just like
you would a hash by dereferencing it,
| | 05:45 | either with the percent sign, getting
the entire hash or with the dereferencing
| | 05:50 | operator to get individual
elements using the curly braces.
| | 05:55 | And that is how you use a hashref in Perl.
| | Collapse this transcript |
| Building mixed data structures| 00:00 | A lot of the power of references in
Perl is in the ability to mix-and-match
| | 00:06 | different types of
references all in one data structure.
| | 00:10 | Let's take a look at what
this means and how this works.
| | 00:14 | We'll start by creating a working
copy of refs.pl and we'll call this one
| | 00:18 | mixed.pl, open that up.
| | 00:22 | Now I'm going to go ahead
and start with an array ref.
| | 00:27 | I'm going to call this artists.
| | 00:31 | And this is an array ref so I'll use
the square brackets and we'll make some
| | 00:36 | room to put some stuff in there.
| | 00:39 | And we'll start with a hash ref.
| | 00:42 | Now what we have here is an array ref,
and it's reference to an array of hash refs.
| | 00:51 | So there will be multiple
hash refs in this array.
| | 00:55 | Let's see in a moment how powerful this is.
| | 00:58 | Say name and we'll use Jimi again,
and his instrument is the guitar, and
| | 01:05 | his genre is rock.
| | 01:12 | And now we put a comma here
because we're going to do another one.
| | 01:16 | And rather than type all that again,
I'm just going to use this structure and
| | 01:22 | we'll go ahead and put somebody
else in. How about Miles Davis?
| | 01:25 | And his instrument is the
trumpet and his genre jazz.
| | 01:32 | We'll go ahead and put in another one,
say Ella Fitzgerald, and hers is vocal
| | 01:42 | and her genre is jazz.
| | 01:43 | And you see what we end up with here.
| | 01:49 | I will go ahead and maximize this.
| | 01:51 | Is you have basically a list of
artists, and yet it's a rich list.
| | 01:57 | In each element you have multiple named elements.
| | 02:01 | This is a very common structure.
| | 02:03 | You'll see this used a lot.
| | 02:04 | I am going to show you an example
from our examples here in this course.
| | 02:10 | If I want to take a look at this list,
go ahead and print it out. We'll use a
| | 02:17 | foreach loop, and we dereference it
with the at sign, because it's a list.
| | 02:27 | So, remember up here at the top,
the list is called artists.
| | 02:32 | Now we're using a dollar sign because
a scaler contains the reference, but
| | 02:38 | we want to dereference it
and use it as the array.
| | 02:42 | So we dereference it like that by
putting the at sign in front of the dollar
| | 02:46 | sign, and then it works just like
any other array and we'll message.
| | 02:51 | We're going to use a new technique.
I'm going to dereference each of the
| | 02:55 | elements separately all within the quotes.
| | 02:58 | So artist is the actual hashref,
because each element of this array is a hashref.
| | 03:07 | So I dereferenced the individual
element of the artist hashref with the
| | 03:12 | dereference operator followed by the
curly braces and I can say name like that,
| | 03:17 | and say instrument, and genre, and it
will just go ahead and it will print all
| | 03:28 | of those elements out.
| | 03:29 | So I'll save this and I'll run it.
| | 03:32 | And there we have our list of
artists in the order that we entered them.
| | 03:39 | If I want to I can actually add
another artist on the fly here.
| | 03:43 | Use the push function, which
pushes a new element on to an array.
| | 03:47 | And the new element that we'll push on
is actually going to be a hash ef.
| | 03:54 | So we'll use an anonymous hashref here
by just putting it in the curly braces,
| | 03:58 | and I'll say name is say Yoyo Ma,
instrument is the cello, genre is classical.
| | 04:14 | And I can save that and run it and
now we have four elements in the array.
| | 04:22 | So in fact it's very common to declare
an empty array ref and then just start
| | 04:27 | pushing hash refs in with
this sort of a construction here.
| | 04:33 | We'll show you an example of that
in our RandomQuote application.
| | 04:39 | Go ahead and maximize this, and up here at
the top you see I declare an empty array ref
| | 04:49 | and it's called quotes.
| | 04:51 | And then in the readQuotes,
I call a function called newQuote and
| | 05:01 | that's declared down here.
| | 05:04 | And in newQuote, I push an anonymous
hashref into the array that's pointed at
| | 05:09 | by the array ref quotes.
| | 05:11 | And then I returned the element
that has that anonymous hashref.
| | 05:18 | And so that gets used up here.
| | 05:22 | Here's the newQuote call and so what
we get here is the hashref that's been
| | 05:27 | allocated in newQuote, and then I go
ahead and I start putting things in it.
| | 05:33 | So in effect building a structure like
this, but I'm just building it on the fly.
| | 05:41 | And it's incredibly convenient.
You have to be able to visualize it.
| | 05:47 | It takes a little bit of abstract
thought to be able to see this, but once you
| | 05:52 | see it and once you start using it,
it's incredibly powerful and it allows you
| | 05:57 | to do something like this application,
which is a complete application that does
| | 06:01 | everything that you need it to do and
it works and we can run it here and it
| | 06:06 | gives us random quotes every time we run it.
| | 06:09 | You can build that in very few lines of
code by using powerful tools like this.
| | 06:14 | And this is I think one of Perl's most
powerful paradigms, to be able to build
| | 06:19 | these data structures on the fly like that.
| | 06:21 | So that is the power of mixed data
structures and references and that's really
| | 06:30 | a lot of what references are good for in Perl.
| | Collapse this transcript |
|
|
12. File I/OUnderstanding streams and files| 00:00 | When we talk about File I/O, it's important
to understand two distinct types of files.
| | 00:07 | You're probably used to looking at
files as being those things are on my hard
| | 00:13 | disk that I read from and write to, and
they have date in them, or web pages, or
| | 00:17 | documents or something like that.
| | 00:19 | In fact, from the perspective of the
computer program that you're writing, a lot
| | 00:24 | of things are files.
| | 00:25 | There are two basic
different categories of files.
| | 00:30 | One is the blocks of data
that you used to thinking about.
| | 00:33 | And these are like the files that
are on your hard disk, and the other is
| | 00:37 | something called Streams.
| | 00:40 | A block of data is simply a chunk of a file
that can be read or written to in one operation.
| | 00:47 | A stream is data that may
come as a series of bytes.
| | 00:53 | This could be keystrokes from the user.
| | 00:55 | This could be data sent over a network.
| | 00:58 | It can be a lot of things.
| | 00:59 | The point is that from the computer's
perspective that's a different kind of a file,
| | 01:04 | but the operations that you will
perform in your code for how to read it
| | 01:10 | and how to write it are substantially the same.
| | 01:14 | There are differences in how files are
read and written whether they are streams
| | 01:18 | or whether they are blocks, but for the
most part, the basic way that you deal
| | 01:23 | with them are the same.
| | 01:24 | This is an important key concept to
understand as we go into talking about files
| | 01:28 | for the rest of this course.
| | 01:30 | So understand this, that
files can be a lot of things.
| | 01:34 | You're used to thinking of them as
being files in a file system on a hard disk.
| | 01:38 | Just keep in mind that a lot of
different types of things are files, and that
| | 01:42 | Perl like most of programming languages,
especially those that have their roots
| | 01:46 | in UNIX tend to deal with all these
different types of things in the same way.
| | Collapse this transcript |
| Using file handles| 00:00 | When you read or write to files
in Perl, you tend to do this using
| | 00:05 | something called a file handle, which is a
different type of a variable in the Perl language.
| | 00:11 | So we're going to look at
file handles in this lesson.
| | 00:14 | We'll start by making a working copy of
files.pl, and we'll call this one handles.pl.
| | 00:22 | We'll go ahead and open that up, and
here you see we have a little bit of
| | 00:26 | code that opens a file, and prints it
out and closes the file, and so this
| | 00:32 | uses the open function call.
| | 00:34 | This variable here is a file handle.
You'll notice that it has no dollar sign in
| | 00:39 | front of it, or anything
like that. It's not a scalar.
| | 00:42 | It's not an array.
It's a file handle.
| | 00:44 | They tend to be written in all capital
letters, when they are in this forum.
| | 00:49 | And you'll see that it's using a couple
of different places here throughout this
| | 00:54 | little piece of code.
| | 00:55 | And so the open function call uses
a file handle as its first argument.
| | 01:00 | Its second argument can be a file mode,
or if you skip that it can just be the
| | 01:06 | file name, so a little bit confusing,
so I always include the file mode.
| | 01:10 | It's just easier to remember that way.
| | 01:12 | And in the event that the "open" is not
successful, we have this "or" condition,
| | 01:17 | and we'll print out an error.
| | 01:19 | And in the error, this is important to
notice, we have this $!, which will give
| | 01:25 | us the error message from the operating system.
| | 01:27 | So, the first thing we're going to do
is we're going to make this error happen,
| | 01:29 | because we're opening the file that
does not exist in this directory, and
| | 01:33 | you'll see the directory listing over
here does not have workingfile.txt.
| | 01:37 | We'll make that in a moment.
| | 01:39 | And so this will fail and
we'll get an error message.
| | 01:42 | So we'll go ahead and we'll save this
and we'll run it and we'll see we'll get
| | 01:47 | the error message there.
| | 01:48 | It says "cannot open file", because
that's the message that I put here.
| | 01:52 | And it has this $!, which gets
interpolated and gives us the error message that
| | 01:59 | says "No such file or directory".
| | 02:01 | So that makes it really clear what's wrong
as we don't have that file in our directory.
| | 02:05 | So let's make a working
copy of linesfile.txt here.
| | 02:09 | And I'll call it workingfile.txt,
and then when we run this code again,
| | 02:16 | and there we go.
| | 02:19 | We've got the five lines of text that
are our working file. Open that up and
| | 02:23 | you see it's got these five lines of text in it.
| | 02:27 | And that's the result that we get.
| | 02:30 | Here we're using the file handle to
read from it and this is the simple form
| | 02:36 | that's commonly used to read all the
lines from a file, print while file handle.
| | 02:41 | Putting the file handle in angle
brackets like that in a list context, it will
| | 02:46 | give you all of the lines of the file,
or in a scalar context, it will give
| | 02:50 | you one line at a time.
| | 02:52 | So for example, I could write this the
other way around and I could say while
| | 02:59 | my $line equals file handle, so
that's very clearly a scalar context.
| | 03:06 | And I can say print $line like that.
| | 03:10 | And if I save that and run it, it will give us
exactly that same result that we just had there.
| | 03:16 | The shortcut way to do it is the way
that I had it before, which is simply print
| | 03:22 | while file handle, like that, and a semicolon,
and save and run, and we get that same result.
| | 03:32 | This is commonly the way that you're
doing it unless you have a lot more things
| | 03:35 | to do in your loop than print.
| | 03:37 | Another common thing to do with a
file handle of course is to write to it.
| | 03:42 | So let's write some quote that will
create a new file and we're going to open
| | 03:48 | another file. We'll call it new file handle.
| | 03:52 | This one will be for writing. So we have
the right angle bracket for writing;
| | 03:56 | we have the left angle bracket for reading.
| | 03:58 | You can look at it as reading from the file,
because it's pointing away from the file.
| | 04:03 | And in this one you can look at it
as writing to the file, because it's
| | 04:05 | pointing towards the file, and you can
say newfile.txt, or error "cannot open
| | 04:13 | file for write, so we know
which one it is, with the bang, and we can
| | 04:19 | call this one "for read".
| | 04:22 | If we had our file names in variables,
so we could put the filename in there
| | 04:26 | instead, but this will work for now.
| | 04:28 | And we want to make sure that
we close it when we're all done.
| | 04:30 | So we'll put a close down here, close NFH.
| | 04:37 | Now we want to read from the old
file and write to the new file.
| | 04:41 | This will look like this.
| | 04:42 | print new file handle while file handle, like that.
| | 04:49 | And so it's just as simple as that.
It looks like I'm missing a semicolon over
| | 04:55 | here, and there we have it.
| | 04:57 | When we save and run this, you'll
notice we don't get anything to the console
| | 05:02 | here, because we didn't
print anything to the console.
| | 05:06 | Instead, if we refresh our file system,
and I'll just right-click on this in
| | 05:10 | Eclipse and click on Refresh, you'll
see we have a new file here. If we open that,
| | 05:15 | it has exactly the same
content as the workingfile.txt.
| | 05:20 | So we have effectively copied one
text file to another text file just by
| | 05:26 | reading the lines in this while loop,
and writing them to the new file.
| | 05:30 | So, this is how you handle the
standard, what I called the old-style
| | 05:35 | file handles in Perl.
| | 05:37 | For a simple operation like you're
just copying from one file to another or
| | 05:42 | you're just reading lines of text from
one of the files and maybe doing some
| | 05:46 | regular expressions on them or
something like that, this will work fine.
| | 05:50 | For a lot of others purposes, it's
useful to use the modules that come with
| | 05:55 | Perl for the object-oriented file
interface, and we're going to look at that in
| | 06:00 | our next lesson.
| | Collapse this transcript |
| Using the object-oriented interface for handling files| 00:00 | The most common way to do File I/O in
Perl is to use the object-oriented method,
| | 00:06 | using the IO::File module.
| | 00:08 | So let's take a look at how we do that in Perl.
| | 00:12 | Go ahead and make a working copy of files.pl.
| | 00:14 | We'll call this one oofiles.pl,
object-oriented. We'll open that.
| | 00:21 | In order to use the IO::File module,
you have to first use the use statement to
| | 00:26 | import the module into the Perl script.
| | 00:31 | Then we can go down here and create our object.
| | 00:34 | So, this is an object-oriented interface.
| | 00:36 | So that means that we have to
create an instance of an object.
| | 00:41 | Objects in Perl are held in reference variables.
| | 00:44 | So we say my $fh =, and then we call the
object constructor from the module, and
| | 00:51 | that looks like this, IO::File, and
then the dereference operator and new.
| | 01:00 | Inside the parentheses of the new
function call, we put the name of the
| | 01:05 | file, workingfile.txt, and the open mode,
in this case a lowercase r for read-only mode.
| | 01:14 | Then we can do the same or error, in
this case, because I don't want it to go off
| | 01:19 | the end of the screen, I'm just going
to put the error call on the next line.
| | 01:23 | I like to leave the OR hanging when I
do this, so that it reminds me as I'm
| | 01:26 | looking at it later that I've got
something on the next line that's still
| | 01:30 | part of this statement.
| | 01:32 | Kind of hangs there instead
of the semicolon to remind me.
| | 01:36 | I'll put in here, cannot open file ($!).
| | 01:41 | We can use exactly that
same error message and the $!
| | 01:45 | will expand to the operating system message.
| | 01:48 | We'll see that in a moment, because
we're going to run this before we actually
| | 01:51 | create this workingfile.txt.
| | 01:53 | Of course, I don't need that anymore.
| | 01:55 | I actually don't need the close anymore.
| | 01:58 | We'll talk about that in a moment.
| | 01:59 | Instead of this FH here,
we're going to use the $fh.
| | 02:03 | In Perl, an IO::File object can be
used exactly like a file handle and
| | 02:09 | that's why I named it $fh.
| | 02:12 | Close is not necessary here, because
with the object-oriented interface,
| | 02:16 | whenever this object variable goes
out of scope, the object instance gets
| | 02:21 | destroyed, which triggers
the object's destructor method.
| | 02:25 | In the destructor method for
IO::File, the file actually gets closed.
| | 02:30 | So the upshot of all of that is that
when the File Handle variable, the object
| | 02:37 | variable goes out of scope,
the file is automatically closed.
| | 02:41 | So, we don't have to do that manually.
We don't have to remember to do that.
| | 02:43 | If we forget to do it, obviously
there is very little impact, because it
| | 02:47 | happens automatically.
| | 02:49 | So this structure here works exactly like
it does with a regular Perl file handle.
| | 02:54 | So we'll go ahead and we'll save this
and we'll run it and we'll get our error
| | 02:57 | messages first time there.
| | 02:59 | There's the error message and this is
getting expanded from the special $! variable.
| | 03:06 | That is the operating system error message.
| | 03:09 | Let's go ahead and make a copy of
this linesfile.txt and we'll call this
| | 03:14 | one workingfile.txt.
| | 03:17 | Now that file exists, so if we run this
again, we will get the output that we expect.
| | 03:25 | 1, 2, 3, 4, 5, this is line 1, 2, 3, 4, 5,
and that's from this workingfile.txt.
| | 03:33 | So that's how you read a file using
the IO::File object-oriented method.
| | 03:39 | It's actually a little bit
more common to do it this way.
| | 03:41 | I'm going to show you this using the
object-oriented interface from the file
| | 03:46 | handle, rather than the default file handle
operator here with the little angle brackets.
| | 03:51 | That's a more common construct
for the built-in file handles.
| | 03:55 | When you're using the object-oriented
method, it's more commonly done this other way,
| | 03:59 | which I will show you right now.
| | 04:01 | And that's using something like this,
while my $line = $fh, and then the
| | 04:10 | dereference operator to
get at the object methods.
| | 04:14 | In this case, we're going
to use the getline method.
| | 04:19 | We'll go ahead and put this in a block
and we'll say print $line, like that.
| | 04:26 | So this is a much more common way to
do this with the object-oriented method,
| | 04:30 | because you're using the methods of the
object-oriented interface here, rather
| | 04:35 | than the default file handle.
| | 04:37 | This opens up all of the power of the
methods inside of that object module.
| | 04:43 | So I'll go ahead and save that and run it and
we'll see that it does exactly the same thing.
| | 04:48 | Yet in the long run, this is going to give
us a lot more flexibility to do it this way.
| | 04:52 | Now, let's take a look at how you would
copy a file, so you can see file writing
| | 04:57 | as well as file reading.
| | 04:58 | I'm just going to make a copy of this
constructor line and make the changes I
| | 05:03 | need for opening a file for write.
| | 05:06 | So I'm going to give it a different
variable name for the object variable.
| | 05:10 | And the file name here will be newfile.txt.
| | 05:14 | Instead of read method, we're
going to use the write method.
| | 05:19 | Then we will differentiate our error
messages, so we know which file is giving
| | 05:23 | us an error, should we get one.
| | 05:24 | I'll just say here for write, and
we'll say over here for read, so that we know.
| | 05:33 | Now we've opened a file for reading and
we've opened another file for writing.
| | 05:38 | Now when you open a file for writing,
if the file doesn't exist, it gets
| | 05:41 | created automatically. So that's good.
| | 05:46 | Then down here all we need
to do is change this one line.
| | 05:51 | Instead of saying print $line like that,
we're going to use the print method
| | 05:56 | from the new file handle variable.
| | 05:58 | So let's say $nfh and the
dereference operator and then the print method.
| | 06:05 | That's a method.
| | 06:06 | it's a function inside of the object
interface and we'll put this $line in
| | 06:12 | parentheses, because that is not
a built-in function call there.
| | 06:17 | That's really all there is to it.
| | 06:19 | We're now copying a file.
| | 06:20 | So we'll go ahead and we'll
save this and we'll run it.
| | 06:25 | You see nothing comes in the output
here, because it's going to file, so if I
| | 06:28 | refresh over here in Eclipse's
little file browser, we'll hit refresh there,
| | 06:33 | we'll see there is the newfile.txt and it
has all of the lines from the workingfile.txt.
| | 06:41 | So that is how you use the IO::
File object-oriented interface.
| | 06:46 | It's actually a lot easier to do
and a lot easier to remember than the
| | 06:51 | built-in file interface.
| | 06:53 | It's a lot more flexible.
| | 06:55 | In the long run, you'll find that there
is a lot more that you can do with it.
| | 06:58 | I recommend that you take a look
at the documentation for IO::File.
| | 07:03 | It's a little bit surprising when you
bring it up here that there's not much in
| | 07:08 | this particular documentation file at all.
| | 07:12 | The reason for that is
explained right up here at the top.
| | 07:16 | IO::File inherits from
IO::Handle and IO::Seekable.
| | 07:20 | It extends these classes with
methods that are specific to file handles.
| | 07:25 | In other words, most of the documentation
you're going to find in here, IO::Handle.
| | 07:32 | So I'm going to press the Copy command
on my keyboard and paste it up here so we
| | 07:37 | can search for IO::Handle.
| | 07:38 | When we bring up the documentation
for IO::Handle, this is actually where
| | 07:44 | you'll find most of the methods that
are available in the object-oriented
| | 07:48 | interface in IO::File.
| | 07:50 | The reason for that is that there
are many things that work like files.
| | 07:55 | The socket interface for connecting
to remote systems, the standard IO
| | 08:00 | interface, a lot of different things
work exactly the same and they all inherit
| | 08:05 | this IO::Handle module.
| | 08:07 | So this is where you will get
the methods that you'll use for
| | 08:11 | manipulating files.
| | 08:14 | So, that is our IO::File example and
you can see you create an IO::File object
| | 08:21 | with the new method.
| | 08:22 | The object is kept in a standard scalar
variable and you specify the file name
| | 08:29 | and the read or write mode, just like that.
| | 08:32 | You can read line-by-line using getline,
and you can write to a file using the
| | 08:38 | print method with the file handle.
| | Collapse this transcript |
| Working with binary files| 00:00 | Some operating systems draw a
distinction between binary files and text files.
| | 00:06 | It's important to understand how this
works, especially if you're working with
| | 00:10 | an operating system that has those
distinctions where you expect your code to
| | 00:14 | run on an operating system
that has those distinctions.
| | 00:18 | The most prominent operating system
that has a distinction between binary files
| | 00:23 | and text files is Microsoft Windows.
| | 00:25 | In fact, all Microsoft Windows all the
way back to MS-DOS have that distinction
| | 00:30 | between text files and binary files.
| | 00:32 | The upshot of this distinction means
that with a text file the operating system
| | 00:38 | will try to interpret and convert line endings.
| | 00:42 | With a binary file, of course
it will leave everything alone.
| | 00:46 | If you try to read or write a binary
file using the methods for a text file,
| | 00:52 | it will try to convert the line endings.
| | 00:56 | Your binary file will be corrupted.
| | 00:58 | So we're going to show you an example
of this and how to do it wrong and what
| | 01:01 | happens and how to do it right.
| | 01:04 | So what we have here is a binary file.
| | 01:07 | It's a JPEG image and if we open this up,
you'll see it's a picture of some olives.
| | 01:11 | So, we'll go ahead and we'll make a working
copy of files.pl and we will get to work on this.
| | 01:19 | We'll call this binary.pl. Open that up.
| | 01:23 | We're going to be using the IO::File module.
| | 01:27 | So we'll go ahead and say use IO::
File and we're going to go ahead and make
| | 01:32 | a copy of this file.
| | 01:34 | So, we're not going to use any of this code.
We'll go ahead and say the original
| | 01:41 | file name is olives.jpg.
| | 01:45 | The new file name, we'll just call it copy.jpg.
| | 01:54 | We will open the original
file using a file handle.
| | 02:00 | This is an object reference variable, IO
::File->new, and we'll give it the file
| | 02:07 | name and we will open it for read.
| | 02:11 | We'll put out an error
message if we can't open it.
| | 02:24 | Now, I'm just going to make a copy of
these two lines of code and just change
| | 02:32 | the original file to the new file.
| | 02:39 | I'm going to change read to write and
come over here and change original to new.
| | 02:46 | That should work just fine.
| | 02:48 | Now we're going to copy the file.
| | 02:49 | First thing we need to do is we
need to create a buffer for the data.
| | 02:52 | This is how you copy a binary file.
| | 02:54 | In other examples, we've copied text
files and now we are copying a binary file.
| | 02:59 | So the method of doing this
is a little bit different.
| | 03:02 | I'm going to use the read method in the IO::
File object and that requires a buffer size.
| | 03:10 | So we'll just create a
variable up here for the buffer size.
| | 03:14 | I'll call it bufsize and I'll
make it a megabyte, so 1024 * 1024.
| | 03:20 | Most computers these days can
handle a megabyte buffer size in memory.
| | 03:26 | So, we'll use a while loop and the
read method from the original file handle.
| | 03:35 | This is the file handle that we created up
here for the original file where we opened that file.
| | 03:40 | That's the olives.jpg.
| | 03:42 | The method read will read a file
using the buffer in the binary file mode.
| | 03:49 | We give it the buffer as the first
argument and the size as the second
| | 03:56 | argument, like that.
| | 03:59 | So for each buffer-ful, we're going
to write the buffer, and we write the
| | 04:03 | buffer, simply using the print method.
| | 04:09 | This will read and write the binary
file and this will work great on operating
| | 04:13 | systems that do not have a
distinction between binary files and text files.
| | 04:18 | So if you're doing this on a Mac or
if you're doing this on a UNIX system,
| | 04:21 | this will work fine. We're on a PC.
| | 04:23 | It's not going to work.
| | 04:25 | So we'll go ahead and we'll
save this and we'll run it.
| | 04:30 | Just so we know when we're done, let's go
ahead and put out a message when we're done.
| | 04:33 | I like to know when I'm done.
| | 04:35 | So nothing else is happening to the
screen here, so I'll just put message
| | 04:39 | "Done" at the end there,
so we'll now save and run.
| | 04:42 | We have the little message Done.
| | 04:44 | So I'm going to refresh this
because Eclipse doesn't refresh it.
| | 04:48 | There is our copy and
I'll go ahead and open that.
| | 04:51 | It says "Windows Photo Viewer cannot
open this picture, because the file appears
| | 04:55 | to be damaged, corrupted or is too large."
| | 04:58 | That's not a very specific error
message, but we get the picture.
| | 05:01 | The original file is fine,
but the copy is not working.
| | 05:05 | The reason for that is that the operating
system tried to translate the new lines.
| | 05:10 | It didn't realize that this was a
binary file and Windows file system
| | 05:16 | defaults to text files.
| | 05:18 | So what we need to do is we need to tell it
that it's a binary file and tell it not to do that.
| | 05:22 | We need to do that in both the original
file and the new file and we do this like this.
| | 05:37 | What that does is it sets
that file handle to binary mode.
| | 05:43 | We'll do the same thing to the new file.
| | 05:51 | We will save that and run it again.
| | 05:53 | Now we have our Done message and we
open up the copy, voila! It works.
| | 06:00 | So that's the copy and that's the
original and they are the same file.
| | 06:05 | So, this is the important bit of code
right here for working with binary files,
| | 06:11 | even if you're running on a system
that does not make this distinction.
| | 06:15 | It doesn't hurt, it doesn't cost anything.
| | 06:17 | It's always a good idea to go ahead and set
your binary mode and use this string here, raw.
| | 06:26 | Now, in Perl, if you call binary mode
without any arguments at all, it will
| | 06:30 | assume raw and it will go
ahead and set it to raw.
| | 06:33 | It's a good idea though for future
compatibility, because you never know, to
| | 06:38 | just go ahead and always
put that string in there.
| | 06:41 | So you'll see code a lot that just
calls bin mode without any argument.
| | 06:45 | You know that that's setting it to raw.
| | 06:47 | But for your code, it's a really
good idea to go ahead and include that.
| | 06:51 | So just to recap here, we open the
file in the same way we open it using the
| | 06:57 | IO::File object-oriented.
| | 07:00 | We set the bin mode on both the old
file and the new file and we copy it using
| | 07:08 | the read method and the print method.
| | 07:11 | The read method takes as an
argument the buffer and the buffer size.
| | 07:15 | Of course, the print method
will output whatever you give it.
| | 07:19 | It outputs that to the new file handle.
| | 07:21 | So we've got the new file handle for the write.
| | 07:23 | We have the original file handle for the read.
| | 07:27 | That is our copy code right there,
to copy a binary file on any operating
| | 07:33 | system, including an operating
system like Microsoft Windows that draws a
| | 07:39 | distinction between binary files and text files.
| | Collapse this transcript |
|
|
13. Built-In FunctionsManipulating strings with built-in functions| 00:00 | Perl has a number of built-in
functions for dealing with strings.
| | 00:04 | Let's take a look at some of them.
| | 00:06 | We'll start by making a working copy of
builtins.pl. We'll call this strings.pl.
| | 00:11 | Go ahead and open that up and
we'll start by declaring a string.
| | 00:21 | And the first function I want to show you
is one called chomp and what this does
| | 00:26 | is it takes the new line off the end of the
string, so we'll just put a new line here.
| | 00:30 | A lot of times when you read a string
out of a file, it will come with new lines
| | 00:36 | attached to it which is normal and if
it has that new line on it, you can use
| | 00:42 | the print function and just say
print $string and it'll print just fine.
| | 00:47 | Save this and run it and
you'll see it prints the string.
| | 00:52 | If we were to print another line after
it, let's say we print the string again,
| | 00:55 | we'll see that the second string is on
a separate line and that's because this
| | 01:02 | new line is the end of it.
| | 01:03 | If this new line wasn't here and we
were to run that, you'll see that those
| | 01:08 | strings were just run together.
| | 01:10 | So the new line is useful and yet a lot
of times, like for instance, if we were
| | 01:15 | using the message function that we've
been using throughout this course,
| | 01:21 | it's more convenient not to have it or if
you're processing your strings or putting
| | 01:25 | them in array and joining them and doing
different things, you might want to get rid
| | 01:29 | of a new line that's already there.
| | 01:31 | So if I have this new line here and
let's say for example printing this in the
| | 01:37 | middle of some other characters.
| | 01:39 | Like for instance if I just put brackets
around it for illustration purposes and
| | 01:46 | put string in there, you'll see that
when I save this and run it that, that
| | 01:52 | that second bracket comes out on the next
line and we might not want this to happen.
| | 01:56 | So Perl has a built-in function for
dealing with this and it's called chomp and
| | 02:01 | what chomp does is it simply takes
the new line off of the end of a string.
| | 02:07 | We'll save that and run it.
| | 02:09 | We see that now the new line is missing.
| | 02:11 | If you try to chomp the end of a
string that doesn't have a new line,
| | 02:15 | it will leave it alone.
| | 02:17 | So we'll do this again with a string
that we already know has the new line
| | 02:21 | missing and you'll see that it
doesn't take anything else off of that.
| | 02:25 | Chomp is an intelligent version of an
older function called chop, which if we're
| | 02:30 | going to use chop instead of chomp,
it would go ahead and take yet another
| | 02:34 | character off of the end of this and
that is not what we want to have happen, so
| | 02:38 | we use chomp instead of chop.
| | 02:41 | There are some other interesting
string functions that you might want to try
| | 02:47 | and we'll declare a second variable here and
we'll use that variable for printing out $s2.
| | 02:56 | And let's say we wanted to get the
uppercase of a string and use the uc
| | 03:02 | function for uppercase.
| | 03:04 | If we save that and run it, you'll see
that the string is now all in uppercase.
| | 03:09 | Or conversely, if we wanted to make
it lowercase, you can use lc to make it
| | 03:15 | lowercase and this is sometimes very useful.
| | 03:19 | As long as we're talking about useful,
one of the most useful functions that
| | 03:23 | you'll find in Perl for dealing with
strings is the substring function and there
| | 03:28 | is actually a set of
functions that work well together.
| | 03:32 | Substring and the first argument is
the string that you're going to be taking
| | 03:38 | a subset of and then the second
argument is where you'll start the substring
| | 03:44 | and it's zero based.
| | 03:45 | So if we wanted to just get these two
letters here, we would count 0 for the T
| | 03:50 | and 1 for the h, 2, 3, 4,
so that would be 5 and 6.
| | 03:55 | We would start at 5 and we would take 2
letters and that's the way this works.
| | 04:00 | The first one is the starting place
and the second one is the length of the
| | 04:03 | substring and then s2 will just say is.
| | 04:07 | We'll save that and run and
we just have the letters is.
| | 04:11 | And if we made this 4 letters long,
we would get "is a" because we also get the
| | 04:17 | space that counts as one, and we have
"is a." So that's the substring function.
| | 04:23 | If we want to find out where a string
is within another string, we can use
| | 04:29 | the index function.
| | 04:31 | For example, if we wanted to find the
first occurrence of the letter s in this
| | 04:38 | string, we would just say this will
find the first occurrence of the letter s.
| | 04:45 | If we save this and run it, we see that
the first occurrence is at index number
| | 04:50 | 3 and again, counting from zero, 0, 1, 2, 3.
| | 04:55 | If we wanted to find the last
occurrence of the letter s, we would use rindex,
| | 04:59 | which does the same thing as index,
except it starts at the end and works
| | 05:03 | backwards and so if I save and
run, we'll see that that's at 10.
| | 05:07 | So 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
| | 05:14 | So that's the 10th one, counting
from the left and starting at 0.
| | 05:18 | And the beauty of this, they're all
being zero-based together like this, is that
| | 05:22 | we could actually stack them together.
| | 05:24 | We could do something like this.
| | 05:26 | Substring of $string starting at this
place and then if you don't give substring
| | 05:34 | another argument, it just goes to
the end of the line and so here without
| | 05:39 | actually sitting here and counting,
we can say we just want the string starting
| | 05:44 | at the last s and going to the end.
| | 05:46 | So we'll get the word string and
remember we have the rindex, which starts at
| | 05:50 | the end and counts back and it finds
the last occurrence of the letter s and
| | 05:56 | using what that returns as the
second argument to substring,
| | 06:02 | the first argument being the string, then
we're able to just get that part of the string.
| | 06:07 | So if we save this and run it,
we'll see that we get the word string.
| | 06:12 | So those are some of the most common and
powerful string functions available in Perl.
| | 06:17 | There are a few others, so it's worth
looking at the documentation for this.
| | 06:22 | If we bring up the Perldoc and
we search for perlfunc, func,
| | 06:30 | that will give us a complete list of all of the
built-in functions in Perl and they're categorized
| | 06:38 | by individual functions.
| | 06:40 | And then if we want to look one of them
up, like for instance this one here,
| | 06:44 | we can simply copy and paste into the
search box there and we can get the
| | 06:50 | documentation for that individual function.
| | 06:53 | So those are the built-in functions
for strings or the most common and
| | 06:59 | powerful ones anyway and
that's how they work in Perl.
| | Collapse this transcript |
| Working with numbers| 00:00 | Perl has a small number of
functions for dealing with numbers.
| | 00:03 | Numbers are not really the
strength of this programming language.
| | 00:07 | This programming language was
designed for dealing with text and it does
| | 00:10 | that very, very well.
| | 00:12 | While some scientific and mathematical
applications have been written using Perl,
| | 00:16 | they're typically done using libraries
are not using the built-in functions.
| | 00:20 | Perl has a limited number of functions
for dealing with numbers and we'll look
| | 00:24 | at some of those right now.
| | 00:25 | We'll start by making a working copy of
builtins.pl and call this one numbers.pl.
| | 00:32 | I'll open that.
| | 00:36 | One function that Perl has for dealing
with numbers is to get the integer of a
| | 00:40 | number, so we'll declare a number
and we'll call it 123.456 and it's got a
| | 00:47 | fractional part there.
| | 00:49 | So we'll say my $n2 = int ($number) and
we'll go ahead and print that, $n2, save
| | 01:04 | and run and you see we have
the integer part of the number.
| | 01:09 | Perl has some functions for
converting from other bases, like for instance
| | 01:13 | converting from hexadecimal.
| | 01:15 | If we had a number here that was
represented as a string and that string was a
| | 01:22 | hexadecimal number, say a5, then you
can use the hex function to convert that
| | 01:29 | into an integer and then display that
and it will of course display in base 10.
| | 01:33 | We'll save this and run it
and there we have a5 in decimal.
| | 01:40 | Likewise, if we have an octal number,
we can convert from octal and of course,
| | 01:46 | this is not a valid octal number but
75 would be a valid octal number and we
| | 01:52 | save and run and there's that
number converted to decimal.
| | 01:57 | Perl also has a function for
generating random numbers and it'll use your
| | 02:01 | hardware's random number generator, if
your hardware has one and we'll just get
| | 02:07 | rid of this n2 variable for now and use
the number variable and we'll say rand()
| | 02:15 | and save that and run it and so this
gives us a random number and you'll notice
| | 02:22 | that this random number is less than 1.
| | 02:25 | If you wanted to be scaled differently,
you can put in a number here, say 100, and
| | 02:31 | it will give you a random
number scaled up to 100.
| | 02:34 | I'll save and run and so we'll get 86.
| | 02:37 | If we run it again, we get a different number.
| | 02:41 | Run it again, we'll get a different number.
| | 02:43 | So these are all under 100 and if you
want of course just the integer part of that,
| | 02:47 | you could wrap this random
function in an integer function and get just
| | 02:52 | the integer part of that, there's 10, 17,
so these are all integers under 100.
| | 03:01 | You can also seed your random number
generator by using srand and that allows
| | 03:07 | you to give it a seed and what this
does is it gives the random number
| | 03:10 | generator a starting place.
| | 03:12 | So if you seed it with a constant
number, it will always give you the
| | 03:16 | same random number.
| | 03:17 | For instance, if we seed it with say
125 and save that and run it then that
| | 03:24 | random number comes up as 2 every time.
| | 03:27 | So sometimes what I'll do is I'll seed
it with some combination of the process
| | 03:32 | ID plus the time and maybe something
else and then every time I run it,
| | 03:40 | I'm guaranteed to get a different
number and there we have that.
| | 03:46 | So those are some of the
number functions that Perl supports.
| | 03:49 | There are a few more.
| | 03:50 | There are a limited number of
mathematical functions like cosines and tangents
| | 03:54 | and things like that.
| | 03:56 | You can find those in the documentation.
| | 03:59 | If you run Perldoc and type in
perlfunc, you get a complete list of the
| | 04:08 | built-in functions that are available
in Perl and they are categorized down
| | 04:13 | here and there's the list of
numeric functions and you can look them up
| | 04:19 | individually in Perldoc.
| | Collapse this transcript |
| Manipulating lists and arrays with built-in functions| 00:00 | Perl has a number of functions for
dealing with arrays, and we'll take a look
| | 00:04 | at those, right now.
| | 00:06 | Arrays and Lists are really just two
different names for the same thing.
| | 00:10 | More often than not the term, List is
applied to the literal lists, and the term
| | 00:16 | Array is applied to the Variables.
| | 00:18 | We'll make a working copy of the builtins.pl
here, and we'll call it arrays.pl. Open that up.
| | 00:26 | We'll start by showing you a nice function
that's a great shortcut for defining an array.
| | 00:32 | We'll say my @list = qw and parentheses,
you can think of it is standing for code word.
| | 00:42 | What it does is it lets you put a
bunch of literal words in parentheses or
| | 00:46 | really any delimiter, the
parenthesis are used commonly.
| | 00:50 | Those words are then put into
an array if they were strings.
| | 00:54 | For example, I can just say Jan, Feb,
March, April, May, June, July, August,
| | 01:03 | September, October,
November and December.
| | 01:07 | I think that's all of them.
| | 01:10 | This will create an array that will
have all of those strings in it, so we go
| | 01:16 | ahead and print the join of that.
| | 01:18 | Join is another wonderful
function for dealing with arrays.
| | 01:25 | What join does is it takes two arguments.
| | 01:27 | The first argument is a delimiter and
the second argument is the array itself.
| | 01:31 | Then it will take each of the
elements of that array and join them together
| | 01:34 | with that delimiter.
| | 01:35 | That will allow us to print this
out in a way that we can see it.
| | 01:40 | So, we'll save this and run it.
| | 01:42 | There's all of our strings
delimited by these colons here.
| | 01:48 | So, that's a great way to
define a list using the qw operator.
| | 01:54 | Let's say we wanted to add
something to the end of the list.
| | 01:56 | Let's say we made up a new
month, and we call it Foo.
| | 02:00 | So we can use the push function for
adding something to the end of a list.
| | 02:06 | So, push will just push that
on to the bottom of the list.
| | 02:10 | If I save this and run it, you'll see
we have a new month here called Foo.
| | 02:15 | Likewise, if we wanted to pop one off the
bottom, push and pop to the opposite things.
| | 02:20 | So, if I say my $string =
pop and the name of the list.
| | 02:27 | Now, a lot of times with built-in
functions people don't put the parentheses
| | 02:32 | because they are built-in, Perl
is able to use them like this.
| | 02:35 | So, you can type this with
or without the parentheses.
| | 02:38 | If I would type it with the
parentheses, it will work exactly the same.
| | 02:41 | But pop is one of those ones
that all often just write this way.
| | 02:46 | So, now I have this string, which will
be the last element of the list, so I am
| | 02:49 | going to massage that one separately.
| | 02:51 | Then we'll go ahead and save that
and run it, and you'll see that I have
| | 02:55 | December up here, because
I messaged it separately.
| | 02:59 | That's the string that I popped off the list.
| | 03:02 | December is not over here.
| | 03:03 | it actually took it off of the list and
returned it to me, so I could have it in a string.
| | 03:08 | So, that can be really convenient
for keeping a stack of variables.
| | 03:12 | You can push on and pop them off.
| | 03:13 | They come often the reverse order that
you put them on and that can be really
| | 03:17 | convenient some times.
| | 03:19 | You can do the same thing at the
top of a list with shift instead.
| | 03:24 | So if I say shift instead of pop, it will take
January instead of December. We'll run that.
| | 03:30 | There we have January, and
January is not on the list.
| | 03:35 | Likewise, if we wanted to, we
can unshift and put it back.
| | 03:47 | We'll go ahead and message the join of the
list again, so we can see that it went back.
| | 03:57 | So, here we have shifted it off to the
top of the list and we printing it out.
| | 04:03 | We print the list without it.
| | 04:04 | Then unshift puts it back on
and it will print the list again.
| | 04:07 | So, we should get January,
and then this list like this.
| | 04:10 | Then we should get the whole list,
all put back together afterwards.
| | 04:14 | Sure enough there we have it.
| | 04:15 | There is January back where it belongs.
| | 04:18 | Finally, let's just go ahead and
take some of this stuff out here.
| | 04:26 | There we have our list, just save that
and run it, ensure that here we have our
| | 04:30 | complete list there.
| | 04:32 | If we want to print the list in
reverse order, we can say reverse.
| | 04:35 | What reverse does, reverses the function
that will actually return a copy of the
| | 04:39 | list in reverse order.
| | 04:41 | So, there will now be a copy of the list there.
| | 04:44 | Let's save that and run it.
| | 04:46 | In fact, if we wanted to instead, we
could say rlist = reverse of @list.
| | 04:55 | Then we could print list and we
could print rlist and see both of them.
| | 05:02 | Save that and run it.
| | 05:03 | You have the list in forward order,
and the list in reverse order.
| | 05:07 | Or if we wanted to, we could make a
sorted list by using the sort function.
| | 05:12 | We'll print this out as
slist there. save and run.
| | 05:18 | Now, we have a list
that's in alphabetical order.
| | 05:22 | That can be useful for a lot of
things that are not months of the year.
| | 05:26 | So, those are the list functions.
| | 05:27 | You can see that there is a good
assortment of them, pretty much everything that
| | 05:31 | you will need for dealing with lists in Perl.
| | Collapse this transcript |
| Retrieving and formatting time| 00:00 | Perl has a few functions for dealing
with time, mostly for finding out what the
| | 00:05 | date and the time is and
for handling those variables.
| | 00:09 | So let's take a look at
how those work here in Perl.
| | 00:13 | We'll start by making a
working copy of builtins.pl.
| | 00:16 | We'll call this one time.pl, and
we'll go ahead and open that up.
| | 00:22 | First we'll just go ahead and get the
time and we'll create a variable called
| | 00:26 | t = time, and time is a built-in function.
I don't actually need the parentheses.
| | 00:33 | That will go ahead and give me the
current date and time, and what it gives me
| | 00:39 | is you'll see it's just a number and
what this is, we'll save that and run it,
| | 00:45 | this is the number of seconds
since midnight on January 1, 1970.
| | 00:50 | That's called the epoch and that's the
epoch used in most modern operating systems.
| | 00:56 | There are some operating systems
notably older Macintosh before OS X that use
| | 01:02 | a different epoch.
| | 01:05 | So most of the time what you'll see
when you use this function is you'll
| | 01:09 | see that number of seconds it's January 1,
1970. Sometimes you may see something else.
| | 01:16 | But never fear, there is a
way to get the time out of this.
| | 01:19 | And so we go ahead and say my now =,
and I'll say localtime of t, and that will
| | 01:29 | give me a variable that will tell me
what the date and time is here and now.
| | 01:34 | I'll put that out and save that and run it.
| | 01:37 | And here we have this strangely formatted
Friday, March 12, at this date and the year.
| | 01:44 | It's got the year over here.
| | 01:45 | That's actually a standard UNIX-ism and
of course Perl's legacy is in UNIX and
| | 01:52 | so this is the kind of a date and time
| | 01:54 | that's often used in e-mail
messages and things like that.
| | 01:57 | This is just the way that somebody
formatted it one day and it stuck.
| | 02:01 | And so if we want it in a different format.
| | 02:04 | We can take a look at the manual page
for local time, go ahead and bring that up,
| | 02:10 | and you'll see here that in a scalar
context it gives us that string that we
| | 02:18 | saw but in a list context it
gives us this array of variables.
| | 02:26 | So I'm going to go ahead and I'm going
to press Command+C on my Mac or Ctrl+C
| | 02:31 | on a PC or whatever.
| | 02:33 | It will copy that into the Copy Buffer,
and I'm going to paste that in here,
| | 02:41 | like that, and now I
have all of these variables.
| | 02:45 | So I can print a string
with some of the stuff in it.
| | 02:50 | For example, let's say I want
the year and the month and the day.
| | 02:58 | I'm going to use the mday variable, which is
this one here which is the day of the month.
| | 03:06 | This is not actually going to
give us the result that we expect.
| | 03:09 | That would be too easy.
| | 03:11 | Go ahead and save this and
run and show you what we get.
| | 03:14 | A couple of things here that just look
wrong. The year is not 110, and the month
| | 03:18 | I happened to know is March, as we
saw in the last screen if you were
| | 03:23 | paying attention, and this
says that the month is 2.
| | 03:26 | Now there are reasons for this but
the bottom-line is that the year is the
| | 03:32 | number of years since 1900.
| | 03:34 | So I have to add 1900 to the year.
| | 03:37 | So I'll use this little shorthand
notation, say += 1900, and that will give me
| | 03:42 | the year that I'm expecting.
| | 03:43 | So I'll save and run and
2010 is the correct year.
| | 03:47 | The month on the other hand is this way on
purpose, and this is 2 because it's zero-based.
| | 03:54 | So this is really convenient if I want
to print out the actual name of the month.
| | 04:00 | I can make an array, call it months, and put in here
Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov and Dec.
| | 04:18 | And then over here where we're
printing out the month instead of doing like that,
| | 04:22 | I need a space here, I can
say months sub-month like that.
| | 04:32 | That will index this array and it
will print out the name of the month.
| | 04:38 | So the month is March and the day is 12.
| | 04:41 | So that's actually really kind of convenient.
| | 04:44 | So if I wanted to here, I could print
this out year and put a -month and put a -day
| | 04:55 | and I can get the time, if I want
here, I can say $hour, $min and $sec, and
| | 05:06 | print that out, and there we have it.
| | 05:09 | The second doesn't have a 0 in it and we
could do some formatting to get that correct.
| | 05:14 | In a nutshell though, that is how
you get the time and the date using the
| | 05:19 | local time function.
| | 05:20 | Likewise if you want to, you can,
instead of local time, you can get Universal
| | 05:25 | Time or Greenwich Mean Time.
| | 05:27 | You just say gmtime here instead of
local time and save that and run it and
| | 05:33 | that gives us the time in
Greenwich Mean Time instead.
| | 05:37 | So those are the time functions that
Perl provides and that's how they work.
| | Collapse this transcript |
|
|
14. ModulesLeveraging code with modules| 00:00 | Modules are collections of Perl
code for use with different scripts.
| | 00:05 | So what that means is that this
is how you reuse code in Perl.
| | 00:11 | Modules can be a set of functions that
you've written, big pieces of data that
| | 00:16 | you've written, or you can get them from
other places like the Comprehensive Perl
| | 00:22 | Archive Network or CPAN, which is a
repository for modules that have been
| | 00:28 | written by all kinds of
people all over the world,
| | 00:31 | I even have some module up there,
which you can download and install on your
| | 00:36 | system and use them with your code.
| | 00:39 | So modules are really a great way to
leverage code that you've already written
| | 00:44 | and to leverage code that others have
already written, so that your effort can
| | 00:50 | be maximized in writing your
own code using these modules.
| | 00:55 | Modules can be object
oriented or they can be procedural.
| | 00:59 | Most modules are object-oriented
these days but there are still some
| | 01:03 | modules that use procedural
techniques and there are some modules that can
| | 01:07 | be used either way.
| | 01:09 | And modules can of course contain
code and/or data and sometimes we don't
| | 01:15 | think of reusing data.
| | 01:17 | But for example, I've got a module on
CPAN, which is simply a list of address
| | 01:22 | codes with country codes and state
codes, with both the two letter codes and
| | 01:27 | the names, so that you can use it in
creating lists on websites and things like that.
| | 01:32 | So reusable code and reusable data
is something you can do with modules.
| | Collapse this transcript |
| Understanding object-oriented and procedural modules| 00:00 | Perl modules can be either object-oriented or
procedural or in some cases, they can be both.
| | 00:06 | Most modules today are object-oriented.
| | 00:09 | Object-oriented code has these properties.
| | 00:13 | Object-oriented code is dynamic.
| | 00:16 | That means that the object determines
what code to invoke, not the caller.
| | 00:21 | So if I'm using an object-oriented
module and I call a particular method inside
| | 00:26 | that module, the object depending on
how I'm calling it and in what context,
| | 00:31 | what's been inherited and things
like that, will decide what code to run
| | 00:36 | inside that module.
| | 00:38 | Encapsulation is another important
property of object-oriented code and that
| | 00:43 | means that the object contains all the
code and all the data that it needs and
| | 00:47 | I don't have to worry about providing that.
| | 00:50 | The details of how things are done or
hidden from the user, the user being the
| | 00:56 | programmer, the one whose using the module.
| | 00:58 | This is what's called abstraction and
that's also an important property of
| | 01:03 | object-oriented code.
| | 01:05 | And finally, one object may inherit the
properties of another object, in fact it
| | 01:10 | can even inherit code, and this makes
it easier to write object-oriented code.
| | 01:16 | Once you already have a basis
| | 01:19 | in other modules you can inherit the
code from those other modules and the data
| | 01:24 | and use that in your code
without having to write it over again.
| | 01:28 | On the other hand, procedural
code has these important properties.
| | 01:32 | Sometimes it's a lot
simpler to write procedural code.
| | 01:36 | Small subroutines may be
coded quickly and easily.
| | 01:40 | Procedural code may be very modular,
functionalities may be used and
| | 01:45 | reused, much like they can be in
object-oriented code but with the
| | 01:48 | simplicity of the procedural.
| | 01:50 | And procedural code tends to be more linear
and sometimes more clear and more readable.
| | 01:56 | So these are reasons why you might want
to write your modules in object-oriented
| | 02:00 | fashion or in a procedural fashion.
| | 02:02 | And like I said, most modules
are object-oriented these days.
| | 02:06 | Object-oriented programming is very popular.
| | 02:09 | Most of my modules are object-oriented
because of the inheritance property makes
| | 02:13 | it really easy to use for more complex
tasks, but for very simple tasks, often
| | 02:19 | times a procedural approach is the
approach that's going to make the more sense.
| | Collapse this transcript |
| Creating modules| 00:00 | So you have a piece of code that you'd like
to reuse and you want to put it into a module.
| | 00:05 | Let's take a look at how you do that in Perl.
| | 00:07 | We'll make a working copy of modules.
pl here and we'll call this creating.pl.
| | 00:15 | So we're going to create a module and there
it is. Open up the window here a little bit.
| | 00:22 | I'm going to paste in a piece of code
here. If you want to do the same thing,
| | 00:27 | you can find this code in the Example-
finished.pm file in your Exercise Files.
| | 00:33 | Or you can just pause the movie
and type-in what you see here.
| | 00:36 | So we'll start by creating a new file.
| | 00:40 | I'm just going to right-click on this
folder here and let's say New > Perl
| | 00:43 | File, and we will name this example.pm.
| | 00:48 | Modules in Perl are usually named
with a .pm at the end and they are also
| | 00:53 | usually named with
capital letter at the beginning.
| | 00:57 | That's very common.
| | 00:58 | Sometimes it's something with
initials like DB, you might name it DB with
| | 01:03 | both capital letters.
| | 01:03 | But the first letter is
usually a capital at least.
| | 01:07 | So we'll call this Example.pm.
| | 01:09 | We'll put it in that file there and
we will start by putting in a comment
| | 01:14 | here and we'll call this example.pm
and you can put what it does, Example
| | 01:21 | perl module, and who wrote it and whatver
else you want to put in your comments there.
| | 01:27 | Then the first line in the file should
be the package name, and in this case
| | 01:32 | we're going to say package Example.
| | 01:36 | Most of the time that is going to be
the name of the module and that's going to
| | 01:40 | be the same as the name of the
file without the file extension.
| | 01:44 | So the package name, what that
does is that sets up the namespace.
| | 01:48 | The way that modules work in Perl is
they work on top of Perl's namespace
| | 01:53 | model, so each module has its own
namespace and the package keyword is how you
| | 01:59 | set up the namespace.
| | 02:00 | We're always going to use strict and
use warnings and we're always going to put
| | 02:07 | in a version string.
| | 02:09 | And you'll use our for that instead of
my and this makes it public so that the
| | 02:16 | calling program can reach into this
module and check the version to make sure
| | 02:21 | that it has a version that it needs.
| | 02:23 | And this is used throughout Perl's
system for keeping track of modules, so you
| | 02:29 | want to have a version number and in
this case I'm going to name it 0.1 and
| | 02:34 | you do put it in quotes like that and
that allows you to use different types
| | 02:37 | of naming conventions.
| | 02:39 | So the next thing you need is you need a
constructor because it's going to be an
| | 02:44 | object-oriented module, as most modules are.
| | 02:48 | So the constructor is usually named new.
| | 02:50 | It doesn't have to be but it's
convention so we are going to create a
| | 02:53 | sub-routine, a function called new, and
that's going to be the constructor for
| | 02:59 | the object-oriented module.
| | 03:01 | And this is very simple.
| | 03:03 | It basically has the class name and
that gets passed in, in the way that it's implied.
| | 03:12 | You don't actually pass it in
explicitly and you'll see how that works when we
| | 03:15 | use it from the calling side. And there
is always a self and what this is,
| | 03:22 | this is an anonymous hash and most modules.
Again, this is not something that's
| | 03:27 | required but most modules use an
anonymous hash for its instance data.
| | 03:33 | And if you're familiar with object-
oriented programming, instance data is the
| | 03:36 | dynamic data that goes along with
each invoked instance of a class but is
| | 03:42 | different is in the name
space of the classes it's running.
| | 03:45 | It's not static; it's dynamic.
| | 03:48 | And then we're going to use the bless
function to turn this hash into an object,
| | 03:56 | and that looks like this, and finally
the constructor returns a reference to the
| | 04:04 | blessed class, and we now have an object.
| | 04:11 | Finally, and this is a Perl
convention, the module must end.
| | 04:17 | It must return 1 and that is
traditionally done by just putting a 1 on a line by
| | 04:23 | itself with a semicolon, and we now
have a working object-oriented model.
| | 04:30 | Well, it doesn't do anything yet.
| | 04:32 | We'll go ahead and we'll save it and we
will continue and we'll take this code
| | 04:37 | here and we will copy this.
| | 04:41 | We don't need the message done,
pressing the copy key, Command+C on a Mac or
| | 04:46 | Ctrl+C on a PC, and I'm going to paste
it over here and then we'll turn this
| | 04:56 | into an object method, so we'll say copyfile.
| | 05:01 | That's what we'll call it and put the
closing ridge down here at the end and
| | 05:07 | then we need to make this into the
calling conventions for a module.
| | 05:14 | Now in object-oriented Perl, the first
argument is always a reference to the
| | 05:20 | object itself and then we have the
original file name and the new file name
| | 05:28 | and equals = @_. So that's how
arguments are passed in Perl and so we don't
| | 05:35 | need these two lines.
| | 05:37 | The buffer size we're going to want to
keep and we need a my here and this can
| | 05:46 | stay the same but we need to import the IO file.
| | 05:48 | So we'll go up here to the top and
we'll say use IO::file like that and
| | 05:54 | scrolling back down.
| | 05:55 | We're saving the binmode, we're copying
the file, and when we're done we're just
| | 06:04 | going to return a 1 for
true that we are successful.
| | 06:10 | Finally, these error calls here, we
don't have access to an error function from
| | 06:15 | within the module so we're going to
use Perl's built-in error function called
| | 06:19 | die and what that does is it does
a hard exit with an error message.
| | 06:24 | And it will give you a stack trace and
tell you what line it was called on and such.
| | 06:30 | And just in the case that
it can't open those files.
| | 06:33 | So there we go, we have a
rudimentary object method in our module.
| | 06:39 | We'll go ahead and we'll save this and
we'll come over to the creating side.
| | 06:43 | Now we don't need all of this anymore,
and instead what we can do is we can use
| | 06:50 | the module up here at the top.
| | 06:52 | We'll say use Example and
that's all we need to do there.
| | 06:59 | And here we say my object = Example->
new and then we can say object->copyfile
| | 07:10 | and you see that Eclipse already knows
about it because we have it in that same
| | 07:14 | directory there and we
give it the two file names.
| | 07:18 | So the first file name is olives.jpg
and the second file name is newfile.jpg
| | 07:28 | and a semicolon here.
| | 07:30 | What happens here is a couple of things,
let's just talk about how this works.
| | 07:34 | When I construct the new object with
this example and the dereference operator
| | 07:40 | and new, what gets passed is the word example.
| | 07:46 | So that the object knows the name of
its new object, and this gets shifted
| | 07:52 | into class and so the built-in bless
function blesses the self, which is this
| | 07:58 | anonymous hash with the name of the object
which gets passed in from this constructor call.
| | 08:04 | Once we have created the object and
we now have this object reference in a
| | 08:08 | variable that object reference then
gets dereferenced and the method gets
| | 08:13 | called which is copyfile.
| | 08:15 | And you'll notice we have two
arguments here but actually three arguments
| | 08:19 | show up on the other side
because self always gets passed.
| | 08:24 | And that is actually a reference to
this anonymous hash that's constructed and
| | 08:29 | that's how it carries along its instance data.
| | 08:32 | So we'll go ahead and we'll save this and
we will run it and we have the Done here.
| | 08:38 | It looks like it worked.
| | 08:39 | We'll refresh this directory and
we see that we have a copy of the file.
| | 08:45 | Double-click on it and
there it is. That's a copy.
| | 08:47 | So this jpeg got copied, here is the
original and the copy, and all we had to do is this.
| | 08:54 | Anytime we need to copy a binary file,
all we have to do is bring in this module
| | 08:59 | and invoke the copy function and we're done.
| | 09:03 | So that's how you reuse code
by creating a module in Perl.
| | 09:09 | It's really a very simple process.
| | 09:11 | It seems like it has a lot of steps.
| | 09:13 | But the easy thing to do is to keep
a template around and here I have a
| | 09:17 | template called template.pm.
| | 09:19 | This is included in your
Exercise Files and this is a template.
| | 09:23 | It's got the constructor there.
| | 09:25 | It has a little sample method and it
even has pod documentation, which we'll
| | 09:30 | talk about in another chapter.
| | 09:32 | So you can use this as a starting place,
just change the name here, change the
| | 09:36 | name there, and put in
your own code and you're done.
| | 09:40 | And you have a working module just
like we created here from scratch.
| | 09:45 | So that's how you reuse code
by creating a module in Perl.
| | Collapse this transcript |
| Understanding object and instance data| 00:00 | As you're creating your modules, it's
important to understand the distinction
| | 00:05 | between object data and instance data.
| | 00:08 | So let's look at an example
so that we can understand that.
| | 00:11 | We are going to start by making a
working copy of the Template.pm, and we'll
| | 00:16 | use this to create a module, an
object oriented module called Artist.pm.
| | 00:21 | Go ahead and open that up.
| | 00:24 | I will start by changing the name
here and changing the name here.
| | 00:32 | The module will not work if you don't
change the name in this place right there,
| | 00:35 | so that's really important.
| | 00:39 | So this is actually an example of object data.
| | 00:43 | This data here doesn't change from
instance to instance and it's actually
| | 00:47 | stored in the module.
| | 00:48 | It's not stored in the object variable.
| | 00:51 | I am going to go ahead and take this
method here and we are going to call it
| | 00:56 | Name and we are going to use this as
what's called a setter-getter where we
| | 01:00 | actually set instance data and get
instance data in the same method.
| | 01:06 | We are going to pass a variable in that
is name and it's typically the same name
| | 01:13 | as the setter-getter.
| | 01:14 | The setter-getter is named the same
name as the instance data and then we are
| | 01:18 | going to create an instance
variable in Perl that looks like this.
| | 01:24 | So that's an instance variable.
| | 01:26 | It's getting stored in the
hash that's pointed out by self.
| | 01:30 | So you remember up here in the
constructor you created an anonymous hash and you put
| | 01:36 | it into self, self is a hash reference
and then you blessed that hash reference
| | 01:41 | and it became an object
reference and it got returned.
| | 01:44 | So every time you create an object you
create an object reference and it uses
| | 01:50 | this anonymous hash and then all of
the data that you create that's instance
| | 01:54 | data like this gets stored in that
hash and gets carried around with that
| | 01:58 | particular instance of your object.
| | 02:01 | You have different instances on an
object. You can have different data in them.
| | 02:04 | We will see how that's done here.
| | 02:06 | So this will get name, which is getting
passed in here, and I am just going to
| | 02:11 | put if defined $name.
| | 02:14 | I am going to put the dollar sign in there.
| | 02:17 | That way it doesn't get called if you
don't pass name, and then we're going
| | 02:21 | to return the instance data, and that way
this can be used as both a setter and a getter.
| | 02:27 | So if you call this method with an
argument, that argument will get used to set
| | 02:32 | the instance data. If you
do not pass in an argument,
| | 02:36 | then you'll simply be getting
a copy of the instance data.
| | 02:40 | Let's see how that works.
| | 02:42 | I am going to save this and we are
going to make a working copy of modules.pl
| | 02:46 | where we will call it and we
will just call this oodata.pl.
| | 02:51 | We are going to use this module, use
Artist, and we are going to create an
| | 03:00 | instance of the object $o = Artist->new,
and then we will set the instance data
| | 03:10 | $o->name and we will name this artist
Jimi Hendrix, and then when we want to get
| | 03:21 | the object data, we can put in a
variable if we want to or we can just get it
| | 03:25 | directly in the message call here.
| | 03:27 | That will print out the name Jimi Hendrix.
| | 03:31 | So we will go ahead and save this.
| | 03:34 | What we're doing is we're creating an
instance of the object and that instance
| | 03:38 | gets saved in this object reference
variable here and then we're using the
| | 03:43 | method in this object to set the
instance data to Jimi Hendrix and we're
| | 03:49 | retrieving that instance
data by calling the same method.
| | 03:54 | When we run this, we'll see it
prints out the name Jimi Hendrix.
| | 03:57 | Now if I wanted to I could create a
different artist, my $o2 = Artist->new, and
| | 04:06 | we can give that one a different name,
say Miles Davis. This is o2 and if I
| | 04:14 | were to call o2 name here instead of the
original O, then we will get the name Miles Davis.
| | 04:22 | That's instance data you can see because
that data is actually getting stored in
| | 04:26 | the object reference,
the instance of the object.
| | 04:29 | If I want to just get the object data,
remember this has object data as well
| | 04:33 | in the form of this VERSION, and you can
clear all kinds of variables here if you want to.
| | 04:38 | It's not a good idea to
overuse this because it's persistent.
| | 04:42 | I can just message, if I want to see
what version I have got, I can just
| | 04:47 | say message Artist
| | 04:48 | VERSION like that and give it a
dollar sign because it's a scalar.
| | 04:54 | And when I save this and run it,
we see we get the version number down here.
| | 05:00 | That also gets carried
along in each of the instances.
| | 05:03 | So if I say $o2->VERSION like this,
I will still get that object data.
| | 05:08 | But that's the static object data.
| | 05:10 | That's not dynamic data.
| | 05:12 | That doesn't change.
| | 05:13 | If I were to change it,
it would change for all the instances.
| | 05:16 | It's not carried along in each
individual instance and do not change it.
| | 05:20 | It's a really bad idea to
use object data like that.
| | 05:24 | It gets very confusing very quickly.
| | 05:27 | So that's the distinction
between object data and instance data.
| | 05:30 | It's important to understand and it's
important to keep track of it as you
| | 05:34 | create modules using the object-oriented model.
| | Collapse this transcript |
| Installing modules from CPAN on Mac and Unix| 00:00 | CPAN is the Comprehensive Perl Archive Network.
| | 00:05 | It is a repository of many, many
modules that have been written by many,
| | 00:09 | many different people.
| | 00:11 | As such these modules vary in
quality. Some of them are wonderful and bug
| | 00:16 | free and used a lot and some of them are not.
| | 00:20 | So this is where you would search for
something that works with JPEG files
| | 00:26 | and you'll get a long list of modules
that may or may not do what is it that
| | 00:32 | you want them to do.
| | 00:34 | Once you have identified the
module that you're looking for and you want
| | 00:38 | to install it, the easy way to install it is
by using the CPAN command line application.
| | 00:45 | If you're on a Macintosh or a UNIX
system, you'll do this from the command line
| | 00:50 | and you'll type in something like
sudo cpan and the name of the module.
| | 00:56 | You're using sudo here because sudo
allows you to run as the root user, because
| | 01:02 | you will be installing the module
system wide for the entire system.
| | 01:07 | It is possible to install from CPAN for
an individual user, but how you do that
| | 01:13 | is going to differ from system to system.
| | 01:16 | So what I'm going to show you here is
what works and the way that it is normally
| | 01:21 | done which is to install it as root.
| | 01:25 | So I'm going to go ahead and install a
module. I'm going to install one of my modules.
| | 01:30 | This one is called BW::Base.
| | 01:33 | What it is, it's the base of the
module system that I've written.
| | 01:37 | It installs a lot of utilities that are
useful and that are used in a lot of the
| | 01:42 | programs that I write and that I distribute.
| | 01:45 | So that's all that's involved.
| | 01:46 | You just type sudo and cpan
and the name of the module.
| | 01:50 | I'll press Return here and it will ask
for the password because sudo asks for
| | 01:56 | the password. Type that in, and there we go.
| | 01:59 | If has fetched it from
CPAN and it has installed it.
| | 02:03 | Now there's all kinds of messages here.
| | 02:05 | Some of them may look like errors,
sometimes it'll come up with errors and it
| | 02:09 | will go around them.
| | 02:10 | It'll have trouble downloading from one place.
| | 02:12 | It'll try some place else.
| | 02:13 | You'll often get this one right here.
| | 02:16 | What you're looking for is this
line, /usr/bin/make install -- OK.
| | 02:21 | If you see that then it went fine, if you
see something else then there may be a problem.
| | 02:27 | It may be missing something.
| | 02:28 | It may need some dependency modules installed.
| | 02:31 | It'll tell you what to do but this is
what's involved in installing modules.
| | 02:36 | Now CPAN is not perfect.
| | 02:39 | The CPAN utility that
installs the modules is not perfect.
| | 02:42 | It often comes up with errors.
| | 02:44 | It can be very confusing and very
difficult and often times even I will go
| | 02:50 | around using CPAN and just
download the module and install it myself.
| | 02:55 | So I'm going to show you
briefly how to do that as well.
| | 02:58 | So I'm going to go back to the web
browser here and I'm going to go and find
| | 03:02 | one of my other modules, which is called BW
::DB, and we will install that the other way.
| | 03:07 | So I'm going to click on it here.
| | 03:10 | I'm going to download it,
where it says Download.
| | 03:13 | I will save link as, I'm going to just
put it in my directory, save it there.
| | 03:23 | Now I'm going to go back to the
command-line and there's the file.
| | 03:30 | What I'm going to do now is I'm going
to open up this file using tar, tar xvfz
| | 03:37 | and the name of the file.
| | 03:40 | Basically what it's done is it's
created a directory called bw-db and there
| | 03:47 | it is there.
| | 03:48 | That's the directory.
| | 03:49 | So I'm going to change into that
directory with cd and here's what we have in here.
| | 03:57 | So now how you install a module from the
command-line without using CPAN is also
| | 04:03 | very simple and prone to error.
| | 04:06 | So I'm going to type perl Makefile.pl.
| | 04:10 | It's a good idea to read the
README file and read the INSTALL file.
| | 04:14 | I'm just going to go through the steps here
for you and this actually makes the Make file.
| | 04:20 | It's called the Make
maker and then I'll type make.
| | 04:23 | Now on a Macintosh before you can
install modules using CPAN or the
| | 04:28 | command line, you need to have Make
installed at a very minimum and for some
| | 04:32 | things you might also need a C compiler.
| | 04:34 | So it's a good idea to install Xcode,
which is on your OS X distribution disk or
| | 04:41 | you can download it from Apple for free.
| | 04:43 | So I've done that on this Mac and I have
Make installed and so I type make, just
| | 04:50 | like that and it does that successfully
and then I type make test, and it will
| | 04:56 | run the tests and it says All tests
successful and I type sudo make install.
| | 05:03 | The rest of it, I don't need sudo
for, but for the install part I do.
| | 05:07 | So I type sudo make install.
| | 05:09 | It may or may not ask for your password.
| | 05:11 | It didn't ask me because it asked
for my password a couple minutes ago.
| | 05:15 | After a certain timeout, it'll
ask me for my password again.
| | 05:19 | So this has successfully installed.
| | 05:21 | We can see that it says installing,
installing, writing, appending and it
| | 05:25 | doesn't give me anything that
looks like an error message.
| | 05:29 | That's how we install a module from
the command-line by downloading it and
| | 05:33 | installing it, if the command-
line CPAN is giving you trouble.
| | Collapse this transcript |
| Installing modules from CPAN on Windows| 00:00 | Now we are going to
install CPAN modules on Windows.
| | 00:05 | Now, because Perl was designed for UNIX-
based systems and Windows is very, very
| | 00:11 | different from a UNIX-based system,
the process of installing CPAN modules is
| | 00:16 | very different than it is on
a Mac or on UNIX-based system.
| | 00:20 | We are using the active state
distribution of Perl on this machine, which is
| | 00:25 | the most common distribution of Perl
for a Windows-based machine and it's
| | 00:30 | probably what you're using.
| | 00:31 | So, we are going to be installing our
CPAN modules using the Perl Package Manager.
| | 00:36 | I've got it on my menu here,
because I have had it opened already.
| | 00:39 | But we are going to go and find it
in the All Programs, because that's
| | 00:42 | what you'll be doing.
| | 00:43 | I'll select the ActivePerl Build
and whichever one you have, it's okay.
| | 00:48 | You select the Perl Package Manager.
| | 00:52 | It needs to synchronize its database.
| | 00:54 | It will start with that and
that takes it a little moment.
| | 00:56 | Now, you'll notice that there is a
number of buttons here on the left hand side,
| | 01:01 | and this selects what it
is that we are looking at.
| | 01:03 | This view here is all of the
packages that are already installed.
| | 01:08 | This view here is the ones
that have upgrades available.
| | 01:11 | You don't necessarily need to
be upgrading them all the time.
| | 01:15 | If you don't actually need to update
a particular module, it might be best
| | 01:18 | to leave it alone, because that can
sometimes cause problems with the existing code.
| | 01:22 | We are going to look at the View All
Packages, because this will include the
| | 01:26 | ones that are not installed yet.
| | 01:28 | I am going to select one of my packages.
| | 01:31 | I am just going to type BW up here, and
that will find the ones that have bw in them.
| | 01:36 | These two here are packages that I've written.
| | 01:39 | So, first I am going to select this
one here, because I know that this has a
| | 01:42 | prerequisite that we do not have.
| | 01:44 | bw-db requires another package called DBD:MySQL.
| | 01:49 | Because it requires the MySQL system and we
do not have MySQL installed on this machine.
| | 01:55 | So, I wanted just to show you what happens
when there is a dependency that is not fulfilled.
| | 02:00 | So, I am going to right click on this.
| | 02:04 | You'll see we get a little pop-up menu
with just one selection for installing.
| | 02:08 | You'll also notice there is a plus sign there.
That means instead of using the popup menu,
| | 02:11 | I could just press the plus key
on my keyboard. So, I'll do that.
| | 02:15 | You see that it says depends on bw-
lib, and it also depends on DBD-MySQL.
| | 02:22 | So, I know that DBD-MySQL
will not install on this machine.
| | 02:25 | If I go ahead and select it, it won't complain.
| | 02:28 | But if I try and install it, it will
give me trouble because I don't have
| | 02:31 | MySQL on this machine.
| | 02:33 | We are not going to go
ahead and install this one.
| | 02:36 | I'll just press the plus key
and that will deselect it.
| | 02:40 | We are going to install this one,
because this doesn't have any
| | 02:43 | dependencies that were missing.
| | 02:44 | I'll press the plus key on this one here,
we see it is a bw-lib is marked for
| | 02:50 | install, and it doesn't
have any messages about that.
| | 02:54 | If we want to go ahead and install that,
we can press Ctrl+Enter or we can press
| | 02:59 | this right green arrow key
up here. I'll press that.
| | 03:02 | It says are you ready to
install 1 package? I'll click Ok.
| | 03:06 | It's doing its work and then
it says Installing package done.
| | 03:10 | So, that's really all there is to it.
| | 03:13 | It's installed the package.
| | 03:15 | Now, if we look in the details over here,
we can see those are all the installed files.
| | 03:20 | You'll notice that there's
some HTML files installed.
| | 03:23 | That's the documentation.
| | 03:25 | So if we go ahead and look at the Perl
documentation, going to All Programs >
| | 03:31 | ActivePerl, and just click on
Documentation, it will bring up your web browser
| | 03:35 | and it will bring up the
Active State Documentation for Perl.
| | 03:38 | If we scroll down here into the modules that
are installed, and then we go to b and we see BW.
| | 03:45 | Here's all the stuff it installed.
| | 03:47 | It installed BW Base, BW CGI, BW Common.
| | 03:51 | So, there is all of the POD
documentation that comes with these modules.
| | 03:57 | That POD documentation
has been converted to HTML.
| | 04:00 | One of the features of POD is that it's
easy to convert it to a lot of different
| | 04:05 | display formats, and HTML is a very common one.
| | 04:08 | We'll talk more about
that in the chapter on POD.
| | 04:12 | That's it. We have installed a module.
| | 04:14 | That's how you install CPAN
Modules on a Windows machine.
| | 04:18 | You don't actually use the CPAN program.
| | 04:21 | You use the Perl Package Manager,
which is actually pretty convenient and
| | 04:24 | pretty easy to do.
| | Collapse this transcript |
|
|
15. Documentation with PODUnderstanding POD| 00:00 | POD is how you do documentation in Perl.
| | 00:04 | It stands for Plain Old Documentation.
| | 00:06 | And it's called that because at the
time when Perl was first created and POD
| | 00:12 | was first created, the standard in
computer documentation was UNIX man pages,
| | 00:18 | and what POD puts out looks
very much like UNIX man pages.
| | 00:23 | The structure of them, the way that
the finished product looks and feels, and
| | 00:28 | the way that it was used that the UNIX
command line was very much like the UNIX
| | 00:33 | man pages, which at that time was an
excellent standard of documentation.
| | 00:38 | Time has marched on and
documentation is much fancier now.
| | 00:42 | POD is still a really good way to do
things because it's basically extremely
| | 00:49 | simple to use POD to
create a uniform documentation.
| | 00:55 | The syntax of it is easy, how it
gets applied to the documents is
| | 01:01 | easy, everything about it is easy
to use, and the finished result is
| | 01:06 | more than serviceable.
| | 01:07 | With the various POD translators
that are available today and even come
| | 01:11 | standard with Perl, you can create
very fancy looking modern documentation in
| | 01:18 | HTML and PDF, and a lot of different formats.
| | 01:21 | The source of it is still this
incredibly simple to use syntax.
| | 01:25 | So let's take a look at how you
create documentation using POD.
| | Collapse this transcript |
| Documenting with POD| 00:00 | POD documentation is mostly used for modules.
| | 00:03 | It certainly can be used for any Perl
code, but it's mostly used for modules.
| | 00:08 | Let's take a look at how that is typically done.
| | 00:13 | Make a working copy of this
module here, we'll call it Example.pm.
| | 00:17 | I'll go ahead and open that up and
we'll take a look at the POD documentation
| | 00:23 | down here at the bottom.
| | 00:25 | You'll notice that at the end of the
code, we have this __END__ directive and
| | 00:29 | then the POD documentation begins from there.
| | 00:34 | Here we have a POD directive.
| | 00:36 | It's the equal sign followed by head1.
| | 00:39 | That makes a level 1 heading.
| | 00:42 | We have the word NAME for the heading.
| | 00:44 | After that, we have just a paragraph
of text and then another level 1 heading
| | 00:48 | that says SYNOPSIS, some more
text, and another level 1 heading.
| | 00:54 | We have this over directive and
that makes an indent of four characters.
| | 01:00 | Then we have an item directive
and these could be bullet items.
| | 01:04 | They're not always formatted with bullets.
| | 01:07 | How it's formatted depends on the POD processor.
| | 01:10 | The typical POD processor is pretty plain.
| | 01:13 | Here we have a formatting codes.
| | 01:15 | This one is B for Bold.
| | 01:17 | This word will be in bold.
| | 01:19 | The formatting codes are one-letter and
then angle brackets around whatever it
| | 01:24 | is that they operate on.
| | 01:25 | Then we have a couple of more
paragraphs, another item and then back.
| | 01:29 | This undoes the indenting of the over.
| | 01:33 | That's pretty much it.
| | 01:34 | At the end, there is a cut.
| | 01:36 | You could actually have more Perl code after
that, except that I used this END directive.
| | 01:42 | That will prevent any other
Perl code from coming after this.
| | 01:46 | But this is typically the way that it's done.
| | 01:48 | POD documentation can be, and you'll
sometimes see it interspersed with the code.
| | 01:52 | Most people put it all the way at the end,
and that's the really convenient way to do it.
| | 01:57 | So let's take a look at what this looks like
in the Perldoc processor and type in Template.
| | 02:04 | That will go ahead and bring up
the documentation for this module.
| | 02:09 | Maximize that so you can see it.
| | 02:11 | That's what the documentation looks like.
| | 02:14 | It's common to use this format. You
have NAME and then you give the name and a
| | 02:20 | single dash and a one-line description.
| | 02:22 | SYNOPSIS is used for how the code is
used and then methods for the different
| | 02:31 | methods in the module.
| | 02:33 | Here we have the new method and what it
does, the METHODS method, which is just
| | 02:38 | a placeholder where you can create
your own, the AUTHOR and the HISTORY.
| | 02:44 | This is all created by this POD documentation.
| | 02:50 | Looking in detail, head1 creates a level 1 head.
| | 02:53 | There are four possible levels of headings.
| | 02:56 | So we could have head2, let's call it
SOMETHING ELSE, and a head3, and call it
| | 03:03 | YET ANOTHER.
| | 03:09 | If we look at that, go ahead and save
this and close this and bring that up
| | 03:16 | and type in Example.
| | 03:19 | So, you see that SOMETHING ELSE is
intended a little bit more, YET ANOTHER is
| | 03:23 | indented a little bit more.
| | 03:25 | That's how those work.
| | 03:26 | You'll also notice that sometimes I
have the text all the way over to the left
| | 03:30 | and sometimes I have it indented.
| | 03:32 | There is a reason for that.
| | 03:33 | When text is all the way over to the
left, it's formatted like a paragraph.
| | 03:37 | For example, if I were to take this
line here and just duplicate it a few times
| | 03:42 | and save that and look at it in the POD
documentation, then we see that it gets
| | 03:53 | formatted like a paragraph.
| | 03:55 | It ignores these new lines and just
wraps it around however it wants to.
| | 03:59 | On the other hand, if the line is
indented at all, it will format it literally.
| | 04:05 | So you see up at the top here
where we have that SYNOPSIS.
| | 04:08 | That is, it's indented, but
it's also formatted literally.
| | 04:11 | I have one line after the other.
| | 04:13 | So when you have an example of code,
you always need to indent it a little bit,
| | 04:17 | so that it stays formatted the way
that you intended in the documentation.
| | 04:27 | We'll just get rid of those.
| | 04:31 | That's pretty much how POD documentation works.
| | 04:34 | Of course, there are a few other commands.
| | 04:36 | There are a few other
formatting codes and some other details.
| | 04:40 | If you look in the Perl documentation
and you search for perlpod, you'll get the
| | 04:48 | entire documentation on how POD
documentation works and all the different
| | 04:53 | options that are available.
| | Collapse this transcript |
| Exploring a POD document| 00:00 | Let's take a look at a typical POD
document, so that we can get an idea of the
| | 00:04 | kinds of things that go in it and
how it typically gets formatted.
| | 00:09 | This is the documentation for
one of my modules, BW::Common.
| | 00:12 | It's just some common utility
functions and this is the source code for it.
| | 00:16 | It's not a very big module, but it
has a few different methods in it.
| | 00:21 | Here's the POD
documentation for it starting down here.
| | 00:24 | You see it has head1 NAME, and then it
has the name of the module and a dash and
| | 00:30 | a short description.
| | 00:32 | This is actually used by the CPAN
importer to automatically generate a one-line
| | 00:38 | description for when people do searches in CPAN.
| | 00:42 | So using this exact format here is a
good thing to do if you're going to
| | 00:46 | publish your modules.
| | 00:49 | Then there is a SYNOPSIS and this
is just typically how you use it.
| | 00:53 | So, if we look in the actual POD
documentation how this comes out, you see
| | 00:58 | those parts look like that.
| | 01:00 | Then we document the methods.
| | 01:01 | So, there is a level 1 header that
says METHODS, and then these are all done
| | 01:06 | with the item command.
| | 01:07 | So we have over 4 and item,
new, item B<comma> number.
| | 01:13 | Some people will use the
parentheses. I use the old UNIX style.
| | 01:17 | You can do this either way.
| | 01:19 | It's probably more
common to use the parentheses.
| | 01:22 | So <comma> number returns a comma-fied number.
| | 01:25 | So this just gives a little
description of each of these.
| | 01:28 | You'll notice with this one, you see
the way that it's wrapped here, when I
| | 01:32 | write it, I tend to put my
sentences on separate lines.
| | 01:34 | It makes it easier for me to edit them later.
| | 01:37 | The Perlpod processor will typically
wrap them around and make them look nice.
| | 01:42 | So, that's the way that those look
and all of those are formatted with
| | 01:46 | these item commands.
| | 01:48 | Then we have back and a level 1
header for AUTHOR, a level 1 header for the
| | 01:55 | COPYRIGHT, level 1 header for the HISTORY.
| | 01:58 | I indent my HISTORY, so that it will
not get formatted, so that it will be
| | 02:02 | literal, just like with the SYNOPSIS.
| | 02:05 | Then I format them really nice.
| | 02:06 | I'll align everything up, so that it's
easy to look at the HISTORY and to see
| | 02:10 | whatever version it is you're
looking at, one of the changes. That's it.
| | 02:14 | POD documentation is incredibly easy.
| | 02:17 | If it weren't, people wouldn't
document their code as easily.
| | 02:20 | Programmers are notorious for
not bothering with documentation.
| | 02:25 | So the easier you can make it, the more
likely it is that the programmers will document.
| | 02:30 | Using POD is so easy
it's actually a pleasure to do.
| | 02:34 | So there is a typical example of how
you can create some simple documentation
| | 02:40 | using POD to document your modules.
| | Collapse this transcript |
|
|
16. New Features in Perl 5.10Using Perl 5.10| 00:00 | As of this recording, Perl 5.10 is
just over a year old and it's still not
| | 00:05 | very widely deployed.
| | 00:07 | The very latest computers and the
very latest distributions are starting to
| | 00:11 | support 5.10, but most computers that aren't
brand-new still have 5.8 or even 5.6 on them.
| | 00:20 | So, writing new code with the features of
Perl 5.10 will limit the audience of the code.
| | 00:27 | If you're writing code that's just for
your own computer, then go ahead and use
| | 00:31 | the 5.10 features, but if you're
expecting to distribute it and expecting it to
| | 00:35 | run on somebody else's server, on
somebody else's machine, then you really need
| | 00:39 | to still avoid the new features of Perl 5.10.
| | 00:44 | Perl 5.10 includes new language
features, and these new features are
| | 00:49 | incompatible with previous versions of Perl 5.
| | 00:53 | So you just need to be careful
when you're using these features.
| | 00:57 | If you want to use the new Perl 5.10
features, you need to include this pragma,
| | 01:02 | use feature ':5.10';.
| | 01:07 | There are a couple of other
ways that you can do this.
| | 01:10 | You can individually select which
features you want to use using this syntax, or
| | 01:15 | you can simply require the latest
version of Perl using this use 5.010 syntax.
| | 01:24 | So if you're going to use the latest
features, this is where you need to put at
| | 01:28 | the top of your program, and in the
rest of this chapter we'll look at the
| | 01:31 | details of how you use some of
these new features in Perl 5.10.
| | Collapse this transcript |
| Displaying text with say| 00:00 | One interesting new feature in 5.10 is
the say built-in function, which does
| | 00:06 | pretty much what our message function was doing.
| | 00:09 | Let's take a look at that.
| | 00:10 | We'll make a working copy of this newfeat.pl
file and we'll call it say.pl and open that up.
| | 00:17 | We've got this use feature ":5.10",
that allows us to use 5.10 features, and
| | 00:24 | there is the say and in fact, this is
our whole file now, because we don't have
| | 00:29 | that message function in here anymore.
| | 00:32 | Go ahead and run this.
| | 00:33 | You'll see there it is.
| | 00:35 | And if we were to put
another one on the next line,
| | 00:42 | semicolon, go ahead
andsave that and run it.
| | 00:50 | see we get two separate lines without
having to put a new line characters.
| | 00:54 | That's what say does.
| | 00:56 | It's very simple and direct.
| | 00:59 | In order to use it, you have to be
using the features of 5.10 and of course,
| | 01:05 | this will not run in older versions of Perl.
| | 01:08 | It doesn't save a lot of work, because
obviously it wasn't that hard to have a
| | 01:13 | message function in there.
| | 01:15 | But it's a useful shortcut and it will
certainly, in the long run, it will get
| | 01:19 | a lot of use when 5.10 gets deployed.
| | Collapse this transcript |
| Selecting from multiple choices with given and when| 00:00 | Certainly one of the most widely
anticipated features in 5.10 is the switch
| | 00:06 | statement, which is not called
switch, but it's called switch.
| | 00:11 | Let's take a look at that.
| | 00:13 | We'll start by making a working
copy of this newfeat.pl file and we'll
| | 00:18 | call this one switch.pl.
| | 00:21 | And if you're familiar with any other
modern programming languages, you'll
| | 00:25 | know what switch is.
| | 00:27 | We've have to have Use feature. At
least we have to have the switch statement
| | 00:31 | mentioned in here, but we can just
use this construct of use feature :5.10.
| | 00:35 | In most languages that have a
switch feature, the keyword is switch.
| | 00:42 | And each of these selections is done
with a case statement and so it would look
| | 00:48 | something like this.
| | 00:49 | You would say switch and you would have
a variable name and you would have some
| | 00:54 | sort of a block and you
would have case and a value.
| | 00:59 | And you would have some other sort of a
block, or indentation or something, and
| | 01:04 | then you would have case and some
other value, and it would work like that.
| | 01:08 | In Perl, however, they went and
changed the name of everything.
| | 01:11 | And so switch is called given, even
though the feature is called switch,
| | 01:16 | called given here, and each of these
cases is called when instead of case,
| | 01:23 | given this when that.
| | 01:25 | It's a very nice little
syntactic thing to say given and when.
| | 01:30 | It's kind of a little bit more
meaningful in English, but they called the
| | 01:34 | feature the switch feature.
| | 01:36 | This is all based on the Perl 6 work
that's been going on for at least 10 years,
| | 01:42 | and is still not even
very near to being released.
| | 01:46 | And rather than wait for Perl 6, people
got tired up and they when added it to the
| | 01:50 | Perl 5.10, so just waiting for
Perl 5.10 be wide enough deployed.
| | 01:56 | There are some great features here in
the switch given feature and we're going
| | 02:00 | to take a look at this.
| | 02:01 | There is something called Smart
Matching, which is actually pretty cool.
| | 02:06 | Let's define a variable
here and start looking at this.
| | 02:11 | Call this one jimi hendrix and
go ahead and we'll say given($s).
| | 02:19 | Delete all of these and let
me format this a little bit.
| | 02:24 | So the $s is the selector. This is the
variable that will be tested in each of
| | 02:28 | these, and we'll say when say undef,
and we'll say $s is undefined, and
| | 02:40 | we will go ahead and make a few copies of this so
we can do some different things with it here.
| | 02:44 | And when it's exactly a jimi like
that, then we'll say '$s is a musician.
| | 02:53 | And when it matches, say, a
regular expression that includes /jimi/.
| | 02:59 | We can say maybe a musician.
| | 03:08 | Or let's say when it's a number, an
odd number, let me say $s is odd number.
| | 03:24 | The default would be "is something else."
| | 03:30 | What's cool about this is we can do all of
these different kinds of matches with this.
| | 03:36 | And this is that Smart Matching that
I was talking about and this is new to
| | 03:40 | Perl, and there are not a lot of
languages that do this or at least there's not
| | 03:43 | a lot of languages that do this well.
| | 03:45 | When we save this and run it, $s may be a
musician, which I just spelled terribly.
| | 03:53 | Fix that and fix that. Okay, save, run.
| | 03:59 | Oh that's much better.
| | 04:01 | And we can take out this here.
| | 04:03 | So we don't need that anymore.
| | 04:05 | So, you see that it didn't match undef.
| | 04:08 | It didn't match this exact match to
a string, and it found this regular
| | 04:12 | expression that it matched.
| | 04:14 | If instead, I were to make this a 7 like
that and save and run, we don't get an
| | 04:21 | error by trying to match it with the
string, or trying to match it with the
| | 04:24 | regular expression, instead it just
goes down there and it finds this numerical
| | 04:28 | test and it says $s is an odd number.
| | 04:31 | So this is the Smart Matching feature,
which is actually pretty cool, and
| | 04:35 | certainly the most common.
| | 04:38 | This is how the switch statement is used.
| | 04:41 | Again it's called the switch statement even
though it uses given and when and default.
| | 04:45 | It uses the Smart Matching
feature that allows you to do a lot of
| | 04:49 | different kinds of matching.
| | 04:50 | In fact, there is a whole document with
dozens and dozens of different ways that
| | 04:54 | you can do matching, but these are some
pretty good examples of how that's done.
| | 04:58 | So that's how the switch statement
works with given and when and default and
| | 05:03 | that's available in 5.10.
| | Collapse this transcript |
| Keeping persistent variables with state| 00:00 | An interesting new feature in 5.10
is the addition of state variables.
| | 00:05 | State variables are persistent
variables that are locally scoped like my
| | 00:09 | variables except that they don't get
reinitialized every time you start a block again.
| | 00:15 | Let's take a look at how this works.
| | 00:17 | We'll make a working copy of newfeat.pl.
| | 00:20 | I will call this state.pl.
| | 00:22 | We are using the use feature ":5.10" which
is required to be able to use state variables.
| | 00:28 | We'll go ahead and define a little
subroutine here. We'll call this increment,
| | 00:37 | declare a variable and we will do shift.
| | 00:42 | And we will say ++$n.
We'll increment it and then print it.
| | 00:49 | Up here we will declare a variable,
my $i =5, and we will go ahead and
| | 00:56 | increment, $i, and we
will do that say four times.
| | 01:04 | So when I save this and run it,
we'll see we get the number 6 four times, because
| | 01:10 | each time $i gets passed,
| | 01:13 | it gets incremented by one and printed.
| | 01:15 | It's using this local variable here,
locally scoped with my and this one here is
| | 01:21 | locally scoped with my and
never the twain shall meet.
| | 01:24 | So each time $i gets incremented all
over again and it starts all over again and
| | 01:29 | it's a 5 and this would
become 6 and it's 5 becomes 6.
| | 01:34 | Now, here's the interesting thing.
| | 01:36 | All we do is change this "my" to
"state" and the behavior changes entirely.
| | 01:41 | What happens is the first time it's
used it gets initialized, and then from
| | 01:46 | then on it ignores this and it
remembers what it was the last time and it goes
| | 01:52 | from there. So just by changing "my" here to
"state," watch how this changes the behavior.
| | 01:58 | Go ahead and run that.
| | 01:59 | And now we have 6, 7, 8, 9,
which is really kind of interesting.
| | 02:03 | Now the only way to have done this
before would have required the use of a
| | 02:08 | globally scoped variable or using a
reference to this scalar out here.
| | 02:14 | Either one of those things has horrible
potential for side effects, name space
| | 02:18 | collisions, and all kinds of possible problems.
| | 02:22 | Typically in order to do something
like that safely you would have had to have
| | 02:26 | used a module and again you've got to have a
globally scoped variable, and it's just a mess.
| | 02:31 | Now you can do little locally scoped
variable and have it behave in this
| | 02:36 | unique and interesting way that just
makes something like this very, very
| | 02:41 | doable and very, very clean.
| | 02:43 | So that is how state variables work,
and that's what they are good for.
| | 02:48 | Of course they are good for a lot
of interesting things beyond just
| | 02:50 | incrementing a variable.
| | 02:52 | You can use them actually for keeping
state in a state machine and that will
| | 02:56 | work very, very well.
| | 02:58 | So here's something else that we can
look forward to as 5.10 gets widely
| | 03:02 | deployed: state variables in Perl 5.10.
| | Collapse this transcript |
|
|
17. Exploring Web DevelopmentExploring CGI| 00:00 | CGI is the interface between a
web server and a web application.
| | 00:07 | So CGI applications always run on a Web
server; they don't run on your desktop.
| | 00:13 | You can of course install a web
server on your desktop for doing development
| | 00:16 | work and many people do that but CGI
requires a web server in order to run.
| | 00:23 | To learn more about CGI, I suggest you
take the CGI Essential Training course on
| | 00:29 | lynda.com and that will
teach you the details of CGI.
| | 00:32 | CGI is not a large subject.
| | 00:34 | It's a relatively small subject.
| | 00:36 | It's really just about a few protocols about
how servers interact with web applications.
| | 00:42 | In this movie, we're going to
look at a couple of examples of CGI
| | 00:45 | scripts written in Perl. Here's one.
| | 00:48 | It's the Hello, World written in CGI
and this is one of the simpler things that
| | 00:54 | you can do with CGI.
| | 00:56 | This is a normal looking Perl script
and the first thing here is that it prints
| | 01:01 | this content type text/html
with two line feeds at the end.
| | 01:07 | That is required to be the first output
from a CGI script in a CGI environment.
| | 01:14 | That tells the server, yes this is a
valid CGI script and it also tells it
| | 01:18 | what type of content.
| | 01:19 | It can be text/html.
| | 01:20 | It can be text/plain.
| | 01:23 | It could be image/jpg, any valid
content type, then what comes after it is of
| | 01:28 | course expected to be that.
| | 01:30 | This syntax is something that we
may not have seen before in Perl.
| | 01:34 | This is called a here document and
this little token here with the two left
| | 01:39 | angle brackets and the token says to
print everything between this and the
| | 01:46 | same token at the end and so inside of
that we have this little HTML document,
| | 01:51 | which you can see there and it basically prints
Hello, World, with a little bit of styling to it.
| | 01:55 | We can look at that in a web
browser and here we have it running on my
| | 02:01 | server, perl.bw.org. This is hello.cgi.
| | 02:05 | This is exactly the same script and
you'll see that when we View Source that
| | 02:10 | that is the HTML that we just
saw there in the Perl script.
| | 02:16 | That is a very simple example of a CGI script.
| | 02:20 | For a slightly more functional example,
we have this here, which is test.cgi.
| | 02:27 | This is the first thing that I will
always upload to a server because getting
| | 02:32 | this to work on a server, it teaches me
what I need to know about that server in
| | 02:37 | order to get other CGI scripts, other
more complicated CGI scripts, to run.
| | 02:42 | So I'd like to start with something
simple just to focus on the mechanics.
| | 02:46 | You know, what does the server require
in order to run CGI. All servers have
| | 02:51 | different requirements.
| | 02:52 | It may require that the
file be named a certain way.
| | 02:55 | It may need to say .CGI at
the end of the file name.
| | 02:58 | It may need to say .PL for
Perl at the end of the file name.
| | 03:01 | If it's a UNIX server, it's going to
need certain executable permissions and it
| | 03:06 | may need to be in a special directory.
| | 03:08 | It may need some configuration set.
| | 03:10 | So whatever those requirements are,
the process of installing this one simple,
| | 03:15 | one file small script is going to teach
me what that is and what it is that I
| | 03:20 | need to know about that particular
server in order to run CGI and once I have
| | 03:25 | the script running then the output of the
script will tell me even more about the server.
| | 03:31 | Here I can see all of the server's
environment variables, all kinds of
| | 03:34 | things about the server.
| | 03:35 | What server software is running on it.
It's got PHP installed, what is the
| | 03:40 | absolute path to my scripts, things like
that that I might need to know in order
| | 03:44 | to configure my scripts and in
order to get them working well.
| | 03:48 | In my usage of it, this is a vital step
in installing any CGI on a web server.
| | 03:55 | Finally, once we have done that then
we can look at installing an application.
| | 04:00 | For example, this is the bw-contact
application that's developed in the CGI
| | 04:06 | Essential Training course and this
is a bit more complicated script.
| | 04:10 | This is about 250 lines of code and it
involves a directory of HTML files that
| | 04:16 | are used as templates in order to
format things on the screen and this is what
| | 04:22 | it looks like when it's running on the server.
| | 04:25 | It presents a contact form and you can
tie things into it and it'll interact
| | 04:30 | with you and it'll send a piece of email.
| | 04:33 | That example is here in the
directory and you can look through it.
| | 04:38 | Some interesting things that you'll
notice about it and we'll talk about this a
| | 04:41 | little bit more later on in this chapter,
you'll notice that it uses a number of libraries.
| | 04:46 | I find it very valuable to take the
parts of those applications that I seem to
| | 04:53 | be using over and over again
and to put them into libraries.
| | 04:57 | You can find a lot of libraries on
CPAN and a lot of libraries are fairly
| | 05:01 | complete and have a lot of these
functions already in them. I've tended to
| | 05:04 | collect my own and you know for some
of that time there might not have been a
| | 05:08 | lot of other choices.
| | 05:10 | But today there are and you can
certainly look at all of those and these are
| | 05:13 | also available in the CPAN repository.
| | 05:17 | So this is an example of a CGI
application that interacts with the user and
| | 05:23 | sends email and in the rest of this
chapter we'll be looking at a couple of
| | 05:26 | other examples as well.
| | Collapse this transcript |
| Using a database| 00:00 | As you're building a web applications,
one of the things you're going to want to
| | 00:04 | do often is to use a database.
| | 00:06 | So, we are going to take a look at
an example of how that's done in Perl.
| | 00:11 | We'll take a tour through a database
application and see the various different
| | 00:15 | functions and how they are implemented in Perl.
| | 00:19 | First, I want to direct your attention
to the RandomQuote application that we've
| | 00:23 | been dealing with throughout this
course, and you'll notice that it has this
| | 00:27 | quotes.txt file and this
is a flat-file database.
| | 00:32 | It's a very simple form of a database,
but as fields and it has records and it
| | 00:37 | has data and that makes it a database.
| | 00:41 | The problem with the flat-file database,
it can be very useful for small amounts of data.
| | 00:45 | But once you get to have any more data
than say what we've got here it starts
| | 00:49 | to become unmanageable.
| | 00:51 | So, that's why there are more
powerful databases available.
| | 00:56 | Here, we have an example of a database
management application that does the four
| | 01:01 | functions of a database:
| | 01:02 | create, retrieve, update and delete.
| | 01:05 | This is a testimonials database
for this web application here.
| | 01:10 | You'll see this little box there.
| | 01:11 | They are testimonials.
| | 01:12 | Well, it's loaded with just the little
silly quotes from my quotes database.
| | 01:16 | But this is where testimonials would be
where you can say oh, your website is wonderful.
| | 01:20 | I love your products, and
it's by S. Dali and M. Twain.
| | 01:26 | This is the application
for managing that database.
| | 01:29 | So you could have hundreds or some
greater number of different quotes in that
| | 01:34 | database and it would be relatively manageable.
| | 01:37 | Here we are just showing five per page,
because we have limited screen real estate.
| | 01:41 | But normally there will
be 10 or 20 or 50 per page.
| | 01:44 | You can look at page 2
and you can look at page 3.
| | 01:48 | You can look at them. Add one.
| | 01:51 | This is a new quote by B. Weinman, and you
can add that to the database. You can edit it.
| | 01:58 | You can say this is a very new
quote and you can update that.
| | 02:02 | Then delete it if you decide you
don't want it in there any more.
| | 02:05 | This is what it makes it easy to
manage a sizable amount of data.
| | 02:10 | So, let's take a look at
how this is done in Perl.
| | 02:15 | Here in Eclipse in the Chapter 17
Web Development folder in your Exercise
| | 02:20 | Files, you'll see a folder called
twotrees and that is that a twotrees web site.
| | 02:27 | Under twotrees, you'll notice
a folder called testimonials.
| | 02:32 | In that folder, you'll see this db.cgi.
| | 02:36 | Here, in this file up at the top you'll
notice that it imports a lot of modules.
| | 02:41 | These are modules that I have written.
| | 02:42 | I have built over the years from all
of the codes that I wanted to reuse.
| | 02:47 | This is what you'll do as well.
| | 02:48 | We'll talk about that process
in another movie in this chapter.
| | 02:52 | But for now I want to direct your
attention to this one here that says BW::DB.
| | 02:56 | We are going to go ahead and
open that. That is this DB.pm.
| | 03:02 | So this is the module
that's being imported here.
| | 03:06 | Use BW::DB and DB.pm.
| | 03:09 | So in the db.cgi file let's take a look
at some of the database operations and
| | 03:14 | see how they're done.
| | 03:16 | If you look at addrec, this is
where record is added to the database.
| | 03:20 | Records in this application
are all kept in a common format.
| | 03:25 | They are all kept in a simple hashref.
| | 03:27 | I pass records around in a
hashref that I typically name rec.
| | 03:32 | So, record is passed into this
function, rec=shift, and that's how we pass
| | 03:37 | things into a function in Perl.
| | 03:41 | So, it's in this hashref,
which is easy to pass around.
| | 03:45 | So, testimonial is filled in from the
cgi and byline is filled in from the cgi.
| | 03:54 | Then we have these two fields in our hash,
and that is passed in this hashref to
| | 04:00 | a function in the database module called Insert.
| | 04:05 | It gets the name of the table,
the hashref with the record.
| | 04:09 | Then over here in the DB module we'll
look for the function that says insert,
| | 04:16 | and there it is and this is what happens.
| | 04:20 | Now insert get three arguments, and
they are self, because all modules get that
| | 04:27 | as the first argument, even
though it's not explicitly passed.
| | 04:30 | Because DB here is an object, when that
method gets called, the first argument
| | 04:36 | is going to be the reference to the
object itself, which is usually called self.
| | 04:42 | Then the table name, and then the
hashref with the name/value pairs.
| | 04:46 | So, I call it here nvpairs.
| | 04:49 | Those nvpairs are then are
read in this foreach loop.
| | 04:53 | So, we get an array of columns and we
get an array of values from the keys and
| | 04:58 | the values in the nvpairs.
| | 04:59 | Then that is taken and an
SQL query is built out of that.
| | 05:06 | That SQL query is then passed on to the
database interface, which is something
| | 05:11 | called DBI, which is used for
virtually all database work or most database
| | 05:16 | work in Perl anyway.
| | 05:18 | You rarely see anything that's not DBI.
| | 05:20 | So, here at the top we have this use
DBI and that is what this $self->{dbh}
| | 05:28 | here is, actually a reference to that object.
| | 05:31 | Then it uses this do method and there we
see all of the description of that do method.
| | 05:37 | The $query is passed which is this
SQL that we have just built and we insert the
| | 05:42 | data into the database.
| | 05:44 | You can see the power of
using an interface like this.
| | 05:49 | We can do things very simply by just
passing around these hashrefs for all of
| | 05:54 | the database operations.
| | 05:56 | So, that's the most complex of them.
| | 05:58 | the rest of them are actually pretty simple.
| | 06:00 | getrec actually does its work with a
simple database query, because it doesn't
| | 06:07 | need to do in a
complicated building of the query.
| | 06:10 | It does this and sql_
select inside the DB module.
| | 06:15 | It will go ahead and create a
hashref, which will have the data in the
| | 06:18 | right format already.
| | 06:21 | Likewise with updaterec, I tend to name
them so that it's really obvious what they do.
| | 06:29 | It uses a simple SQL
query to update the database.
| | 06:34 | It uses a method called sql_do in DB.pm.
| | 06:39 | Likewise, deleterec, which deletes
a record from the database using a
| | 06:44 | very simple SQL query.
| | 06:48 | Finally, we have listRecs.
| | 06:51 | This one is a lot of fun,
because this is what does that paging.
| | 06:57 | Here's the paging logic here.
| | 06:59 | We look at this on the paging function here.
| | 07:02 | We have the ability to go to
the next page and the next page.
| | 07:06 | So, these are previous and next
buttons, where we can jump directly to
| | 07:10 | an individual page.
| | 07:12 | All of that logic is right in here.
| | 07:14 | Because so much of the work is passed
off to these modules and to even other
| | 07:19 | functions within this application,
it really just comes down to this simple if
| | 07:25 | else for handling the next page,
previous page, and the page jump buttons.
| | 07:30 | We simply update a page number with
each of these, we increment it for next page,
| | 07:33 | we decrement it for previous page,
or we update it with a particular page
| | 07:38 | number with the page Jump.
| | 07:40 | Then we do a small amount of math
here to figure out the offset from that.
| | 07:45 | Then we put that into the SQL
using this LIMIT structure now.
| | 07:49 | Every SQL engine has a
slightly different syntax for this.
| | 07:54 | If you are interested, take a look
at the SQL course here on lynda.com,
| | 07:58 | SQL Essential Training.
| | 08:02 | So, this is basically the meat of
the database management application.
| | 08:06 | You've got all the code here, you can
look through it with your knowledge of
| | 08:10 | Perl from this course, and you can
probably understand most if not all of it
| | 08:14 | from that, and get an idea of how you
build a database application using Perl.
| | Collapse this transcript |
| Developing a module library| 00:00 | As you work with Perl, over time
you're going to come up with a number of
| | 00:05 | functions and a number of routines and
number of pieces of code that you like to use,
| | 00:09 | that work the way that you like
them to work and that you will reuse over
| | 00:13 | and over again and you should put those
pieces of code in modules because that's
| | 00:18 | the unit of reuseability that Perl uses.
| | 00:21 | Here is a list of modules.
| | 00:23 | These are some of the modules that I
built over time and you have in your
| | 00:28 | exercise files here all of these modules.
| | 00:31 | These are most of the modules that are
used in my everyday work in the Perl language.
| | 00:37 | So here we've included a
few of them in this file.
| | 00:40 | This is the db.cgi.
| | 00:42 | This is the database management
application for the twotrees web site and in
| | 00:47 | building this application I reused a
lot of code that I've developed over the
| | 00:53 | last 15 years or so.
| | 00:55 | And so what I'd like to do is I'd like
to show you some of these modules, not so
| | 00:59 | that you'll use these modules but
rather you'll get the idea that as you learn
| | 01:05 | to work the way that you like to work
and as you come up with code that works
| | 01:09 | the way that you like it to work, that
you will build your own module library.
| | 01:13 | And your module library will look
entirely different than my module library.
| | 01:17 | And it will work the way that you work,
while mine works the way that I work.
| | 01:21 | Now CPAN, the Comprehensive Perl
Archive Network, has a number of modules
| | 01:27 | available to do a lot of these same things.
| | 01:31 | You're certainly welcome to use those modules.
| | 01:34 | As you do, you'll find that they all
have their own way of doing things.
| | 01:38 | For example, the error reporting, as you
use those modules, you'll find that they
| | 01:43 | all have their own way of doing error reporting.
| | 01:45 | So you'll have to find the errors and
check the return codes of all of these
| | 01:49 | different functions in umpteen
different ways and you may find that
| | 01:53 | frustrating as I have.
| | 01:55 | And so a lot of times like in the case
of this DB module, I've simply taken a
| | 02:00 | library of modules from CPAN.
In this case that would be the DBI library.
| | 02:07 | And I've just wrapped my own code around it,
so that it works the way that I like it to work.
| | 02:11 | Their functionality is great.
It's reliable. It's bug free.
| | 02:14 | It's pretty much the standard way of
interfacing with databases in Perl and yet
| | 02:19 | I would like the error reporting to
be the way that I do error reporting.
| | 02:23 | I would like the way that it passes data
to be the way that I like to pass data.
| | 02:27 | So that all of my code, when I'm
building an application like this, I can do in
| | 02:31 | 500 lines when it might otherwise take
me a thousand or several thousand lines
| | 02:36 | of code to do, if I have to use all of
these disparate modules that do things in
| | 02:40 | all of these different ways.
| | 02:42 | And so as you build your portfolio of
code in the Perl language, Perl has this
| | 02:48 | great module system and so I
encourage you to use it to build a library of
| | 02:53 | modules that work the way that you
work, so that your code can be more
| | 02:57 | efficient and effective.
| | 02:58 | So let's take a look at
what some of these things do.
| | 03:01 | Now know all of these modules if we
take a look here DB.pm, they all start with
| | 03:06 | a use base BW Base.
| | 03:09 | And so what that does is it takes this
base module and it inherits from this
| | 03:15 | base module the common things,
for instance the constructor.
| | 03:19 | I don't need to write this over and
over again for each one of my modules.
| | 03:23 | I can write it once and inherit it.
This init, which is fairly complex and what
| | 03:29 | this does is it allows me to pass all
kinds of values into my constructor and
| | 03:34 | to build a set of properties for the object,
and it use this generalized setter getter.
| | 03:43 | There's a common debug method that
gets imported by all of my modules.
| | 03:47 | Probably the most powerful thing in
this entire library is this simple error
| | 03:52 | reporting, which is these
three functions. error,
| | 03:55 | underscore error, which is only
used internally, the error without the
| | 04:00 | underscore, which is used for getting
the error message and the checkerror,
| | 04:05 | which is used from all of my different
modules for checking errors as they call each other.
| | 04:11 | It just looks very simple here but the
fact that I have this makes my coding so
| | 04:16 | much easier because the weakest
part of Perl is in its error reporting.
| | 04:22 | Even this is a little bit kludgey but
it works and it's consistent and I use it
| | 04:27 | across all of my modules and they
all end up working the same way.
| | 04:32 | So let's take a look at the CGI module.
| | 04:36 | This is one of the most common ones that I use.
| | 04:38 | Now there's a very popular CGI.pm
that comes with your Perl distribution
| | 04:43 | that most people use.
| | 04:44 | It's very big, it's very complicated,
and it does a million different things
| | 04:49 | that in my opinion are mostly tangential to CGI.
| | 04:53 | CGI is a small subject.
CGI is just a few things.
| | 04:57 | It's about setting a query string.
| | 05:00 | It's about using cookies, setting
headers, and it's about displaying the text
| | 05:08 | on the web browser.
| | 05:10 | My CGI module is relatively short.
| | 05:13 | It's about 400-500 lines
including the documentation.
| | 05:17 | It just does these few things that
your average program needs to do with CGI
| | 05:23 | and the other things that you might
find in the larger CGI libraries, I will
| | 05:28 | have those in other places. Like I have
a separate HTML module for like building
| | 05:34 | a select box, for forms, and a
separate address code module which simply has
| | 05:42 | data of two-letter address codes for
states and for countries for building the
| | 05:48 | little select boxes.
| | 05:50 | These are the kinds of things that some
people tend to pile all into one big module.
| | 05:54 | I like to separate them out so that I
only need to include them when I need them.
| | 05:59 | But the point is not for you to do
things the way that I like to do them,
| | 06:03 | but for you to find the ways that work
for you. As you do that, to build your own
| | 06:09 | module library that works the way that you work.
| | 06:12 | That will result in you're being able to
build your applications with less code,
| | 06:19 | spending more of your time looking at
the logic of the application that you're
| | 06:23 | building, and not having to worry
about all of these different ways of doing
| | 06:29 | things, to just be doing things the way
that works for you, and then you'll be
| | 06:34 | able to focus your effort more on
building reliable and powerful applications
| | 06:39 | using your own library of Perl code
that you've collected and reused over the
| | 06:45 | course of your work.
| | Collapse this transcript |
|
|
ConclusionGoodbye| 00:00 | In this course, my goal was to give you
a good foundation in Perl with all its
| | 00:05 | major features and properties so
you can use it to build powerful and
| | 00:08 | compelling applications for
yourself and for your clients.
| | 00:12 | I've covered the basic syntax of Perl,
its use of lists and hashes for data
| | 00:16 | structures, its unique features like
postfix conditionals and loop controls,
| | 00:21 | its modules and its object model and
I have walked you through some real
| | 00:25 | applications so you can see how to
apply your programming knowledge to building
| | 00:29 | your own Perl modules and applications.
| | 00:32 | I've really enjoyed creating this
course for you and I sincerely hope that it's
| | 00:36 | as useful for you as it has been fun for me.
| | Collapse this transcript |
|
|