Introduction to Perl Programming | Kyle Schaub | Skillshare

Introduction to Perl Programming

Kyle Schaub, Software Engineer

Introduction to Perl Programming

Kyle Schaub, Software Engineer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
11 Lessons (56m)
    • 1. Introduction

      1:24
    • 2. Perl Installation

      1:46
    • 3. Development Environment

      3:37
    • 4. Scalars

      4:44
    • 5. Arrays and Hashes

      7:23
    • 6. Conditionals

      6:07
    • 7. Loops

      10:01
    • 8. Subroutines

      6:12
    • 9. References

      3:00
    • 10. Reading and Writing to Files

      9:03
    • 11. Class Project and Wrapping Up

      2:14
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

81

Students

5

Projects

About This Class

Perl is a fast, powerful, and incredibly flexible programming language. It is widely used throughout the software development industry for many reasons; it is easy to learn, works on any operating system, and it's open source. New programmers and experienced developers alike use Perl for a wide range of tasks. From writing simple scripts to automate your work, to creating large-scale projects, this language can get the job done in any scenario.

No matter what your experience level is, anyone can learn Perl. This class assumes no prior technical knowledge - everything you need to get started will be covered in the lessons. The following topics will be discussed:

  • Scalars
  • Arrays
  • Hashes
  • Conditionals (if, else, else if)
  • Loops (while, for, foreach)
  • Subroutines (functions)
  • References
  • Reading and Writing to Files

With these fundamentals under your belt, you will be fully capable of writing your own code. After these lessons, there will be a short project that involves writing a script that'll give you a taste of what automation is like with Perl.

Meet Your Teacher

Teacher Profile Image

Kyle Schaub

Software Engineer

