Coding 101 21 (Transcript)
Shannon
Morse: Today on
Coding 101 it is all about Perl, with our new Code Warrior! It's up next...
Netcasts you love from
people you trust, this is TWiT. Bandwidth for Coding
101 is provided by Cachefly at cachefly.com. This
episode of Coding 101 is brought to you by Squarespace,
the all in one platform that makes it easy and fast to create your own professional
website or online portfolio. For a free two week trial and 10% off use the
offer code: Coding.
Fr. Robert Ballacer: Welcome to Coding 101, it's the TWiT show
where we let you into the world of the code monkey. I'm Father Robert Ballacer.
Shannon: And I'm Shannon
Morse and for the next 30 minutes or so, we're going to get you all coded up on
everything you need to know to be a Perl Code Warrior.
Fr. Robert: That's right and
by the way, Perl... So we're not in Python anymore, we're done with the Python.
Shannon: I am so excited
about this, I can't wait to get learned up on all of
this.
Fr. Robert: Learned up. Now
the cool thing about Perl, Shannon is- We covered C# in the first module,
right?
Shannon: We did, yes.
Fr. Robert: And that was very
structured and it was a compiled language so we had to turn it into a binary
before we could actually do anything with it. Then we did Python, which was an
interpreted language, right? It was dynamically compiled so we didn't have to
worry about running it and making an executable. We're now doing Perl which is
also a scripted language, it's like Python. But it's also more structured like
C# so we're kind of doing that best of both worlds. It's a very useful language
but it's going to borrow elements from both C# and Python.
Shannon: So it's kind of in
the middle.
Fr. Robert: It's in the
middle, yeah. It's like the little baby bear, you know? Not too hard, not too Pythony.
Shannon: Not too soft.
Fr. Robert: Just right. Now
before we do this, of course the first episode of any module has to be, how do
they get started with Python? The cool thing about this is unlike C#, they're
not going to have to download that huge program from Microsoft to get the
visual studio IDE it's not-
Shannon: Can I actually use
Perl inside of Notepad?
Fr. Robert: Oh yeah, it's just
like Python. Anything you have that can edit text, you can write Perl programs
in.
Shannon: So all I need is
the distribution so that my computer can actually run these programs, is that
right?
Fr. Robert: Exactly and I
understand that you are going to tell us how we do that, regardless of whether
we're using Linux, Mac, or Windows.
Shannon: I sure am. So if
we take a look at my computer over here, I have a website up that is called
perl.org and if you go to perl.org/get.html, this is where you can actually
download the distribution. You'll notice here that it says Perl is already
included in Linux and in Mac OS X.
Fr. Robert: I know okay, I get
it. If you run Linux or OS 10 yeah, Perl is already built in and you don't have
to do anything.
Shannon: That's kind of
awesome. But it might be an older distribution so you might still want to go
here and update to the newest version. Also, you'll notice with Windows there
is this one called Strawberry Perl and there's Active state Perl. So Strawberry
Perl is basically an open-source build of Perl and then there's Active state,
which is the one we're going to use for the show today. ActivePerl downloads, you just click the 'Download ActivePerl for Windows' button and then choose whether you're X86 or X64. I'm X64 so I
just download this MSI and then I run it and I'm good to go. That's all you
have to do.
Fr. Robert: Now I'll say this,
I know some people who have really messed themselves up downloading the wrong
version. If you're running a 64 bit operating system, download 64 but
otherwise, don't. In the worse-case scenario, it will install because it's
going to pass the check but then you're going to get funkiness. Perl won't do
anything, so do a quick check. If you're using Windows right click on 'My
Computer,' go to 'Properties' and it will tell you right there whether it's a
32 or a 64 bit operating system, it will tell you which one to download.
Shannon: And if you're
confused about where to find 'My Computer' in Windows 8 like I did you could
just go to the search bar and search computer.
Fr. Robert: Or go to our
Google+ page at g+.to/twitcoding and you can ask
someone there, we promise we won't make that much fun of you.
Shannon: That's true and
speaking of our Google+ account if you go over there, you can even show us your
code once you get really into Perl. Generally, on the first episode we don't
have any viewer submissions that we can share with you but going forward on our
Perl module, we'll be able to show different examples that our fans have showed
us.
Fr. Robert: Exactly, and
that's part of what we really enjoy. Every show and every time we do one of
these programming modules, Snubs starts the show by going into the G+ group and
by pulling out examples from people who are at all different levels of
programming expertise; beginner, intermediate, and expert and we show it off on
Coding 101. It's part of the way we can show you
what's possible even if we haven't quite gotten there in the lessons.
Shannon: So, you want to
get started? I'm pretty excited.
Fr. Robert: I don't know, it's Friday...
Shannon: Oh come on!
Fr. Robert: Okay let's do it.
Now, let's talk a little bit about Perl okay? Perl, if you did the first two
modules, C# and Python, again it's going to be somewhere in between. It's an
interpreted language, it's a scripted language which
means I don't have to turn it into an executable, right?
Shannon: Yeah, that's
awesome.
Fr. Robert: Because like
Python, I can write it in Notepad, I can write it in V, I can write it in Emax. You pick your text editor of
choice, and that's what you're going to be writing in and all you have to do is
run or script the program in real time and it will tell you whether or not it
works.
Shannon: That's so cool, so
all I have to do is open up Notepad, write my code, and then save it as a .pl?
Fr. Robert: .pl right. The extension tells the computer that this is a
Perl file and that it should be using that extension, or distribution, to go
ahead and process what's in it.
Shannon: Got it, cool.
Fr. Robert: The other thing I
remember about Perl- And our Code Warrior is going to touch on this. -Is Perl
is one of those languages that is absolutely suited
for web work.
Shannon: Really?
Fr. Robert: It really is, it really is. It's very good with manipulating data, it's
very good with dealing with information that is both going and coming from the
computer on the internet so keep that in the back of your mind. As we are doing
these lessons, ultimately our Code Warrior is going to teach you how to use
this to... I don't know, say, generate forms on a webpage or properly process
information that's coming off of a webpage.
Shannon: So is that the
reason why Perl was made?
Fr. Robert: No, Perl was made
because there was a bunch of geeks who said, I don't like any of the
programming languages, I want Perl. That's pretty much how all programming
languages are made. Let's get back to- I'm going to start a Holy War and I
don't want to do that. The first thing that we want to do when we talk about
Perl is we want to talk about the control structure. Remember C#? C# was unique
because we could write the entire program on a line, as long as we separated it
with semicolons remember? Python was different. Python took white space into
consideration.
Shannon: Right, that's why
we have to put the tabs and that's why we entered between each line.
Fr. Robert: Exactly. Right,
which was important because if you didn't format it right, even if the proper
words were there, the proper commands, if you didn't make it indented or if you
didn't put it on a new line Python wouldn't know how to deal with it and you'd
get an error.
Shannon: I remember that.
Fr. Robert: So Perl, in this
instance is more like C# than it is like Python. You need to end every
applicable line with a semicolon so it knows that this is a code block and I
execute this before I move on to the next.
Shannon: Okay.
Fr. Robert: So let's go ahead
and take a look at what a code block inside of Perl looks like. Go ahead and
switch to 'My Computer' and don't worry if you don't understand everything
here, we're going to explain it eventually. Right now what you see me doing is
I'm declaring two variables and of course-
Shannon: Numbers and
string.
Fr. Robert: Numbers and
string, right. I'm filling the numbers variable with 1, 2, 3, 4, 5 and I'm
filling the string variable with- This is a string. -I end each one with a
semicolon.
Shannon: And I also notice
that with the string one you have that in quotes and you start each one with a
$.
Fr. Robert: Yeah, yeah we're
going to go over that. But this is what the general structure looks like. Now
if I wanted to, I could delete the lines. In fact, you're going to see this a
lot when you deal with pro programmers, especially obnoxious pro programmers.
They tend to like to write everything in a line.
Shannon: Oh my God that
would drive me nuts.
Fr. Robert: Yeah.
Shannon: I like the organization.
Fr. Robert: I like the
organization. But you don't need that. You could write this on a line but
please, please just follow best practices and keep it nice and easy to
understand. Alright, now that you've seen the basic structure and what it looks
like, and hopefully you've made the comparisons to C#. Let me actually show you
what I've done here. If you come back to me, what we're going to be doing is we
need to first show you how you work with variables in Perl.
Shannon: Yay, variables!
Fr. Robert: Okay, now...
Shannon: Please tell me
it's easy.
Fr. Robert: It's so easy. Do
you remember back to C# when we had to declare variables of a certain type so-
Shannon: Yeah, you had to
say if it was a string or an integer or whatever.
Fr. Robert: All those good
things. So you had to specifically say at the beginning, before you started
doing any expressions, these are my variables and these are the variable types,
right? That's part of C#. Then we got to Python and Python- I didn't have to
tell you what kind of variable it was.
Shannon: It just knew.
Fr. Robert: It just knew
right? It does context and also, I didn't have to declare all of my variables
in the beginning, I could do it in the expression. As I need the variables, I
just put them in there and Python just allocates memory.
Shannon: Which I really
liked.
Fr. Robert: Which is nice,
because Perl, even though the structure is more like C# the variables and
declaring the variables is more like Python.
Shannon: Oh I like this so
far.
Fr. Robert: Right so I don't
have to declare a type or all of the variables at the beginning, I could just
use them as I write the expressions and it will be allocated within Python.
Shannon: I'm excited now.
Fr. Robert: Yeah, and when we talk about variables in Perl there are a couple of different types.
Most notably, there are scalers, there's
arrays, there's associative arrays, and there's hashes. We're only going
to talk about the first two. Let's not get confused, we're talking about scalers and arrays.
Shannon: Okay, what is a scaler.
Fr. Robert: A scaler, quite simply is like any other variable that you've
written in Python to hold a string or number or a character.
Shannon: Oh okay.
Fr. Robert: These are either
numbers or strings. They don't require you to mark the variable as a number or
string it all looks the same to Perl and it actually will infer from context
what kind of variable it is. So for example, if I put something in quotations
and write a sentence, Perl will figure out that's a string. If I write 1, 2, 3,
4, 5 Perl will figure out that's an integer.
Shannon: Wow, that's easy.
Fr. Robert: If I write 3.14
Perl will figure out that's a real number, it's a float. Perl also doesn't
require you to differentiate between integers or real numbers, which is nice
and we'll get to that later on. But when I use scalers all I have to do is I have the name of my variable- So let's say var1, and
right before the variable, I put a $.
Shannon: Why do you put a
dollar sign?
Fr. Robert: Because that
indicates to Perl that this is a variable.
Shannon: Oh okay so that's it's indication.
Fr. Robert: That's it's indication, whatever is connected to this $ is going to
be my variable, that's my variable name. That's both how I declare it and how I
call it. So if I call something "$var1" anytime I want to use var1, I
have to proceed it with a $.
Shannon: Oh I see.
Fr. Robert: Okay so just
remember that. Consider that part of the name, it's not var1 it's $var1 right. Now if you go ahead and let's go ahead and go back to my computer,
here you see I have created these variables, numbers-
Shannon: That's why you
have the $.
Fr. Robert: Right. This is
$numbers and this is string. This is actually the name and $numbers I fill with
1, 2, 3, 4, 5 and $string I fill with, This is a
string. Now you remember how we printed in C#? The see out, and there was a function call and it was kind of-
Shannon: Print line...
Fr. Robert: Yeah, and Python
was a whole lot easier, right?
Shannon: Yes.
Fr. Robert: Again, this is
where Perl is more like Python. This is what printing looks like, print. You
just print and then you could either put the name of a variable, you could put
numbers, or you could put something in quotations, which is a string.
Shannon: Cool.
Fr. Robert: What I've done
here, this is a unique piece and it's different from Python so you got to
remember this. I've put, print = "This is me printing a String:
$string/n";
Shannon: So even though
you're putting that inside of quotes, it knows because you've got that $ that
you're not telling it to print $string, you're actually telling it to print-
Fr. Robert: The variable
string, right. And then I do it again for this one; print = "This is me
printing a variable with numbers: $numbers"; So by putting $numbers, it knows to go ahead and print the value of the variable
numbers which is 1, 2, 3, 4, 5. Now this, <STDIN>; is just the standard
input, just like in Python-
Shannon: So it's like
enter?
Fr. Robert: Right, it's
waiting for input from the user and then the character turn, the enter key. The
reason why I'm going to put this at the end of my program is because I don't
want the window to close. Because otherwise it would just
flash. So this is going to wait for me to hit enter before it closes.
Now if I run the program, because I've got Perl installed on this, it'll just
do this, This is me printing a string: This is a
string
This is me printing a variable with numbers:
12345 and each time it is going to go ahead and print what was inside that
variable and now it's waiting for me to hit enter. That's it.
Shannon: Pretty obvious.
Fr. Robert: Pretty obvious,
very simple. I'm going to hit enter and it's going to close the window.
Shannon: Cool.
Fr. Robert: Okay so, in that
sense, printing and playing with variables in Perl is pretty darn easy, there's
not a whole lot there and it is basic. Hopefully if you've watched any of the
other modules, especially the Python module, this should be an old hat right?
Shannon: Definitely. But of
course, if you haven't watched any of the earlier modules, definitely go back
and watch them because they're super useful. Especially the
first module because we defined a lot of the terms that we're using now like
variables, integers everything like that.
Fr. Robert: Right. I want to
show one more thing, if you'll go back to showing my screen, remember how I
told you how you don't have to declare what type of variable you were printing
right? This is a really good example of that. So it knows that here, I'm
creating a scaler variable because I've put a $. And
I've called it var1, var2, var3 and I'm filling it with an integer, 28. I'm
filling it with a string, 'Coding 101' and I'm filling it with a floating 3.14.
Nowhere have I declared that the first is an integer, that the second one is a
string, and that the third one is a floating point.
Shannon: Yeah, you just put
them in.
Fr. Robert: Which
is nice. This is really how I like programming to be done, where it can
figure out what I want. I'm just going to go print them out print Var1, print
Var2, print Var3. The little \n thing after the Var1,
2, and 3 commands-
Shannon: That just tells it
to enter to a new line at the end of that line of commands.
Fr. Robert: Right. If you go
to a Perl start-up document, you're going to see that it gives you a bunch of
control characters. You could add a new line, you could actually append to the
line, \n just says drop down to the next line. So if I run this it will look just
like this. It's going to
clear 28
Coding 101
3.14
and then it's going to
print those three and so I then, get
28
Coding 101
and
3.14.
Shannon: Easy.
Fr. Robert: Easy peezy lemon squeezy. Nothing to it, right?
Shannon: Okay, I'm ready to
move on.
Fr. Robert: Let's move on to
the slightly more complicated but not much more complicated arrays. Arrays are
sort of like in Python what we did with lists. So what I have is the name of an
array and it has an index. That index tells me what element within that array
I'm dealing with. Here's something unique about Python. When we're talking
about an array in Python, we're talking about a one-dimensional array. It's
just a list of elements that belong to that array.
Shannon: Just a list,
that's it.
Fr. Robert: Just a list. We
can go to more complicated data types later, but right now we're just dealing
with that one-dimensional array. Now, this is what it looks like when I use an
array if you'll go back to my screen I'll show you. Notice how this character
is different.
Shannon: Whoah, that's a lot of stuff.
Fr. Robert: What did I use for
declaring a scaler variable?
Shannon: Oh why is there an @?
Fr. Robert: Yeah, it's
different. So when I'm in Perl, if I use a $ it's declaring that I'm using a scaler variable. If use an @ it
means I'm declaring an array.
Shannon: Oh okay.
Fr. Robert: And just like in
Python, what I've done here is I'm declaring this array called C101 and then
I'm filling it with three values; Padre, Snubs, and Brian. So three different
values, or three different strings go into that array called C101.
Shannon: Ah I see, okay. So each one is a separate string.
Fr. Robert: Each one is a
separate string, but they are all part of that array. The way I can prove it to
you is I have right below it it says as follows,
print "Here are the values in the array 'C101' :\n"
; So look at the print.
Shannon: It's the $.
Fr. Robert: It's the $, right. Because you can think of an array as an array of scaler variables. So even though it's an array, each
element itself is a scaler variable.
Shannon: Oh that's cool.
Fr. Robert: So you see how
this kind of works. So if I have an array called C101 with three elements;
Padre, Snubs, and Brian what that really means is that I have three scaler variables. Each one would be called $C101 and then
in the brackets, either 0, 1, or 2 because remember we
start at 0, right? Those are my three scaler variables inside that array. So when I hit this what it should do is it should
say, Here are the values in the array and it should
say Padre Snubs and Brian. So let's go ahead an run it, and you'll see it like that. Padre Snubs and Brian.
Shannon: It works!
Fr. Robert: It works, and this
proves to you that the array is just three scaler variables.
Shannon: Right.
Fr. Robert: Or as many
variables as you put into it. If I had put twenty values in there, we would
have had twenty scaler variables.
Shannon: Cool.
Fr. Robert: Alright so let's
look back at the code because it actually continues. What we have here is- This
is kind of cool.
Shannon: Whoah, what's going on here?
Fr. Robert: So I've created a
new array called @coding and @coding is equal to
@C101.
Shannon: So the first
array, the @C101 is equal to that coding array.
Fr. Robert: Exactly. It means
that the coding array is going to create an exact copy of @C101 array.
Shannon: Why would you do
that?
Fr. Robert: Well because
sometimes I want multiple copies of the same array. Like for example, remember
when we were doing the sort in Python?
Shannon: Yeah.
Fr. Robert: What we wanted was
the original array and the sorted array because the sorted array was going to
change. Well this is a very nice way to do it because I can create an array
that is just an exact duplicate of another array and then I've got a copy so
when I do something to it-
Shannon: It's a copy, it's not deleting the original array.
Fr. Robert: Right. So going
back to my code, what I'm going to do after this is I'm going to say the values
of the array Coding101 have been copied into the array, Coding. And then I'm
going to print the values of the array, which would just be Coding0, 1, and 2
so let's go ahead and run that.
Shannon: So that would just
print the same thing over.
Fr. Robert: Right, exactly. So
go ahead and zoom in on that Bryan because I know you're like Superman like
that. So when I hit enter, it's going to print me the values of the coding
array but it'll be exactly the same since I just duplicated it. And boom. Easy. Let's go now, to the last step. There's one more step
before we say goodbye to arrays and that is that we need to change an element
in the array.
Shannon: Ahh! You're confusing me...
Fr. Robert: Wait, here we go.
We already know that an array is just a list of scaler variables, right?
Shannon: Right.
Fr. Robert: And we know how to
print individual scaler variables, right?
Shannon: Yes.
Fr. Robert: We also know how
to assign individual values to scaler variables, go
back to my screen and you'll see it right there. What I've done is this...
There's the array called C101 and the scaler variable
for the third element, which actually is number 2 is
going to change to, cranky hippo.
Shannon: Oh, so you're just
changing the second... What was it called again?
Fr. Robert: The element. It's
actually the third element because of the 0 on the first. So when I print this,
now it's going to show me this... In the meantime, the array, coding stays the
same.
Shannon: Ooohh...
Fr. Robert: So yeah, after it
prints the changes made it will print the original array that we made a copy
of.
Shannon: Oh, that's easy!
Fr. Robert: I went over it
really quickly, but just remember-
Shannon: But I get it.
Fr. Robert: Yeah but the nice
thing about this is Perl is very good at making your data types easy and the
array is just an array of scaler variables so how do
you refer to an array? You refer to it with the @. How
do you refer to scaler variables?
Shannon: $.
Fr. Robert: So if you are
referring to the elements inside of an array, which are just scaler variables, you refer to them with the $.
Shannon: Cool, okay.
Fr. Robert: If you're confused
right now our Code Warrior is going to clear all of this up. Now Shannon, before we go to the Code Warrior, I'm thinking that maybe- I
feel like I want to hear a little something something about a company that might help me to present my ideas. Like for example, the
lesson that I just did.
Shannon: I know of a site
that could do just that. It's called squarespace.com and if you've never been
there before they are the all-in-one platform that makes it super easy to
create your own professional and beautiful looking website. And it's easy to
use, that's the best part. Some of the reasons that you're going to love Squarespace, because I know you will, they're constantly
improving their platform, they have all these new features, new designs and
really really good support. So let's talk about those
beautiful designs okay? They have over twenty-five beautiful templates, tons
and tons of different examples are on their website so you can check them out
before you buy. You can also build them quickly so you can build your own
professional or business website. They also have the best mobile experience so
if you get on your phone and go to your website, it's
going to look good. It's not going to look all jumbled and strange as if it's
just supposed to be on a laptop. It'll look good for your phone, your tablet,
or your computer or any kind of device that you want to use it from. Squarespace is easy to use too but if you do need some help Squarespace has live chat and they have email support
24/7. There was actually one time I had to use their chat systema and somebody was on it immediately. I was just like, hey answer this question.
And they were like, alright I got you. And they just fixed it for me, super
easy. Plus, there's a completely redesigned customer help site for easier
access to self-help articles and video workshops just in case you actually
don't want to talk to anybody. Hosting is included, Squarespace takes care of the hosting with a distributed network that does not go down and
they're inexpensive! They're not going to charge you like an arm and a leg or
like your first born to actually use their site. It
starts at just $8/month and it includes a free domain name if you sign up for a
year which is what I do. Even their code is beautiful, Squarespace takes the time, and they take just as much pride in their back end code as they
do in their front end design and they have a developer platform so if you
really want to dig into the site customization, in the developer accounts that
never expire in development, they take as much time as you need before
launching your site so if you feel like you've got to fix this one thing to
make it gorgeous, take your time it's all good. And pay nothing until you
actually go live with your site to the public. So some of the
developer platforms feature complete control so you can edit all the code that
affects the display of your website. Every line of
html, CSS, JavaScript all under your control. They also have GIT and
SFTP repositories so they have version control that comes standard, you can
connect to template files via GIT and SFTP and they have developer tools like
LESS Preprocessing, they have JSON Templating, Script Comboing, Retina-ready Responsive Image Handling,
hello Retina-ready, and tons and tons more. So start a free two-week trial
where no credit card will be required and you can start building your own
website. Once you try it out and realize you love it, you'll most likely decide
it's about time to sign up for SquareSpace and when
you do, make sure to use the offer code: CODING and get 10% off and show your
support of Coding 101. And of course, we thank SquareSpace for supporting our show over here, Coding 101. Remember, a better web awaits
and it starts with SquareSpace.
Fr. Robert: Thank you SquareSpace.
Shannon: We love you!
Fr. Robert: We love you. Now
this is the time of the where we're supposed to introduce our Code Warrior but
unfortunately we couldn't find one.
Shannon: What?
Fr. Robert: Yeah, we couldn't
find one in the internet but what we did was we found one here at home.
Shannon: Ooooohhh....
Fr. Robert: Ladies and
gentlemen we welcome to you, the latest and brand new Coding Warrior in a line
of code heroes, Mr. Patrick Delahanty. Patrick, thank you very much for being our Code Warrior.
Patrick Delahanty: Oh
my pleasure, I've been looking forward to this.
Fr. Robert: Now we asked you-
Obviously you were an easy choice for us because we work with you every day
that we're in here but also because you program Perl for a living. You're not
just an instructor.
Shannon: Oh is that what he
does here?
Fr. Robert: Yeah, that's what
he does. That little office over there, that's all he does all day.
Patrick: I've been doing
Perl since like 1985, 1986. I've even got a Perl book by Randall Schwarts that is copyright 1996 and it has a menu in it
from 2002.
Fr. Robert: Which by the way
folks, there were a lot of you who were saying we should get Randall Schwarts for Perl. We will have Randall Schwarts for Perl when we start getting to 102 but we didn't want to use him for the
basic stuff because that's... He's, he's Randall Schwarts.
Patrick: Yeah, I bow to his
knowledge.
Fr. Robert: He's like a Code
Emperor. He's not a Code Warrior. But Patrick, so you've been telling me for
the longest time that I've got to get into Perl and that if I love Python, I'll
love Perl and if I get C#, I'll get Perl as well. But why did you get into Perl
in the first place?
Patrick: Well I started
learning Perl when I worked for internet.com and we were doing websites that
needed dynamic back ends and dynamic pages so we used Perl to create the CGI
scripts that would present all of the data to the users. It was so easy to pick
up and it's just like a natural language because of how you could think about,
oh I want it to do this. And that's how you would structure the statements.
Fr. Robert: Yeah. I will say-
Thank you for doing this. -I understand that you really kind of you pulled back
the original slate that you gave to me that you really wanted to cover with the
Coding 101 audience, was maybe a little ambitious.
Shannon: It was over my
head.
Fr. Robert: Yeah it was one of
those things that's like, that's not 101 Patrick so thank
you for- I'll say it. -Kind of dumbing down your lessons a little more.
Shannon: For me.
Fr. Robert: You wanted to
introduce us to a few things that you think our Coding 101 monkeys should bring
home and play with. You want to introduce us now?
Patrick: Yeah, we're going
to start off with just more about the strings and other variables and so I've
got Perl on my Mac here, didn't have to install anything.
Fr. Robert: Okay yes, yes I
know...
Shannon: Jealous...
Patrick: I can use Notepad
or Text Edit or anything, BB Edit or whatever really. But what I'm going to do
here is bring it up in the terminal and so I'm in the terminal.
Shannon: What...? Easy.
Patrick: And this is- If
you know Unix commands like LS and everything, that's
all in there. So I've got my files here and I'm going to use Emax as my editor just so I can have it all on one screen.
With this program, you can see that I've also demonstrated some comments, which
all you have to do is put the number symbol at the beginning of the line. Now,
at the start of my file, which I didn't see in yours I tell Perl where it is.
Fr. Robert: Right so with
this, when we start moving into actually moving our assignments into the web we
will always have to have that line because that line is what's going to allow
whatever's reading it to understand where this particular piece of code should
go. We didn't have to do it on ours on Windows because we were running it
straight in a Perl environment. But eventually, we will want to include that in
there.
Shannon: Okay.
Patrick: And when I've done
Perl coding on Windows servers I've had to put in the C: and all of that. So
what I've done here is I've made an array that has seven of our shows, the
first seven listed alphabetically. And you can see that the spacing doesn't
matter. I've put it all nicely formatted.
Fr. Robert: Right. But you
could have that all in one line if you wanted to be a jerk about it.
Patrick: Yeah I've got
commas between them all here and then close it. Then I've defined our network
name and our network equals twit. Then I've picked a number, so I've selected
the number 3 and my print statement is simply, subscribe to, and then the
content of that array with this number. So I didn't put the actual number in
the print statement.
Fr. Robert: So yeah, just like
when we did the statically defined element number. What you've done is you're
saying the array called 'shows'- But we're addressing a single element so of
course we're looking at it as a scaler variable. -Use
the scaler variable called 'selected' to tell me
which element I want to look at.
Patrick: Yes. And so I
could easily change that variable, maybe there's some outside thing that would
select a different value and it would print out a different show name. And so
you can probably predict what this is going to print out when I run it. So let
me exit out.
Shannon: I know!
Patrick: Perl episode 1...
Fr. Robert: And while you're
typing that we've got some great questions in the chat room, DoctorMorgiss wants to know what the dollar signs mean.
It's just scaler variable, any scaler variable uses the $. And someone asked, wait you can put the variable names
inside of quotations? Yes you can, that's one of the cool things about Perl, you don't have to end the string, put the variable in and
restart the string. You just drop it in there. As long as you've got that scaler variable indicator at the front, the $, it knows to
print it.
Patrick: Yeah, there's so
many different ways to do things in Perl and I'll show off a couple different
ways to print in a little bit but here, I've executed it and it says,
"Subscribe to Coding 101, a fine TWiT show!"
Shannon: Aw, that's cool.
Patrick: So let's go back
in and what I'm going to do here is that something to keep in mind is that Perl
is case-sensitive. So if I put network equals frog pants and then network
equals adequate.com, which one do you think it'll print out?
Shannon: It'll still print
out the first one, TWiT, right?
Patrick: Yes, because it is
case-sensitive so they're treated-
Fr. Robert: You've actually got
three different variables there.
Patrick: It still says, 'A
fine TWiT show!'
Shannon: So it's just
ignoring the two other variables?
Patrick: Yeah, they're
treated as completely separate. So that's tricky when you're coding things-
There have been so many evenings where I have been trying to figure out why
this isn't working and then I'm like, oh this one has a capital letter and this
one doesn't. So it's a little quirk with Perl but-
Fr. Robert: But that's to be
expected, I mean, at this level if you're coding then you need to pay attention
to capitalization.
Patrick: Yes. Now let me go
into another example here. In this one I've got two different ways to print. So
it's similar to what we mentioned earlier. There's the original way, which is
on this first print line. But on the second line I've actually ended the quote, I've put in a period, put in the variable name,
another period, and then continued it. But this is helpful if you have- Let's
say there is a variable and you wanted the word to begin right after it so it's
like how else would you say, hey separate these two.
Shannon: So that way Perl
knows that's not the entire name of the variable and that should be separated.
Patrick: Yes, so if I
wanted it to end right here, I could do this and take the space out and then it
would print the variable with no space. But if they were together like this, it
would think it's a different variable.
Fr. Robert: Yeah it would
actually think you were talking about a variable called 'networkshow,'
which it doesn't have a value for at the moment.
Shannon: That's good to
know. Okay.
Patrick: So now let's
execute this, it prints the exact same thing with two different print
statements.
Shannon: Cool, I like it.
Patrick: Now I actually
have a crazier example here because you might wonder, well what if I want to print something that has quotes in it?
Fr. Robert: This always gets
people, when they start putting punctuation- Especially when something comes up
where they want to print a $ and they don't want it to think that you're
calling a scaler variable.
Patrick: Yeah, so here I've
put quotes around this but I can't just put a regular quote because if I do
that, it's going to be like, oh you're ending the thing but then it crashes
into this so I escape it by putting the backslash before the double quote. Down
on this line, I've done the same thing but it's still separated. Here's another
way to do it but you'll see the output is a little different for this line
here. Where I have the single quote and I didn't have to escape this because
there's no double quote at the beginning.
Fr. Robert: Oh, Maxwave wants us to explain escape. Maxwave,
when you escape you're essentially telling the compiler or interpreter that's
going to be looking for this code not to execute this next part. Don't execute
this, this is actually a regular part of the string and I'm not telling you to
do a command. If you didn't do that, Perl would think that you're ending the
string and then it would look for something else to execute. The escape escapes
out of the end of the string.
Shannon: So you're telling
Perl, no really this is what I meant to do.
Fr. Robert: Yeah, I just want
you to put this, don't mess with me.
Patrick: So on this fourth
print line, I've got still the single quote but I've separated it out just like
the second one. I ended the quote here and started this variable. So 4 and 2
are very similar but 4 has the single quotes. And then
this fifth example here gets a little crazy. Because I'm telling it to print
until it sees EOF.
Shannon: What is EOF?
Patrick: End of file. It
will print until it sees that so I can actually go in and put in returns and
hey this is even more- And as long as this EOF starts on a new line...
Shannon: Oh...
Fr. Robert: Now Patrick, I
will say what's interesting about this is, this is a very simple example- This
was designed to help our users figure out some of the concepts we just gave
them but the fact that you're using EOF tells me that you're pulling from your
experience of pulling external data files. Eventually this technique could be
used to pull from a file that's not on the computer. It's not even on your
network, parse it, get the information out of it and then do something with it.
Patrick: Absolutely, but
let's not get ahead of ourselves here.
Fr. Robert: Let's not get
ahead. But in case people were wondering what the EOF was for, that's what you
would use it for.
Shannon: Cool.
Patrick: And so let me exit
out here and we'll run this to see the difference here. So yeah, this is the
second line, it did the same thing as before. Now the third one, presents this and this had the single quotes around everything and it did not
interpret this variable or the network variable it just printed it. It didn't
care if there were variables.
Shannon: Yeah, it took it
literally.
Patrick: Yeah it just went
and printed it so, it ignored that. Even the new line, that's why it's all run
together here.
Shannon: Got it.
Patrick: It's like I don't
care about all this other stuff, I'm just going to put the text there.
Shannon: So quotes make a
huge difference in Perl.
Patrick: Yeah, now this one
is the fourth one. Because I separated out this variable, remember I ended the
single quote, had the variable name, and then started it back up. It actually
interpreted that. So even though this part is in single quotes, this part was
not so it prints the value. Now here's the start of the final example, the EOF
example. You can see it does the new lines, it does everything, it doesn't care and will put in all of the values so this is
very helpful, especially when I'm presenting this as webpage content. Like if I
have to do a big chunk of html, rather than having to escape all of the quotes
and any of the $ and all of this other stuff I would have to worry about, it's
a whole lot easier to just do the EOF method and put in a big chunk of text.
Fr. Robert: Patrick, thank
you. Thank you very much. I think that might be good for the first episode,
give people a good taste and feel. Although we do have a good question from DanS in the chat room and that is, what's the craziest
thing you've done with Perl recently?
Patrick: Oh me, let's
see... Yesterday, I did a thing that interfaced with a URL shortener so that we can make a bunch of short URL's for TWiT.
So you can go to twit.2/c101 and it redirects to Coding 101.
Fr. Robert: Of course, that's
what you do with Perl. You do useful things. Patrick Delahanty,
our Code Warrior, can you tell the folks where they can find you like your
Twitter address or your website, where should they find out more about you?
Patrick: On Twitter, I'm @PDelahanty and my website, enemycodes.com is coded entirely
in Perl. Except the forums, which I didn't code, but everything other than the
forums is all Perl.
Shannon: That's awesome.
Fr. Robert: And we thank you
very much, we will see you next time when we need to call on our Code Warrior.
Patrick: I look forward to
it.
Fr. Robert: Shannon, that's a
lot for the first episode I think.
Shannon: I have a lot to
work with.
Fr. Robert: You have a lot to
work with. And we want to give our audience a little bit of time to flex, for
many people who already have a coding background, I know this is really simple
but please bear with us this is what we have to do. We always want to reach out
to the people that have never coded before.
Shannon: I'm glad we do
because a lot of times, the first episode is what gets me excited about a
language because I'm like, oh I really can do this yes! That was easy to
download.
Fr. Robert: Your takeaway
should be it's very easy to work with variables and it's very easy to print.
Those are two very important things and we just gave it to you.
Shannon: Done and done.
Fr. Robert: Done and done. Now
if you want to find out anything about anything that we talked about today,
you've got to go to our show page. There you'll find the episodes and also our
GIT hub, right?
Shannon: That's right we
put all of our code in our GIT hub and we also separate them out by modules so
you can easily find every single piece of code that we show in our episodes. We're
also on iTunes if you use iTunes for all of your RSS feeds, or podcasts,
definitely subscribe to us over there and share it with your friends if you
think they would be interested.
Fr. Robert: That's right and
if you want to get more involved, as we mentioned at the beginning of the show
you've got to join our G+ group. Drop in at g+.to/twitcoding.
You're going to find a vibrant community, over 1,000 people who are at all
levels of programming expertise from the noobs to the
super, super gurus. So if you've got a question, you want to answer some
questions or, if you want to show Shannon the code that you wrote that you
think we should show off, that's where you go.
Shannon: Definitely and
I'll be sharing my own code on the episodes as well. Of course, if you aren't
on Google+ and you're like, oh I don't like social networks I'm on Twitter.
We're also on Twitter and we're on there pretty much
constantly so you can find me @snubs.
Fr. Robert: And I'm @padresj. I'd say until next time I'm Father Robert Ballacer.
Shannon: I'm Shannon Morse. End of file.
Fr. Robert: End of line. Do
you want to do a podcast?
Shannon: Do you want to
make a program? Everyone programs with me, do you want to make a program? Let
it code, let it code.