Coding 101 24 (Transcript)
Shannon Morse: Today on Coding 101: The most
important thing you will ever learn in programming. Also,
dogs.
Netcasts you love from people you trust.
This is TWiT!
Leo Laporte:
Bandwidth for Coding 101 is provided by CacheFly. At C-a-c-h-e-f-l-y.com.
Father Robert Ballecer: Welcome to Coding 101, it’s the TWiT show where we
give you the knowledge to live in the wonderful world of the code monkey. I'm
Father Robert Ballecer.
Shannon: And I am Shannon Morse, and for the
next thirty minutes we're going to get you all learned up on everything you
need to know on how to be a code warrior.
Fr. Robert: Speaking of code warriors, Shannon,
our codewarrior has been giving our folks at home a
challenge the last couple of days with regular expressions, right? I mean, it’s
been kind of a long slog.
Shannon: Regex is a little bit confusing.
Fr. Robert: Its one of these things, where I
think everyone sees why it’s important, everyone understands why it’s something
they need to learn, but at the same time there are so many modifiers and so
many different ways to attach these different operators that people kind of get
daunted by it a bit.
Shannon: Absolutely. You know, it was so
funny, the other day I was working over at Hack5, and my coworker Darrin. He
pulled up a terminal and he started putting in regular expressions, and I was
like, "So, this is totally cross platform. This isn't just in perl. It’s so cool!"
Fr. Robert: Look, if you've got a mac, its built in. Yes, I know that. But, for example, I'm using
a Windows pc, and I've perled an installment which
means I can use perl regular expressions at any time
to do searches through my entire computer or any drives that are connected. So,
I mean, think of it as your own private Google.
Shannon: Oh. That's beautiful. That's
perfect.
Fr. Robert: Yeah. But, if you're going to do
that, you have to learn how to use it.
Shannon: You do. Which is
why last week that's all we did. All we did was talk about regular
expressions and today I wanted to show you a very quick and simple example
that's similar to the one that padre showed us last week, to give you a really
quick review. So, if we want to pull up my computer real quick, I'll go ahead
and show you the one that I created. So I just called it Example Episode 47. So
this asks, "Name your favorite youtubers."
Fr. Robert: We've got a standard input, and
open. And we know that that's a regular expression. He's taking standard input,
dropping it into a variable called 'quote' and then he's using a regular
expression to look through quote.
Shannon: Yes. So we are searching for
spaces, we are changing those spaces to enters.
Fr. Robert: New lines, right?
Shannon: New lines, yes. And then this is
global, so it’s going to look through the entire standard input. And then when
it outputs, it’s going to output each of their different youtubers on a different line. And then I do a second one where I print out, "Who is your favorite twitter folk?" So you put in standard
input of all the different twitter people that you really like to follow. It
does the same thing, then it says, "You said
quote." Now, this quote, let me show you what it
looks like. I'll pull that up. Okay, so favorite youtubers,
we'll say, TWiT revision 3, and Padre and Hack5. So
when I hit enter, and each one appears on a new line. And then it says what are your favorite twitter folks? So you know, I follow TWiT, so of course, Leo Laporte. And I also follow @snubs, that's me. And @mistersnubs.
Fr. Robert: Hi Darrin!
Shannon: So, when it does that, it says,
"You said Leo Laporte, snubs, and mistersnubs. You should also check out twit coding
101."
Fr. Robert: We just hard coded some advertising
into that program. Well done, very well done.
Shannon: Now another thing that I could also
do with this is I could put, if they entered TWiT Coding 101, I could say, "Oh, well you should also follow this one."
So I can see how this could be used on websites to make you purchase other
things, like, say I want to go on Modcloth and buy
this dress. And then they say, "Oh, well if you like this dress, you might
also like this thing." So that could totally be used as a regular
expression.
Fr. Robert: Well this is one of the things that
we've tried to explain. The power of the regular expression, and this is why if
you've done any search engine backend research, you understand this is what
they use, because they don't want to just match the word. They're looking for
particular strings. For example, Patrick loves, for some reason, rabbits.
Shannon: For some reason? They're adorable!
Fr. Robert: They are adorable. But there are
many different strings and many different data sets that may include
"Rabbit" in different capitalizations and different combinations. You
want a command, you want a method, you want a class that will allow you to look
through everything for that particular pattern, no matter what its attached to. And then you can make recommendations based
on patterns that are found. That’s the power of regex.
Shannon: It’s so cool! It gets me super super excited about coding. Yay, I love this stuff!
Fr. Robert: Dr. Mobius is not excited about
Regex but I think that's just because Dr. Mobius is dead inside.
Shannon: Hey, we've got a viewership example
too, in our Google+ community. Do you want to check it out?
Fr. Robert: Yes please.
Shannon: Awesome. Okay, so this one comes
from Scott. He posted an example that is basically a rock, paper, scissors game
with the computer. It totally makes me feel like the computer is going to
cheat. So, I click on rock, paper, scissors. I'll go
ahead and show you what it looks like. It says, "Rock, paper, scissors -
best of three. Please select for game number one. Rock,
paper, or scissors." I'm going to say three, for scissors. It says,
"Rock beats scissors, I win!" I swear its cheating. So this time I'll
say rock. Hit one... It says, oh! It’s a draw. He picked rock too. Yes.
Fr. Robert: Wait a minute, I'm seeing a pattern
here...
Shannon: Uh oh. Okay I'm going to choose
three again and see if it does the same thing. Oh, it picked paper!
"Scissors beats paper, I win!" No, I win!
Fr. Robert: Okay, so I like this.
Shannon: And then you hit a key to end. So
now if I enter into this, I can go to edit. So he has... Basically he put in a
whole bunch of 'if else's and he also put in this thing called random. So right
here, the computer generates a random number for the computer right there. And
then, depending on the number, it'll give you back a different choice.
Fr. Robert: Let me put a disclaimer here,
because I know there are some advanced computer geeks out there who are saying,
"Wait a minute. Your random number generator isn't really random." We
know. We understand that. There is no real easy way to do a random number
generation in any code base, no matter what language you're using, but this
will give us enough to give us some variety between the choices that the
computer makes.
Shannon: Yeah, it absolutely does. And then
Scott did put in a lot of good comments into his code, so definitely check it
out over on the Google+ community - that's over at G+.to/twitcoding101. And the
last one I wanted to share with you guys today was from Daniel. So this isn't a
code, exactly, but he decided to share some of his research and his areas that
he found regular expressions online. So he has three different links on here,
and I'll open those up for you. One of them, I believe, is a poster. But these
are all different regular expressions that you can print out so you can get to
know them. Very very very good sheets. Very helpful.
Fr. Robert: Oh, my goodness. This is actually
exactly what we - believe it or not, we've been searching for a really good
reference sheet for a long, long time. Even the really good books about perl don't have the collection of these things in one
place. So this is incredibly valuable. Make sure to check this out. This will
be in our show notes, thank you Daniel.
Shannon: Yes, thank you so, so much. This is
very helpful.
Fr. Robert: This is why we love the group,
because we have the hive brain. The hive brain seeks out the things that we
need.
Shannon: We do, we're like bees.
Fr. Robert: I don't make honey. Okay.
Shannon: Moving on.
Fr. Robert: Now moving on, I know that we've
had some people that are growing tired of us doing regular expressions. This is
pretty much the third episode that we've been touching on it, the second which
we've been going full on. I understand that it's kind of confusing, I understand that for people who already know regular expressions, it can be
frustrating because you feel like we need to move on. But we've actually got a
graphic here that was given to us by one of our users, SJHCanada.
This is a classic XKCD reminding you why you need to know your regular
expressions. Now this is fun, they're talking about a killer who's followed a
roommate on vacation, and they're saying...
Shannon: Can I act this out? ... "Oh,
no, the killer must have followed her on vacation!"
Fr. Robert: "But to find them, we'd have
to search through two hundred megabytes of emails, looking for something
formatted to look like an address!"
Shannon: "Oh, it’s hopeless!"
Fr. Robert: "Everybody stand back! I know
regular expressions!"
Shannon: Now Padre is going to swing in,
onto the set.
Fr. Robert: Perl! Hooray! Wow, that was
actually really good. So folks if you ever start getting tired of learning
regular expressions, just remember one day, you will save your roommate.
Shannon: Excuse my horrible laugh.
Fr. Robert: Let's move on, okay. Now this is
going to be an abridged version of a regular expression episode, because we
have spent a lot of time on regex already and I didn't want to beleaguer the
point. This will be the last episode that we spend on regular expressions. But
Patrick has promised us that in the codewarrior section, he's going to introduce us to the most important thing you will ever
learn about any programming language ever. And yeah.
Shannon: Oh my gosh.
Oh boy. Sounds kind of scary.
Fr. Robert: He says that if we don't agree we
can have his bunnies.
Shannon: I'm a little intimidated... We can
have his bunnies?
Fr. Robert: We can have his bunnies.
Shannon: Okay. I don't agree.
Fr. Robert: Automatic. Alright. I thought before we get there, I thought maybe we should do a quick look at the
regular expressions so we can understand exactly what's going on, so that when
we get to Patrick we can better understand. Now, when we've been dealing with
regular expression, we understand that we put the name of the variable, right.
We start with the name of the variable, and we put the equals signs and the tilde sign and that tells it, "Okay, I'm going to be running
a regular expression." Everything after that is the confusing part.
Everything before that, we've seen it before on the code base.
Shannon: Everything before that basically
doesn't change, anything after that could be any
regular expression.
Fr. Robert: It can be crazy, which is why we
need to look through it and specifically look at the different pieces. Now, I
like to separate it into four different pieces. You start off with the match operator, you move to the end number, you separate these by slashes.
You move to the expression, you move to the second expression, and then you
move to the match expression operator. Okay, so think of it as four different
pieces. Now the question is, what goes into each one
of those pieces? For the expression, we already know, right? The first
expression is always going to be what you're trying to match, the pattern
you're looking for.
Shannon: So in our case, it has been the
standard input from the user.
Fr. Robert: Right, so you look at that and let’s
say you're looking for "rabbit". So "rabbit" is what you're
looking for, and you're looking through the variables, the string for
"rabbit". The second expression, if you've chosen to replace, will be
what it puts in its place. So that's really easy. The expression part is easy.
What the difficult part is, is the match operator that goes at the beginning
and the modifier that goes at the end. So remember that. Operator,
modifier. These are the things that you need to know. Now according to
what we've been doing most recently, there are three match operators that you
will see most often. In fact, there's really only two, but we're going to give
you three. The first one is M. M/ - do you remember what M does? M is just
match.
Shannon: Oh, match. Okay.
Fr. Robert: Right. So, just find it. Don't do
anything to it. Just find it. So, if I had, I don't know. Say, the script for
Blade Runner and I looked for tears and I put the M to match it, it will find
the place in the script where tears are used, and they can find the famous line
of, "Like tears, lost in the rain." Which I can't do, though it was
an awesome line, okay? Now S, S/ is slightly different, and folks, this is
going to be in the show notes so don't worry. Don't write this down. Let it
soak into your head. This is the one that we've been using most often. This is
substitute. So where as M is just finding it, S means find
this expression and then replace it with a second expression.
Shannon: So like, for us, we've been finding
the word cat, and then substituting it with the word rabbit. With
the /S.
Fr. Robert: By extending that, we only need
that second expression if we're substituting. If there's no substitution,
there's no second expression, right?
Shannon: So if you're just looking for a
match, you only need one expression.
Fr. Robert: One expression. And you would do
the match operator, the expression, and any modifiers.
Shannon: Ah, perfect.
Fr. Robert: Okay, so M and S, in reality unless
you're doing some pretty high end stuff, those are the only two match operators
you're going to use in Perl. You're going to find the match and substitute.
Shannon: There's three...
Fr. Robert: There's two, there's TR... Translator regular expressions. Patrick's smiling in here right now. He's like, "Yeah, we never use
that." I have seen - that's the face of a programmer going, "There's
some easier way to do this."
Shannon: They taught me that in college.
Fr. Robert: Exactly. Exactly. If you really want to use it, go ahead and take a look at the reference sheet
that we got from Daniel, practice it, code with it, and then you're going to
learn that you're never going to use TR.
Shannon: What does it do?
Fr. Robert: Patrick, I'm going to get to you
when we get to Codewarrior. I want you to give an
example of TR. Because I hate explaining it.
Patrick Delahanty:
I've pretty much only used it to convert uppercase and lowercase back and
forth. If I want to make something all upper case, I'll use TR to do it, or if
I'm changing uppercase to lowercase.
Fr. Robert: We call it, "Changing
Everything to Shouting Operator". Alright. Now,
so we've got the match, the useful match operators. Those are the two. I'm
going to call it two, not three. And then we've got the one or two expressions,
depending if you're just matching or if you're also going to be replacing, or
substituting. And then we've got what are called the Match Operators. These are
the fun ones, because there are a whole ton of them.
Shannon: So these are the ones that go at
the end?
Fr. Robert: They go at the end, right. I talked
about we divide it into four pieces? These are in that fourth section. First
one is G, we played with this. It means match global, look through the entire
thing from beginning to end, looking for this pattern. I is insensitive. It means we don't care if its capitalized, we don't care if its lowercase. You could put capital C-A-T or
lowercase C-A-T, or c, capital A, t. And it would
still find it, because its ignoring capitalization,
its ignoring case. And, it means to treat the streams as multiple lines verses
"as" which would treat the strings as a single line.
Shannon: So, does that mean if they entered
in several lines it would just treat it all as one?
Fr. Robert: It’s a different way of looking at
the variable, so, they're talking about strings. They can actually be huge, right?
They can be multiple lines, within a particular variable. A string is not like
what we learned in C sharp, where it’s just a short phrase. A string in perl, could be the entire file. So, I don't know. Look at it with its proper spacing
and its proper layout, or I'm going to look at it entirely as a single line. It
makes a difference.
Shannon: Yes.
Fr. Robert: There is also M... I'm sorry. There
is also O, which the value is the expression only once.
Shannon: Only once, okay...
Fr. Robert: So that's like, if for example, you
just wanted to find the expression one time and then stop the search.
Shannon: Oh, okay.
Fr. Robert: And my personal favorite is X,
which allows you to use extended regular expressions, which is another way of
saying, "allows you to use white space in the expression".
Shannon: Oh, interesting.
Fr. Robert: Right, and again, you're going to
need sometimes. All of these things are going to be contained within the
reference file that we got from Daniel, so again, Daniel, thank you very much.
We're going to include a link for that in center in our show notes. There are
many more modifiers, so many more modifiers. But these are the ones that we
want to play with. Please feel free to experiment with the other modifiers,
with the other operators, see what they do. You can't destroy anything, but, if
you want to cut down confusion stick with what we're giving you now.
Shannon: Got it.
Fr. Robert: Does that make sense?
Shannon: Yes, it does. I actually could have
used that X modifier, the one that actually includes whitespace in your
expressions. A couple of weeks back, I tried to write one where it searches for
Amy Pond and replaces it with Rose. And I was like, "Why is it not
working!?" And it was because I added a space in there. It would have been
helpful...
Fr. Robert: Exactly, it’s a regular expression.
Now this is the time where we introduce back into the show our Code Warrior.
Now you know him, you love him, you've seen him for
three episodes before this, plus all over the TWiT TV
network. Ladies and gentlemen, its mister Patrick Delahanty. Patrick, thank you for
coming back.
Patrick: My pleasure, great to be back.
Fr. Robert: When we were planning the show, you
told me you would mark down a certain number before, and you said, "This
will be the most important thing they ever learn." You wouldn't clarify
what that meant, but I trusted you. Now tell me, what's the
most important thing that they'll ever learn.
Patrick: I've been looking forward to this
episode, because this is the most useful line of Perl that you'll ever learn.
If you learn one thing in perl, learn this. It’s a
command line search and replace. Some people call it Perl Pie, and you'll see
why in a little bit.
Fr. Robert: Alright, so command line search and
replace, so what you're saying, is what we've been doing is practicing with
using the regex command inside of a program. So compiling it into some sort of
usable form that we can run interesting programs like Replacing cat with bunny or looking at your favorite podcasters - you're saying I could
actually use the regexp command at my command line,
outside of my compere.
Patrick: Absolutely. And so you can have a
search and replace that operates on one file, multiple files, just from one
line of perl. So I use this very often, and it’s the
most useful thing I've ever learned in perl and I'm
happy to pass it on. So let’s actually take a look. I'm going to continue here,
I've got some text files. I've got some bunnies too. But I've got some text
files. Before you do anything, make a backup. I've already got a backup
directory. If you save files...
Fr. Robert: Folks, you're doing a search and
replace, so if you replace something you needed, its gone, just so you know.
Patrick: Yeah, so I want to have a backup of
these files just in case, you know, fat fingers maybe I'll type it wrong or
I'll erase the whole thing. So before you even try this, make a backup! So,
I've got these files. This one, it’s got a list of TWiT hosts. I've got another one here that's just a list of mailing addresses. These
are not correct addresses, so please don't try to mail these places.
Shannon: That's totally my correct address.
Patrick: That's the police station in
Oakland, so...
Fr. Robert: Yeah, go there.
Patrick: I think the Star Wars script and
Shannon's bio for the TWiT wookie...
Yeah, and um, so I'm going to go into my command line here. And I don't have a perl program, I'm just going to type perl p - i dash e. This is where you get perl pie.
Shannon: So you're in the command line...
Patrick: Yes. And I'll explain that the p - i - e do after a second here. But they only do a single row
with my regular expression. So I'm going to search for Shannon Morse and I'm
going to replace that with Shannon's married name...
Shannon: The Dilatator.
Patrick: Shannon the Dilatator, that's your
married name, right? So I'll put...
Shannon: So, global?
Patrick: Yeah, so global, then we do the
single click to close that.
Fr. Robert: Make sure you go ahead and zoom in
a little bit.
Patrick: Yeah, it’s up in the top line,
people may have...
Fr. Robert: They may have issues.
Patrick: I could make that windowed. Oh,
there we go. Okay. And so then I'll type *.txt.
Fr. Robert: Now we should show folks that even
though we're using the command line, he's using the exact same format. So we've
got the operator, we've got an expression, a second expression that you're
going to use to replace it with, and we've got the modifier, which is G, which
we know is global so it's going to look through the entire thing.
Patrick: And at the end, I put *.txt so it’s
going to look through any file that ends in .txt.
Shannon: Is this any in your computer, or
any in that specific folder.
Patrick: Any in this folder.
Shannon: Okay.
Patrick: And I could do, I mean this is just
normal file operations. I could do this, and it would be everything, every
subfolder.
Fr. Robert: Don't do that. Yeah, don't do
that...
Patrick: Let's not go crazy here. I'll just
do every text file, and it will replace Shannon Morse with Shannon the
Dilatator. And now we'll go back to my files. If I reopen this -
Fr. Robert: You're going to have to zoom in on
this one, now, Josh.
Patrick: Make this bigger, here... So if we
look down, we'll see down here...
Fr. Robert: There you go!
Patrick: Shannon the Dilatator.
Shannon: That's funny.
Patrick: Now if we look in here, reload the
normal file... Shannon the Dilatator. Replace that
one, I don't think your name is in the Star Wars script, but in your bio I'll
open that back up... Shannon the Dilatator.
Fr. Robert: Now there are some people in the
chat room who don't understand why this is so powerful. Now let me give you an
example - Here we have a series of files that control the profiles of the tricaster. Every once in a while we have to make a Global
edit, like we change a line, we change the way we use the lower thirds, we
change the timing on something. If we needed to do it manually, what we would
have to do is have Alex go into the tricaster, load
each profile individually, change it, make sure its right, exit and go into the next one. I mean, consider
how many shows we have on the TWiT network.
Shannon: That would take days.
Fr. Robert: It would literally take days.
Before we used regex, it would take days, but with this kind of command what
you can do is, say look through the tricaster profile
folder, look for everything that contains the line that we're looking for. So the lay, or the name of the advertiser, or the name of the
segment that we're changing in every show, and change it to Y. It would
go thru and within a couple of seconds it would do everything. It would change
all of the settings in all of the profiles at the same time. Its amazingly powerful and I think that's why Patrick has a pretty good case for
saying that this is the most important thing that a computer programmer is ever
going to learn. Because it has applications beyond just
programming.
Shannon: It can be used in the workplace, that would be so so easy.
Fr. Robert: It’s a Google for your computer.
Shannon: So useful.
Fr. Robert: Your personal Google!
Shannon: Now, Patrick, I know this isn't the
necessary part of the tutorial for perl, but just in
case anybody is wondering, can you show them how to change directory over to
the correct directory of a text file?
Patrick: Change a directory to a correct
directory...?
Shannon: Yeah, a terminal?
Patrick: Oh, just change directory. That's
just... Let me go into my terminal here.
Shannon: So you do a, uh...
Patrick: Um, I had already typed the next
command, but...
Shannon: I am so sorry!
Patrick: I was unprepared for these!
Fr. Robert: We've got some zings out in the
chat room saying you could just use "said". Yeah, you could do that,
but what's so nice about this is if you learn it for perl,
which you're going to learn it for perl because it is
so important if you're programming in perl, you
cannot take that knowledge and use it without using any other utility, without
starting up any other tool, or without having to put it on any other piece of
software. Your perl experience to do the exact same
thing that you would have it do when its inside one of
your programs. That's actually very, very cool. When you
think about it, that's a nice way to do it. Patrick, do you have an
update for us?
Patrick: Yeah, I've got the... If you want
to see the list of files, it’s just under Unix or Mac
you can type windows, its DIR. If you're going to change the directory, I could
have the backup directory, CD change directory, backup. And if I want to go up
a directory back where I was, like that. So I can just move around.
Shannon: Yay! It’s pretty much the same in
windows, except for that DIR.
Patrick: I have the command that I argue,
that I mentioned earlier. I can explain what those do, if you'd like?
Fr. Robert: Yeah, actually, yeah. Could you
show us and actually give us some more specific use, because there are some
people saying, "Wait a minute, that's not all useful." Or maybe it's
only useful if you're a system administrator, give us some better examples.
Patrick: That was just my first example, but
first I'm going to explain what these arguments do. The first one is dash E,
and that tells it that its running this as a command
line and not executing a file. So dash E, is just this is a command line command.
Dash I is used to modify the input file in place, so it means they're just
updating the file I tell it to. And dash P runs the file as if it was in this perl code here, so it would be whatever I type would be in
here, and then if it doesn't work it would give an error. But, perl pie. P I E. I
can give another example here, let’s go to what I
typed up for... Thanks Shannon.
Shannon: You're welcome. Just hit upper
bench times.
Patrick: I didn't actually X from this so...
Dash E... I'm going to look for /. but I'm not looking
for the period with just any character, I'm actually looking for a period. I'm
looking for .html and replacing it with .php.
Fr. Robert: If you had just put .h without the
slash, it would have looked for everything with html after it. Whereas we're
telling it, no, actually look for a dot, a file with the extension by html.
Patrick: Replacing it with php, and I want global in case it’s sensitive. If it's
capital html, I want to replace it with php.
Fr. Robert: Right, so from what we know about
regular expressions, what this should do is it should go through the directory
and look for any file with a .html extension and
change it to a .php extension.
Patrick: Yes. And I have a couple of html
files in here that we can take a look at before I go and modify it. And see,
this one here, you can see there's a bunch of links down here, with .html.
There's one with html, so, it shouldn't change that one. There's also a couple
of links here. There's another link up here, but there's no .html so it
shouldn't change that. Then we'll look at this other file, because I want to
change both. There's one... And... This doesn't have
any .htmls so this shouldn't be modified at all.
Alright let's go execute that code, and see what happens. It’s done! It’s going
to go back. And reload this file... Oops I’ve got to close this and open it
back up. I'll zoom in again, and yup. These three links are now .php. These are also .php, but I
left on our content. So this is for if you were just updating some web page
content, or whatever. If you're changing over to php,
maybe in a future module for coding 101, then you could easily change all your
links that way. And another example - you'll see in these html files, I've got
some comments here. Maybe I want these comments for my own coding preferences,
but I don't need them or want to publish them.
Fr. Robert: Actually, Davinci Wonder has a very interesting question and I actually don't know the answer to
this one. Can you create a log of the changes? Because I don't think you can do
that, right? This is making global changes, that's it.
There is no record of what's happened.
Patrick: There's probably a way, but I can't
think of one off hand. You could always actually do a full perl script that logs all the files and goes through, but for a command line... Off
hand, I can't think of a way.
Fr. Robert: Again, I think the only way it
would be is if you were actually recording your screen or logging your
keystrokes. Sorry... Sorry DaVinci.
Patrick: But yeah, so these comments in
html. Let's try to remove those. But you'll see they're all different, so I
can't just search for this comment and remove it. So I've got to do some
creative expressions here.
Shannon: They all start with bracket,
exclamation marks...
Patrick: Yeah so they have that common bit,
because they all start with this less than, exclamation mark, dash, dash. And
they all end with dash, dash, greater than. So, what I'm going to do, is I'm going to clear this out, okay. Perl pie, and we
will search for - and I'm doing the backslash before this unusual character
just to be safe.
Shannon: That's to make sure that its actually searching for that character.
Patrick: Yeah, yeah.
Fr. Robert: The character, not for what the
character represents.
Patrick: And the hyphens are fine, I don't
have to put the backslash there. And now I'm getting a little fancy... So what
this does, I have bracket and then close bracket. And the
plus. And that means that it wants to find at least one of whatever's in
the brackets. And what's in the bracket? I've got this greater than, with the
backslash greater than, but I've also got this carrot. Which
means "not". So what this is looking for is anything that is
not closed bracket. Or, greater than. So, I'm looking
for this open comment and then anything that isn't this and then we will go
dash, dash, and at the end of the comment with the backslash greater than. And
I'm replacing them with nothing. And global, close that up. And what it'll do,
start html...
Shannon: I see what you did there, sort of.
Patrick: So I've pretty much said, the
equivalent of what this star is over here, with all of this saying,
"Anything that's not this and a comment."
Fr. Robert: Another way of saying this, is the pattern you're asking to look for is that open
carrot, and the closed carrot with those special characters, and then anything
in between those two you're going to replace with like, nothing.
Patrick: Yes. Absolutely. So if we look at the modified code here, all my comments are gone. It’s just a
blank line. It’s beautiful.
Shannon: It also erases the greater than and
less than signs. Everything.
Patrick: Yes. So it stripped the comments
from this.
Fr. Robert: Now folks, there's going to be a
lot of people saying, "Why would I want to remove the comments from my
code?" What we're trying to show you here is how you can dynamically
generate pages, how you can dynamically change code, say, within a webpage, in
order to make it match something that you want it to do. This is one of the
strengths of perl. We mentioned it in the very
beginning. And it’s something that we're going to be covering in the next
episode. But before we get to that, you have one more example, Patrick.
Patrick: Yes, I do. And we'll go back to the
command line here, and let’s say that I want to get a little fancy with my
search and replace, and I'm going to look for Shan. This? Can you guess what this does?
Shannon: S plus...
Patrick: S is space but it’s capitalized
with a non-space. So this will match anything, it will match Shannon,
Shenanigans...
Shannon:So it’s looking for the beginning of a
word and anything that's included in that word.
Patrick: And everything for the rest of the
word, until it gets to a space or a new line. Any space type
character. And I've also got it in parenthesis, and what this does...
I'll explain after I type out the rest of this. I'm going to get to the second
part... How do I spell that... There we go. I copied
it. Sailor... This dollar sign one is the first group in parenthesis. So, it’s
taking whatever matched in here, and putting it wherever the dollar sign one
is. If I put something else in parenthesis, that would be dollar sign two, and
then dollar sign three, and so on. So I'm taking this, and I'm going to find
Shannon, or Shenanigans, and I'm getting rid of Tilitator and replacing it with Sailor Shannon. And we'll do a G and we'll do that, and
we'll do that star dot txt... And let’s take a look and see what happened. Did this work. Alright see, here's the file before, you've
got... Let me make it larger. We've got Shannon The Dilatator, but if we open again... No I don't want to save it. Let's open it
back up, and zoom in and if we look down, Sailor Shannon!
Fr. Robert: Fantastic.
Shannon: Da, da, da, daaaa.
That's awesome.
Fr. Robert: Now, Patrick, I will say this. I
think this is cool, I think this is something that you should definitely learn.
But I'm seeing a lot of disagreement in the chat room, so I will say this: if
you disagree, that this isn't the most important thing that you will ever learn
in programming, if you think Patrick is full of it, if you think Patrick now
owes you a bunny, please go to our G+ group and tell him why. Or tell him, even better, what you think actually is the most
important thing to learn, while learning to program.
Patrick: If they disagree I'm going to
search and replace their comment with something else...
Fr. Robert: Actually that would be interesting,
search and replace Google+... Now folks, I know that this is a lot of
information so don't worry we're going to make sure that this all ends up in
the show notes. You're going to have our full notes, you're going to have all the code that Patrick used. And you're going to find
that really cool reference sheet that Daniel gave us so you can start playing
around with regular expressions without freaking out and thinking that you're
going to erase everything on your computer, replacing it with Shannon the
Dilatator. So Patrick, thank you so very much for being our Codewarrior yet again. Next week, I'm excited about this. Do you want to explain to these
folks what we'll be doing?
Patrick: We're actually moving onto the web
and we'll be doing, if I'm remembering the schedule right, we're going to be
doing printing to the web with perl, so dynamic web
pages.
Fr. Robert: Which is what you
do all day, so this should be interesting. Patrick, thank you again. Do
you want to tell the folks where they can find you?
Patrick: Sure, you can find me at Twitter at PDelahanty and also check out my websites down there
on the lower third... Animecons.com, entirely coded with perl.
Fr. Robert: Its perl.
It’s for the real world.
Shannon: The Real World. MTV, TWiT style!
Fr. Robert: That's enough of that... Shannon,
we did promise them that they're going to be able to find out all of this stuff
that we gave them, right? We told them to go to our show page... Where are they
going to find that?
Shannon: Yeah, that's over at
TWiT.TV/twitcoding101.
Fr. Robert: And if you want to get more than
just that, you're also going to find a link there to the github site, that we're still using. I know, I know, it’s horrible. We're starting to
realize.
Shannon: I like github!
Fr. Robert: Get hub is nice, but we need to... There's a couple of tools that we're going to use to make it
more useful. Because right now, you know this, it’s difficult for them to find
part of it on the show notes page, and part of it on the github page. We can't put it on the show notes page, because the formatting goes all
wonky and it’s useless to you. So we are going to find a better way for you to
get our show notes. Thanks to everyone who's been sending in suggestions. We've
been evaluating them. The reason we haven't moved is we don't want to change
it, and change it again when we realize it doesn't fit something else. So it’s
going to take some time, please bear with us.
Shannon: We'll take some looking around but
we'll choose the best one possible for this show. Also we are on iTunes too.
You can find us over on iTunes, just search for Coding 101 and you'll find us
in the search queries. I believe its available audio and video, so whatever you
need. It’s great for commutes, thats for sure.
Fr. Robert: Yeah, and it's not just audio and
video, its audio, its low quality video, standard video, and high definition
video, so you can find a version that you want, yes. Now also, did you know
that we do this show live. We're live right now. This is actually happening.
Shannon: Again?
Fr. Robert: I know, weird, right? Every Thursday, 1:30PM pacific time, you can find us at
live.twit.tv and as long as you're going to watch us live, why not jump into
the chat room, yeah?
Shannon: IRC.twit.tv, we check it out during
the show. We'll answer your questions.
Fr. Robert: Its right
down there. We see that little... He's mad, he's upset. Guy's threatening to
beat me up, that's strange... We love the chat room anyways. But of course, the
big place to get ahold of us is going to be our Google+ community. That's where
she pulls examples from the audience, and puts it into the program. So if you
want to end up on the show, go to g+.to/twitcoding 101 and go ahead and join. I think we're at almost eleven hundred...
Shannon: It's over eleven hundred...
Fr. Robert: Over eleven hundred? I want to get
it over nine thousand.
Shannon: Over nine thousand!
Fr. Robert: We're going to join and see what
the old and the young geeks over there can do for you. Finally, if you're not
into the G+ groups, yeah the twitters, right?
Shannon: We are on the twitters. I am @snubbs.
Fr. Robert: And I'm @padresj.
Shannon: Oh, and you know what? We should
say, Cranky Hippo is @crankyhippo,
who is just happening to get married today. But he's not here...
Fr. Robert: But actually, if you could, go to
Alex's twitter page. That's An Elf. He's got a picture of him and Bryan when
they were kids. It’s so cute.
Shannon: Awww,
congratulations Bryan.
Fr. Robert: If you could retweet that picture,
and tell them congratulations on Alex and Bryan getting married, that would be
fantastic.
Shannon: Oh, boy. And also, thanks to Josh
for filling in today. Thank you so much!
Fr. Robert: Yeah, Josh. You did good! Until next time, I'm father Robert Ballecer.
Shannon: I'm Shannon Morse.
Together:
End of line!
-
- -
Fr. Robert: What is he doing with his arms?
Shannon: I don't know.
Fr. Robert: That's so weird.
Shannon: He's an awkward child.
Fr. Robert: The thing... Go back to that
picture, you know the thing about Alex, is that he hasn't changed. He still
looks like that. Except for the hair. The hair is not
so afro... But you know Bryan looks like...
Shannon: Bye everybody! Awww...
Fr. Robert: Look at that! We've got Padre,
we've got Alex...
Shannon: What is that?
Patrick: That's today.
Shannon: That's today? Holy crap, that's a
lot of people...