Teacher

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Your creative journey starts here.

  • Unlimited access to every class
  • Supportive online creative community
  • Learn offline with Skillshare’s app

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Introduction: welcome to the introduction to Pearl programming class. The goal of this class is to get you familiar with everything you need to get started writing your own PERL scripts. If you've never program before, this class will be perfect for you to get your first taste of what programming is like and learn all of the concepts that you used throughout all programming languages. The lessons are designed for those with no prior coding knowledge, so the content is easily accessible to everyone. If you have program before and you have an understanding of the basics, like what a variable is, how loops work, what functions are, then there may be some sections in this class that you can skip over. Here is the outline for the class. We start by getting Perlin sold, as well as getting our development environment set up. Then we'll start working on our pearl fundamentals. This is the part that some more experience students may choose to quickly glance through or skip, since it will be at the beginner level. Once we're comfortable programming with Pearl, we're going to write a script that can read and write to files on our computer, giving you a taste of what automating some tasks can be like. After these lessons, you'll be ready for the class project. This will be your opportunity to apply the different concepts discussed in this class to a re a world problem that you can work to solve. So when you ready, let's move on and get everything set up to write some pro code. 2. Perl Installation: in this lecture, we're going to get Pearl installed to our computer if you're on macro clinics that I have good news for you. PERL is already installed, so you'll be able to skip this lesson. However, if you're on Windows, there's just a few steps me to do before we can start programming first. You want to go to pearl dot org's and we'll go over to this download section, scroll down to the window section and look for the binary section. What we want is this active PERL You're gonna click download and I'm gonna choose this latest version the Windows installer. Just wait for that to finish downloading. Now you're gonna click on this to run it. I already have it installed, but the promise to be pretty self explanatory. You just going to be clicking next and maybe answering a few questions on here and then I'll start downloading. When I installed it took about 10 minutes to install, and then afterwards you didn't have to restart anything. You're able to use parole right away Now. The way to tell if Pearl is installed on your computer correctly is if you go toe explorer window and we can create a new text document and we create some filed dot pl. So if you make a file that ends and dot pl, it should recognize it as a pearl source file. This dot pl is what Pearl files and with so once that looks good, go ahead and move on to the next lesson where we'll talk about setting up our development environment. 3. Development Environment: in this lesson. We're going to get our development environment set up for us to program with Pearl, in my opinion, the best environment for programming with Pearl, his visual studio coat. It's a free, open source editor, and it's really easy to use. He was gonna go to code that visual studio dot com, and there's to be a big download button We're here, and it should download whichever version is appropriate for your operating system. Once it's done downloading, go ahead and run the installer or get it installed with your own Mac. And once you're finished going through all the problems and getting it installed, you can go ahead and open it up, and it should look something like this. What I like about visual studio code is that you can assign a working directory to it where it will show all the files in a particular folder, and that's what we're gonna do for these lessons. What I did was I created this pro folder. You can do it however you like. I have mine at sea projects pearl, but it really doesn't matter. As long as you have this folder somewhere and what we're gonna do his in visual studio code can go up to file open folder and they're going to navigate to where we want to have our working directory at. In this case, I have my C products, Pearl, and let's not colder and you can close this welcome screen and all that. So you see, over here we have our Pearl folder. Now, if we want to do some work in this folder, we need to actually put some files in it. We can click on this new file and we caught his name is whatever I'll name mine test dot pl If working with Pearl, make sure that you name your files dot pl with that extension at the end. So now our file is created and we can see that's open here. Also, if you take a look inside of your explorer window, you'll see that it also created the file there. So it's actually there on your computer now to verify that everything's working. We're gonna right a little time. Motive PERL code and make sure that it runs correctly. So I want you to type in Prince hello world and then make sure you see it every time you make a change to your file, you have to make sure you save. Otherwise the changes won't take effect. So how to run it? It's pretty easy. We just had to go to this terminal and then click on New Terminal. The things he down here we have our come in front and make sure that this is at the correct folder. So mine opened up at Sea products pearl, which is where we're working at the moment to run the file that we created, you used the pearl keyword and then you type the name of the file you want to run, which is test dot pl and click Enter and we can see that hello world gets printed out to our terminal. Based on this line that we put in the file as a side note, this can also be done elsewhere. Say we wanted to shift right click and shoes open power show window here that would work as well. Then we can just type in the same thing Pearl and in the name of the file, we want to run test appeal. And then there is our hello world message gets printed out now that our development environment is all set up, ready to start programming 4. Scalars: we're now ready to start programming. If you've never used a programming lingers before, there's no need to worry. We'll cover all the basics soon. Everything you need to get going with Pearl. If you already know your programming fundamentals, then you'll be in good shape. Pearle has all the same basic tools as other languages, like variables, arrays. If statements, loops and everything else feel free to skip the portions that you feel you already know and just use this website called Syntax Station all right here to get a grasp on how the languages you're familiar with translate to Pearl Syntax station dot com is a website I put together a while back for other courses I published, and it offered so much of sample code for really common things you want to do with several different languages. In our case, you want to check out this pearl section and definitely short of references throughout these lessons to remind yourself how some of these things work for these lessons, I'd like to separate our code up into different files for each video. So for this one, I'm gonna go over here, including new file and her first topic is going to be scale er's somebody called us killers dot pl and that it opened up now Scale er's are very similar to variables in other languages. Basically, a scaler holds a single piece of data. This data could be a number, a character or a full string of characters in our code. Here we can create a new scaler like this dollar sign and then the name we wanna name it. So I'm gonna name my score, equals, let's say, 90 with a semicolon at the end and be sure to save here. The name of our scaler is score, and it contains the integer 90. You notice that whenever we refer to a scaler, we put this dollar side in front of it. You'll also notice that lines like this will end in a cynical in. Be sure to keep these little details in mind as we move forward. As I mentioned, earlier scales can also hold text. So let's try subject equals and the name quotation marks Matt. Same deal. Here are scaler is name is subject and it holds the string math text contained between quotation marks like this are called strings. Now that we have some scale er's in our code. Let's do something with them. What we're gonna do is we're going to print out a message that uses the values we have stored here. So we'll use print and then we'll write the message. My score waas, and we'll grab the value for score. Then we'll continue on and say in my subject class. Now this kind of reads exactly how you'd expect. We're gonna put down a message that starts with the text my score was, but then we'll grab the value from our score scaler and slip it into this message and seeing the subject. So if we save and then come down here and run our program, you do that by typing and Pearl and then the name of our file Steelers dot pl. And then we could enter. We can see that our message says my score was 90 in my math class. One important thing to know about scaler XYZ that their values can change the red. The course of the program. Here I start to score off to be 90. But somewhere in between this and the message, we could have a change to something else so we'll try some, like score equals score plus two. So here the value of score that we have sort here is going to be set equal to the current value, which is 90 plus two. So we can expect the score will now contain the number 92. And it does 92 in my math class. By the way, if you want to rerun the last command that you ran in the terminal, if you just press the up arrow, it'll recall the last thing you printed. You'll find that pretty much any purple program out there who use scale er's as it is, the simplest form of managing data within your program. In the next lesson, we're gonna talk about some ways that will be able to store lots of data and organize it in meaningful ways. 5. Arrays and Hashes: in your program, you may end up having lots of different data in the example we have here. What if I was keeping every student score for this math class? If the class had 30 students, I would have to make 30 scale er's, and that would be a bit overwhelming to keep track of. This is where a raise come in a razor similar to scale er's. But instead of holding just one piece of data rays can hold multiple, we're gonna make a new file. But I'm actually gonna copy all this stuff over to this new file. Google has one array hash appeal and place it in now to demonstrate a raise. Let's just go ahead and create one gonna get rid of this line and will create an array called all scores. Now raise. Always start with the at symbol like that. Now we're gonna set this a rate equal to parentheses. Now, in this parentheses, we're going to put all of the integers that we want to have this array contain. So let's say you wanted all the scores to be No. 87 that a comma the next scored in the 95 of the Kama 72 and 81. You can see that all scores contains several different integers here, each one separated by a comma. Now that this rate contains data, we can now access these individual values down in the Prince statement. Let's change this to at all scores. Now we to tell which of the score going toe access. And we do that by putting in square brackets and then putting in the index of which one of these we want to access. Now the index refers to this is index zero. This is index 12 and three. So every one of these scores had some position in the array. So we said all scores zero that would be referring to this number right here because in a raise, its in next zero, meaning the first number starts at zero, then one and two and three. So let's try running this pearl. We're now in array hache dot pl and we'll click. Enter scores 87. So we're looking at this number. We change this to index to and saved. That would be 01 two. It would be talking about this number here, so it's just verify that 72. That is the summer. Now, Even though the race started off containing only four values, we can add more to it anywhere in a code. Let's say after this line we want all scores. It will specify index four to contain. No, no. 99 now already contains five values, the last one being 99. And if we look at index for save and then we'll run this, we get 99. In addition to a raise, hashes are very helpful tools for organizing our data. They're similar to a raise and that they hold multiple pieces of data, but they do so in a different way. A raise hold information in this linear pattern, one after another, and each item contains its own index or is located at some position. Hashes are different from this. They use key value pairs to set in retreat information. You understand what this means in just a second, let's get a hash programmed into her coat so hashes they always start with the percent symbol and we'll call ours my scores, and we'll set this equal to again. We use parentheses, but now we're going to use key value pairs, so we'll create a key called Math Comma, and then the value for math will be 100. Then we'll create a key called history. Its value could be 85 and science its value will be 92. So here my scores is the name of our hash. And in the parentheses here you'll see that we have three different key value pairs. For example, math is the key is corresponding. Value is 100. The values in the hash, they don't have an index or they don't have a position. The only way to access the data points are to use the key. And here's how you grab that information. We're actually gonna get rid of all this and we'll say print now, even though my score starts with a percent sign when we initialized it toe access a particular value. We use the dollar sign just like scale. Er's let's say my scores and then we use curly braces like this. Now, in here, we put the key that we want to use. So let's say we want to see what the value was for history. So what this print statement is doing is we're accessing my scores the hash and make sure you're using curly braces and we're looking at the key for history. So what we can expect is that will use this history. String will go into the scope of the my scores hash and find the corresponding value for history. And if we save and run this, it prints out 85 the corresponding value for the history key. Let's combine a few things to do something cool. I'm going to undo the last few things to get our old print statement back. Now this Linus saying a score as well as a subject. We can actually use this subject value right here as the key in our hash. So let's try that. Is there saying all scores were gonna access the hash? So remember for accessing hash when you to use the dollar sign, and our house name is my my scores that we use curly braces. Now, when the curly braces goes are key and, like this ever gonna try using subject as our key. So we should expect that since subject contains the string Matt, we'll be using the math key, and here therefore when we run, we should expect to see 100. We got 100 the math class. Now, if we change this math Teoh Science, for example, and then we saved now I was going to say in my science class. But it's also going to use science for this hash, so we should expect to see 92 in the science class. Now that we have some efficient ways of managing our data, let's move on to implementing some logic into our code so I can start to be a bit more useful. 6. Conditionals: so we know how to manage the data within our code. But what can we do with it? One of the most fundamental concepts of programming is utilizing conditional statements to get started. Let's go ahead and create a new file in this one conditional that pl now conditional czar simply sections of your code that Onley run based on certain conditions. Let's read a basic example. To demonstrate how this works. We'll create a scaler called Number said Equal to five. Now we'll do an if statement, which is our first kind of conditional. We'll say if number is greater than zero. So this is asking the question is the number clear? Zero? And if it is, then we'll perform. This line will say number equals number times two and then at the very end, we'll print number and then make sure you save. So this is how this program will play out. We created our number scaler, and it's such a five. Then we get to this. If statement, the if is gonna ask the question is, are scaler called number greater than zero? And if it is in this case it is five is greater than zero then we'll perform. What's between these things? Curly braces. So in this case, we will multiply our number by two. So if we run this, we should expect that our number is set to 10. If this fire was changed to negative five, for example, and we saved, this is no longer true, We would ask is number great and zero. And since it's not, then this will not run. So our number is still left at negative five. It never gets doubled. Now you can have as many of these statements as you want. You can just write a 2nd 1 if you want to, We can ask is it's number is less than or equal to zero, and if it is, we'll do something like number equals negative hundreds. So in this case, this is statement is not true, but this one is so it is going to run this line and we can see that our number will be set to negative 100. Now you could write your code to have just a ton of if statements, but there's actually an easier way to right this exact same thing, but more concisely, we can get rid of this completely, and there's a keyword called Hell's. What else means is that if this section is not true, then we'll run this section instead. So we'll try. Setting number equals number times negative one. So, like I said, the program will start and first it'll ask, Is this true? And if it is, it'll run this line. But if this isn't true, then it will skip this and go to the else section instead. In a little run this, In our case, it's going to go to the else section, and our negative fives would be multiplied by negative one to become positive. Five. Now there's one more piece to this that we can use it. It's called the else. If this section goes between the if and the else and looks like this used the key word else . If spelled like that now, the else if it's similar to the else and that it will only run if the previous question was false. But the thing but else if is that it also has another condition. So here we can ask if number is equal to negative 20. When you have to equal signs like this that means you're asking the question. Is this equal to this? It's different from this land up here. We have one equal sign. That means you are setting this value to this value and for this self else. If let's just do something like number equals number divided by 10. So this is a logical order of this program is going to go to the if and ask this question. Is number greater than zero? If it's not, it will go to this and ask the question. Is number equal to negative 20? And if it's not, it'll go the else and run this line. So with negative five, this is not true. This is not true. So it'll run this section if we change this to negative 20. This is not true, but this one is. So it'll run this line, meaning that if we save, always remember to save and then we run this again. We can expect to see negative two or negative 20 divided by 10. So it ran this line. Something to keep in mind is that you can have as many else ifs in a series of operations like this as you want, but you can only have one if you can only have one else if you want to have more than one. If you'd have to have a completely different section like this, do something else down here with conditional statements under our belt rail to write programs that do interesting things and perform calculations based on what data we're given . 7. Loops: loops are a tool that you'll find throughout pretty much every programming language. They live up to the name by performing a certain section of code and then looping back to a starting point based on certain conditions. These are kind of like if statements, but you designed them to run multiple times. Let's start by creating a new file. We'll call this loop, stop pl and let's start this off by trying out the first kind of loop that we'll be talking about the wild loop first, let's create and scaler call it my NUM equals zero. Then we'll make our loop. You use the while, keyword. Now, in here, we have some kind of condition that this wild people check for in our case will check to see is mine. Um, less than 10. Then use curly braces again. Now, in here you have some operation that will run on every iteration of this loop. We'll try something like my numb equals mine. Um, plus one. And then, after everything else, we will test print my No, By the way, I'd like to show you a little shortcut. This kind of line is very common where you just increase some scaler by one. There's actually a shorthand version of this. Instead of writing this whole line, you can get rid of all this and just write mine. Um, Plus Plus, this is the exact same thing is before mine, um will increase by one. Similarly, there is a minus minus. But here we want plus plus. So this is how you read this code? We have our scaler set to zero. Then we have our wild loop. We're gonna ask the question, is my numbness intent? And if it is, we will run this code. But since in a while loop it's going to run this, then go back to the condition again and ask again is mine. I'm less than 10. And if it is, it'll run this again. So this section gonna keep looping and looping, running and running and running until at some point, this condition is no longer true. In this case, my numb starts at zero. But every iteration of loop my name is going to increase by one. So every time this runs, my name is getting a little bit bigger each time. And eventually my name is going to reach 10 and the while it was going to ask is 10 less than 10 and that is false. 10 is not less than 10. So this is over and this line no longer runs and the program continues on. So if we run this, we can expect that our output should be 10. My name started at zero. This looped and ran this line 10 times, and then we print it out. One thing to keep in mind about Loops is that even though this intersection of code ran 10 times, it all happens pretty much instantaneously. We could bump this up to say one million and morning it again. It'll still pretty much be instantaneous. Even though this line ran one million times, we got her answer immediately. Computers are incredibly quick, and it can get through a 1,000,000 cycles in no time at all. A very important thing you need to remember while writing loops is to make sure that you don't accidentally create an infinite loop. An infinite loop is one where the wild condition never becomes false, and therefore the loop will run forever. If this happens, your program will stop al demonstrate this now instead of increasing mine, Um, by one each time. Let's decrease it. Since my NUM is going from zero and just going down into the negatives, this will always be true. It will never surpass one million. Therefore, the Wild will keep looping infinitely if we save and run. This is what that looks like is running right now, but we never get her answer. It's still just running in the background, and nothing's really happening. To cancel this. You can do a control, see, and it stops. So before I forget, let's change this back two plus one. Okay, now that we understand how while loops work, we're gonna look at a different kind of loop. This one's called a four loop. It's a little bit more complicated than while, but with a little bit of explanation on what's going on, you shouldn't have any trouble. Let's write one up first. This is how it looks. You have four, and first we'll start with I equals zero with semi colon. Well, do I is less than five semi colon, and lastly I plus plus then same as the wild. We're gonna have curly braces and inside will do the same thing as before. It will increase mine, Um plus plus. So there's a few parts of this loop that aren't obvious What's going on? So let's go through everything that's going on here. First off, let's look at this scaler. I equals zero in this loop eyes going to act as our generator. Basically the scaler. I is going to keep track of how many times this loop has repeated itself this middle part eyes Listen, five tells the loop when it should end. In this case, the loop will keep looping while I is less than five. So as long as this is true, the loop will keep cycling. And finally, this last part I plus plus is what happens to her. It aerator after every loop every time this middle section runs, this is going to run as well, right? Afterwards. So let's just talk to you exactly how this will run the four little start. I will start at zero. And since this is true zeros, listen five mine, um is going to increase by one. And also I was going to increase by one. So now I is equal to one now is going to go back to this and ask again is one lesson five. And since it is, this is going to run and this is gonna run now eyes too. Two is Listen, five is gonna run again. It's gonna keep going and going until I will reach five. There's gonna ask, is five lesson five and since it's not, the loop will be over and this will not run anymore, and the program will move on. So running this program now we get a result of 1,000,005 and the one million comes from this loop. This increased my numb to one million and this loop ran five times, meaning it increased my numb. By five. You can actually accomplish the same things with both loops. We'll find situations where one will be easier to implement than the other. Now there's one last sleep I'd like to show you, and it's a really useful one. It's called the for each loop, but this one actually requires some kind of list. So let's quickly create an array of integers. We'll call this numbers and we'll put 246 and eight in here Now the loop we want to create well, look like this well right for each has one word. Then we'll make no, um And then we'll define which list we want. Talk about Norwich Array, which is at numbers we put in some curly braces. Now, what I'd like to do is actually add up each of these values to get a some. So, actually, I think the easiest way to do that is to create a scaler here. We'll call it some. We'll start off at zero now in this for each loop. What the four each does is going to go through every item in numbers or each one of these. And it's going to store the current value that we're looking at in. No. So what we can do is we can just increase some equals sum, plus no. So, again, numb is going to contain each of these values throughout the sleep. The first time's gonna contain too, and it led to to some then none will contain four. And it left for to some, then six, then eight. So in the end, this some scaler will contain every value from this numbers array. So let's try out real quick if we save and then run Actually, once it print out some, we get 20. So that means that our four each loop went through every one of these numbers and added it up together and put it in this some scaler. 8. Subroutines: Subroutines and Pearl are basically what functions are in other languages. In fact, many Pearl programmers just refer to subroutines as functions. But regardless of what you call them, subroutines are incredibly important, and they'll make your coat really easy to organize and reuse. To start, let's create a new file. It's got served at you. Subroutines are sections of code that only get run when you specifically call them. And what's nice about this is that you can call them multiple times at different points in the program, and you can even give these sections different parameters so they run using different data . Let's could one up so you can see how they work. We'll start with sub and in the name of our subroutine will call it hello when we put in some curly braces and then in here, we'll just do print Hello and unsafe. Now, when we run this with Pearl subbed a pl, you'll see that nothing happens. This is because, like I mentioned earlier before the code and a subroutine run, we need to actually call it when you do that by typing the name of the summer teen, followed by two pregnancies haven't see. Let's try this more time and the real we have a message. Hello? Being printed out. Now you might be wondering, what's the point? Why didn't we just write a print statement and be done rather than create a subroutine and call it Well, now that we have the subroutine, we can call it a bunch of times anywhere in our code. We could do something like this. Well, hello. Three times it's now we run this we get Hello. Hello. Hello. This be using first running Whatever isn't here every time we call the Subroutine. By the way, if you don't like your text all being on one line like this, you can add the line break character that's backslash in to the end. And if we say this, we'll put it on different lines. So the significance of subroutines is that we could write some complicated code and here and then just call the subroutine elsewhere. That way, you won't have to rewrite the same code in two places. The general practice and programming is that if you have a section of code that might need to run in more than one place, you should write a subroutine or function for it. Now subroutines don't have to be some defined action like printing. Hello, we can make it. So we passed some data into the subroutine or calling. And then that summer team could use the data to do something interesting. For example, we could make a simple subroutine that multiplies to values together. We'll start with sub. We'll call this multiply values known here. We need to accept parameters and this is how you do that. That one Val to is it the equal to an array that we're passing it? Then once we have our values that were passing in, we can just return now one times about it. Now it's print multiply values. So this is how you pass in parameters to function or subroutine when we're calling the subroutine. With these parentheses here, we can put values in between here and between its parentheses to say we want value one to be four and we want value to be setting. So what will happen is we call it subroutine. We pass in the array for in seven for all, correspond to about one, and in seven will correspond to Val to then here it's multiplying those two values together , and then we're returning it. Now let's see what happens when we run. We get 20. Actually, I'm going to remember these for now. A long time. Wait 28 4 times seven. So there's this keyword return here that's important to understand. With subroutines, you have the option to return a value. In this case, we're returning the product of al one in Val to when values returned from a subroutine. It's as if that value is replacing all of this. The call itself. So since we're returning four times seven or 28 it's as if this print statement just got 28 we're just printing out 28. A common thing you'll see is ascending the output to a variable or a scaler so we can call . This result equals multiply values. And then so now, with my values when called, it's as if we're just replacing this call right here with 12. And then when we go down here and we print the value of result and run it, we get 12. Once you get comfortable with using subroutines, you'll see that they are incredibly useful tools. There's a reason every programming language has something similar. Having the ability to reuse your code easily and with different data could make big tests turn out to be really easy. 9. References: in some languages, including Pearl, you have the ability to handle your data using references. These there were a lot like pointers in C plus. Plus, let's go ahead and create a file references, appeal references, air kind of like variables or scale er's. But instead of containing data, they contain the location of data. When you have the location of data you can access and modify it from anywhere in your program without knowing the scaler, this data is stored it. I know this sounds a little bit strange, but it'll make sense. Once we get into it. We'll make a sailor called name and we'll say, Kyle, next, we're gonna create our reference. No reference is pretty much just scaler. We'll call it ref name. But instead of sending it of value, you need to dio backslash and then give it a scale. A 2.2. So our reference ref name contains a reference to our scaler called name. Basically, it's storing the location of this, or rather the value contained with it. Now, to grab the value at that location, you need to d reference. So the same do that you do print and then said just doing $1 sign. You do, too. And then type in the reverence. So we need to use $2 signs. You're going to d reference the reference, which just means you're going to grab the value that it's pointing to. So where we have a reference to name and by de referencing, we're gonna grab the value that's at that address. Just double check this. That's one. It Pearle references up here and a Prince of Kyle. Now what's the purpose of this? Well, if name ever changes, then that means that ref name Congrats, that change as well. Rough name is just the address of where the data is. It doesn't know what its value is until we d reference. So we could do something like this. We could say his name to the Alex So the names changed. Alex, keep in mind that we didn't change anything about ref name. It's still pointing to names address. Therefore, when we run it says Alex, this is what makes reference is useful in certain situations. We might not want to store data, but rather just look at whatever data is stored somewhere else. And this practice of storing addresses can be used on lots of things here. We're storing the entrance of a scaler, but it could also be an array or a hash or could even be a 17. 10. Reading and Writing to Files: we're gonna get into some programming techniques that will allow you to write some code that'll do something productive. Specifically, we're gonna look at the process of reading and writing to files. This is a common use keys for Pearl. We will have a file with some data and you want to write a program that reads the data, does something with it and then return some useful information. So a few things we're going to create a new file. I'll call this read right that pl have that opened up. And next, let's create a file for us to read. So in the same folder, we can create a new file and we'll call this scores that txt. So it's just a plain text file in this text file. We can put any kind of data we want for this example. I think I'm just gonna put a bunch of scores each on different lines, something like, uh, 87. Okay, so we have six different scores each on different lines within the scores, not text file. And by the way, just to clarify this is still creating all these files within our project folder and explore. You can see that scores of Texas here And it contains all that data that I just put in now in our program and read write dot pl If you want to read those numbers we just created, we will need to open the final first to create what's called a file handle. The file handle is like a scaler that will let us see the data contained within the file. So first we went to call the open 17 open and then we're going to create our file handle. We'll call it dollars on F H just like a scaler and the money to give it the file that we want to open. In this case, we're gonna open scores that txt just like that. So to clarify our and putting it very simply, we're gonna be taking the contents of scores a text and putting it sort of into FH. You'll see what I mean. It just a second. Now that the file is opened and we have our file handle, we actually want to go through all this data and do something with it to start. Let's try printing out all of this data to the console to verify that it's reading correctly. To accomplish this, we need to use a while loop Well, say while and then we'll take a look at road equals. And we have this new format me to do just like this. Let me just want to print the value of rope. Now, I know this wording is a bit strange, but let me explain. Earlier in the loops lecture, I talked about how wild loops have a condition inside these parentheses here. As long as that condition is true, the loop will continue on. And that principle still holds true here. But we're taking it a step further. What we're doing is we created this scaler called Roe and we're signing it to the current row from the file handle to clarify when this loop starts, Row will contain the data from the very first line in our text file. Then the contents of the loop will run and we'll print out that value of roe. So at that point, the very first line of the text file will be printed out. The sleep will continue running. But when he goes back to this condition, it'll this time Look at the next row So the second row of our file. Then, of course, gonna print out that one back here in front of the 3rd 1/4 1 and keep going on and on until eventually we don't have any more Rosa print. And at that point, this is no longer true because there's no more data to read in the Wild Lupul End and no more Prince will happen to verify this works. Let's go ahead and run this pearl were you to write dot pl and then we have it. We have all the scores from our text file. They should line up to be the exact same. So we know how to read data from a file. Now let's actually do something useful with this data. Let's write a program that will calculate the average of all these scores as a reminder. The way to calculate the average is to add up all the values and then divide that some by the total number of scores. Let's start with that first step, adding up all the numbers to do this will need a scaler to store are some some equals zero now, every time we read of value from the text file. We want to add it to some. So here, instead of printing row, we can just add it to some. Some equals some plus wrote, and that's all it takes. We now have the sum of all the numbers. Verify this works. Let's go ahead and print it and we get 5 25 Now we need to divide this number by the total number of scores that we added together. Looking at the text document, it looks like there are six scores. But how do you figure this out in our code? Well, we could just keep track of how many times is wildly runs. We can have a new variable or a new skill, or we'll call it count, start off zero. And then every time this runs, that means we're reading a new row or a new piece of data so we can just do count plus plus . So now account contains the total number of scores. Therefore, always do. It's just do some divided by count, and that is the average number of all our scores. So this is great. Our code doesn't seem pretty useful. What would make this even more useful is if we created a new output file and wrote this calculation there a few things. First, let's store our calculation into a scaler. Instead of just printing it out, we'll say results equals this. Then after that, we should close our file handle and you do that. It's stepping close and then our file handle name. Whenever you do an open of a file, you should always close the file handle after you're done. So now that that's out of the way, let's create our output file first, me to open our new file, and we can use our fate again. And this time there's a new perimeter we need to put in here if you want to write to a file it to include this symbol in there to clarify that were writing and then we want to put in the name of the file that we're writing to in this case will create a file on Call it output that text Now to write to this open file. It's really easy. We just do print. Then instead of before religions print out some string or some number, we can pass it to things. First we can pass it the file handle. And as a reminder, this file handle is pointing to output text a brand new file, then whatever we want to write to it, which is results. And then, as I mentioned, we should close this file handle after we're done. So to recap, we're creating a new file handle for this new output. That text we are writing a result to that file handle. And then we're closing the file handle. And if all is working correctly when we run this keeping an eye on these files to left here , we should expect a new one to pop up. And there it is. We have our output, that text and inside appetite text. We had a 7.5, which we decided was the average of all of our scores. And there you have it. We have created a useful program with Pearl. You could give this pro script to someone else, and they'd be able to run it by using their own text file of numbers to get the average. This is a nice, simple example of how you could use PERL to create some automation for certain tasks. 11. Class Project and Wrapping Up: we're nearing the end of this class, so we're ready to talk about the class project with everything that we talked about so far . You have everything you need to tackle this problem, and it's fairly simple. All you'll need to do is alter the code from the reading and writing to files lesson to fit this scenario. Here's the problem. You need to write a program that can read from a text file, and the text file you'll be reading will contain a big list of scores. One score on each line. Well, your script needs to do is find the highest score in list. To accomplish this, you'll need to look throughout the scores and determine which one has the highest value. When he find that out, put the answer to a new text file. If you use the code from the lesson on reading and writing, you should only have to write A few lines of code to accomplish is think of what other fundamentals you'll need to use in order to complete this task. When you come up with a solution, be sure to take a screenshot of your code here and posted to the class project page. I'll take a look at it and give you some feedback. If you spend some time thinking about this problem and you can't quite figure it out, be sure to contact me and I'll give you some hints. Until then. Good luck. Once you finish the project that will mark the end of this class. Thank you very much for going for this material with May. If you have not already, please leave a review, and if you enjoy the class, please recommend it. I greatly appreciate the support from here. I suggest doing some research on some of the cool features that you can do with Pearl. There are a lot of networking capabilities and different ways to automate tasks, and these ideas could be accomplished by utilizing the fundamentals. Discuss in this class. Also, feel free to contact me with any PERL or random programming questions. I'm always willing to help. However I can. I always like hearing about what kind of projects students are working on, or if you need some ideas or guidance moving forward, I'd be glad to provide some input. And with that, I believe that's everything. Thanks again for taking my class. I look forward to seeing you in the next one