 Hi everyone again welcome to the Linux programming and scripting class we will be talking about pearl in today's lecture let's look at what we covered in the last lecture and then we can start from there so let's quickly go through that so in the last lecture we had a lot of things that we covered lot of the things that new to most of you one thing we went into like more details about the pattern matching I think like I mean this is one of the key things that you want to understand in pearl then we also took it took a look into the pearl debugger the various debugging commands that even through and I think like I mean that's another thing like we want to run into trouble with any pearl programs you can invoke the pearl debugger and then use the pearl debugger to debug your program that will be like very useful and then we also looked at the functions some of the functions in more in-depth manner one was grep the grep is a function that is used to get particular word from given array of words or given dictionary and actually like I mean grep is used widely to compare two arrays and get the difference of the array or also compare two arrays and give unique items are the inverse of the differences essentially meaning or the commonality between the two arrays that's one thing and then also like the difference of the array as as output and then these things can be used by simply the function we looked at it basically how we can combine the hash table concept or hash array concept and then use grep to actually grab from that in the indices essentially so that's one thing and then the sort actually here we also went into more detail here actually like I mean the last class we use a new operator all the the steamboat operator essentially make this operator is used as a monitor to do the sorting the regular sort is an alpha sort essentially so there is lot of issues basically so when you have to have a numeric sort and also again and how do you do the numeric sort and sending order and depending on how do we do all those things we can achieve using the steamboat operator one thing to note here is in Perl the sort function by using a quick sort algorithm so essentially as you know the quick sort algorithm basically like just starts with any one variable any one element in the array and then it tries to sort the elements that are greater than that element after that element and then the elements that are less than that element before that element and then once it basically like once it anchors that particular element then it passes these two lists and then it continues the same way because it goes through so typically we see that it has a complexity of n log n I think like I mean you will probably like get into all these details once as a real computer science at least to know here you want to make about this complexity is in this log n area n log n is kind of it is very good considering the sort if it is inspired that means that you are actually going through the object in two times basically so I think like I mean this is a much controlled behavior so that is what we are using the sort so sort is fairly efficient so that's what I want to say about that then we also looked at the file open file close testing the files with various switches whether we can read it read a file write a file things like that and then also like in the same case for the directories to basically these testing tests can be done for the directories and then how do we open the file and these file handles things like what we talked about that and then we also talked about the strict keyword essentially so this is to make sure that Perl does not omit any kind of mornings or any other errors that isn't on this because Perl itself is a tolerant program so even if you made a mistake but if it is syntactically and semantically correct it just lets those things go through so I think strict is one keyword that we will be using basically it is the use strict we will see like more and more programs actually so initially like I mean if you are doing like simple programs you may not want to do this but once if you are doing any Perl programming development I strongly urge to use strict in your programs so I think this is pretty much like in what we saw last lecture I think like I mean that we went to like a lot of the modern of these areas but today I want to actually introduce two topics today one is there are some leftover items that I want to talk about one of them is essentially like how do you run a program within a Perl code so there are some need techniques for that and I will explain that and then the big topic for today is some of the object oriented features of Perl which is actually prominent in the Perl 5 which is the fifth generation Perl and this is essentially what is called the packages essentially we will talk about probably how do we use packages basically I mean one thing that made Perl a versatile language that exists today is the ability to actually export and import modules from different sources so we will see like I mean motivation behind that and then also like how do we do it in when we talk about the packages so let me talk about the first ones essentially as I mentioned how do we run programs inside Perl so say like I mean you want to do externally like I mean a CD to a directory or something like that inside of Perl how do you do that so we will see there are actually three ways of executing commands within Perl the first one is what we call as exe c or exec the exact commands essentially it executes a command that you provide but it never returns so this is the keyword the never returns it's like a written from a statement in a function if the command is not found the exec returns are false it never returns true because if the command is found it never returns at all there is also no point in executing returning to returning standard out standard error or exit status of command so some examples essentially like here cc or print standard error couldn't execute full with the dollar bang I think like I mean this one you already know about this basically this is to print the error message number so it gives the constant error message number for this particular error the other way to run exe c is just to enclose the whole statement within the curly braces and then essentially you can also if this returns zero then it goes into the next statement which is it'll say that it's not able to execute so exact pretty much like finishes a program so basically like I mean once you run the except it basically it just comes out from that program so this is one method there are places where this gets used so we will we will talk about it talk about some other things but it's it's one of the useful commands the second one is called system in system essentially like I mean once you specify the command it executes the command and then once the command finishes basically the perspective is continued and then it goes to and the return value for system command is the exit status of the command itself so if you are doing a CD and the CD become successful and if it doesn't one the return value from system is also one but if that CD was not possible and it returns to zero then the system command also returns to zero again it's mildly useful but it is not really useful because as we know then we try to execute commands yeah we can execute command like CD for example is is to go the take the program pointer to a different directory but if something like I mean if you are using an external sort or a final go and to find certain things how do you actually get those values and then process them further that's simply not possible with the system command so here the example for using the system command is command with arguments and within the reports inside the passes and then a better way is essentially like in closing command arg1 arg2 arg3 everything as its own strings so that it's actually better for the system see it and then one would run the system command now the third option that you have is what is called a back take and this is I think like I mean this is very prevalent one and then a lot of people use this and essentially this is something that that's you will be using in your program as well the difference between the back take and the system is it is like system there it executes a command and the Perl script is continued after the command is finished just like the system command but there the difference comes in is essentially the next line here where contrary to the system the the return value is standard out of the command so if you do the back take it's not the status but whatever goes into the standard out becomes the return value so here we can say like I mean in the scale of context dollar result is command followed by arg1 arg2 and then you notice the back take here back take here so this commander gets executed and essentially like I mean whatever the commands return value stored in as a scalar into the dollar result and then if you want if you if the command actually returns a list then all you got to do is basically like I mean you can store it as an array with result as the name and then executed command back take mode and then whatever comes up is stored as an array basically whatever the result of that command so this is very useful when you are doing a sort external start for some for some reason and then actually using it and storing it into the results so so again to recap on this one exe c essentially like I mean basically it executes the command but it never returns so it's useful but it is not that useful the system essentially like I mean this is the next best thing their system actually returns the the exit status of the program of the command rather than actually any any other thing basically whether it's an output of the command or doesn't produce that it only produces the exit status so here if you want to use this you may have to store the values into a temporary file and then basically like again read it back into the into Perl and then you can use it so like I mean system command is typically used to generate like I mean the argument data which will be used at some point of time later in the program so that is the system and then finally the back take commands the back take commands essentially it's just like the system where it executes the command and the Perl script continues after the execution is finished but the difference is that actually now you can pass on whatever the output of the particular command back to the program that initiates this call so it can be passed to those those things and then you can do for the processing or not so the back take is like a very useful I will say like I mean more often than not we will see the back take commands than the system commands so now that we covered this let's see the next one basically is how to write and use Perl modules again remember that this is basically Perl 5 which is some people call as object oriented Perl but I think like I mean the key thing here is your developing program how do we get it into another Perl program and how we can foster like a collaborative development of various Perl systems in fact today lot of companies whether it's hardware or software are using Perl as a wake up I mean use or doing Perl programming and the way that they do it is collectively they are doing it and then more in order to make the system work we need these modules the concept of modules and how do we actually write and use them so just a before that I wanted to introduce you one small function which is called the qw function it's called code word it is used to generate lists of words the qw actually extract words out of the spring using embedded white space as the delimiter so this is important you can actually like change this delimiter to any other delimiter that you can read up on that of this command command and then it returns words as a list essentially so and then it also one thing that note to note in one thing to note is that it doesn't it happens at compile time so which means that the call to the wq is replaced with the list itself before the code starts executing so here one simple example is code word temp file tender it returns temp file and tender as two different values so now so this is important in the next section when we talk about the modules because like I mean most of the modules we write it as functions and basically use for code word function a lot so let's look at first of all what is the module so the module is essentially a separate namespace in a separate file with related functions and variables so there are three things here one is a separate namespace another one is a great file and then there are related functions and variable so how do we do that we will see basically so what is the namespace the namespace provides a certain context to the function and variable so essentially you can think of namespace as any kind of names itself so my hammer Mary's lamb gulliverse travels Dutch cheese French bread your car so you can think of this as like this these pronouns here between your own various possessives here basically these are all the namespace that now that's basically like I mean that provides the context to the these functions so if so in short essentially like I mean you can say like two people writing soft function you can say like is the heart or these sort so we will be will see more about the that context essentially so here one example I have like two engineers jack and harm they both wrote a function to shuffle a sequence and you want to compare the two functions they both call the function as shuffle now how do you distinguish between them so the way to distinguish between them means to put the function in its own namespace so now we call the first one as jacks or harms so there is a sub shuffle and this is harm's implementation and then there is a sub shuffle which is jacks implementation so if you if you put basically say like I mean this is the sequence that we need to do and then if you say like I mean shuffle seq this will not work because it doesn't know like I mean okay which function to use again same thing it does not work so we need to distinguish between this and this how do we do that so again it is all the same basically like I mean the top one and then what the sequence that we want to shuffle is also the same only thing is like now we name the functions as shuffle harm and shuffle jack so you notice here that actually names are different we add this additional element to it yeah this is well and good but what happens basically when we you know we have to somebody has to tell the others essentially like if say like jack is the one that is that develop the shuffle second time now jack needs to at least harm needs to tell jack that hey jack I am using the name shuffle harm so you should use shuffle jack or everyone should have these kind of names arbitrary this is kind of arbitrary you can see basically so to avoid this essentially we use the package essentially so what we say is basically we call we will have the packages arm and then inside that we will still use the same name which is shuffle and then that is followed by the harm implementation and then we will have jacks block with the package called jack and then it also has the same thing the as a function which is shuffle and then this is jacks implementation so now we define these two things but how do we use them basically so the way to use it is like again like on the sequence is the same here we use harm as the package followed by this column column and then the function name here again the same thing for the jack it is basically jack column column and then the function these two will work basically and then you will get like two different answers based on how each one implemented these two functions so so this is the package that we can see basically like I mean so the package is essentially like I mean something that is used inside the same program so essentially like I mean so now if you want to compare these two packages basically like you need to put these two packages copy from harm's area and put it into your program copy from jack's area and the shuffle and then put it in the program and then we need to write the basically like then we can write the whole program to work with that and it's kind of cumbersome basically like I mean so one thing that we want to do is we don't want to even depend on harm or jack in order to develop our program so so for that we need we need to turn these packages into modules so to turn this into modules we save harm's package as harm.pm and jack's packages jack.pm but one small change that we may we put this one column I mean one semicolon as the last line of equal so then so then they are packages so then harm can check in this module I mean these packages are converted into modules now the harm can check in this module and then jack can separately check in this module and then they are somewhere basically so in order to use the these modules in our script basically what we need to do is we need to use the keyword use followed by the package in it I think like I mean you already saw the use of the keyword use essentially warning use strict these are all the various ways that we were using this and then the name of the module pile usually is the name of the package basically with the extension pm so essentially like I mean so we just say this is use harm and then use jack in this case so here we let's see like how we do basically so the harm.pm itself the way that we will write is we use the same use strict and warning we call it as a package HRM and then we write the shuffle then you can see that the one semicolon is there now in the Perl script the way that we can do it is basically use harm and use jack these are all the package names essentially because the file itself is stored with harm.pm so this is the packages so we kind of imported this packages from these locations now we give the same thing and then this is the same command that we saw and then this should produce write expected results we can also export functions by the modules into the main namespace so here we basically like I mean we use what is called an exporter and then essentially like I specify using the code word the exporter into this array and then export is nothing and basically like this is the another variable these are the two preset variables that we will see what they represent in the next sections so the export okay functions have to be explicitly imported basically so essentially like I mean so what this means is like I mean once we add this in in front of our programs essentially that becomes the modules of the functions that can be exported so actually there is a e missing here you can say basically so we say basically like for the shuffle function the export is okay for that particular function and then that's so that's what we mean here basically like for functions have to be explicitly imported so here essentially like I mean we use the same thing basically and then we say like shuffle sequence and then we will say like jack shuffle sequence so see here that actually like we don't need to specify harm anymore here that the reason is basically like I mean we cannot we don't need to put the arm because this is something that is what is being exported from this function so for the harm actually like we are using this so basically like it is directly exported into this main namespace so you don't have to specifically assign a jack namespace for that particular shuffle so wherever you call shuffle it's assumed that it's basically coming from harm okay so the location of the Perl module is the other thing basically that we want to talk about essentially it's essentially like I mean it's specified in this at INC array so once you have the Perl minus V which is the verbose often then you can actually like print this at INC to see where the where Perl looks for modules in your path so and then you can also add this path with the Perl 5 live in unendurable which is use live and then path to the libraries so now the question is like why we use modules so as I mentioned basically like there are there is one reason which is the sharing and core development of various programs but let's look at this essentially so the number one reason is essentially to organize the components of the program so what we can do is when we have like large programs you can develop some of the components as a bottom sub approach where we write the essential functions and then those functions can be stored in various files and essentially like an export mechanism or modules to export that into the main main program so this is one reason the second reason is the code reuse there we can use the same once we write a particular sort function and if we know that that is the most efficient way of doing we can use that over and over again in other scripts and we don't have developed this at all you can use reuse the code from one to the other and then the third main motivation for using the modules is it's a convenient way to publish or share the code with others so there are a lot of modules and their wide range of functions are available so this is one website that I want you to go and check for the various modules in Perl so some more details on the modules modules are an important and powerful part of Perl programming language the module is a named container for a group of variables and which can be loaded into your program by naming this collection of behaviors and storing it outside the main program you're able to refer back to them for from multiple programs and solve program solve from problems in manageable chunks as I said this is essentially like promoting the reuse mechanism the modular programs are easily tested and maintained because you can actually test test the modules and maintain them and you can avoid repeating the code and then the the mainly main reason why we can avoid repeating the code is also because we need to change any kind of bugs in only one place and then all the others automatically gets changed and then Perl modules can may also contain documentation they can be used by multiple programmers without each programmer needing to read all the code so essentially it promotes multi-user people to work on programs simultaneously and it also promotes developing a big environment which does several things collection of tools and techniques basically for solving a given problem and they can be generated by that and then the modules are foundation of the CPAN which contains thousands of ready to use modules many of which you will likely use on a regular basis so Perl installation the CPAN modules also becomes and then you can use them for your programming purposes so I want you to take a look at the CPAN the website is given the previous one so now let us look at what is CPAN so before that like I mean as I mentioned the Perl modules they are set of related functions and in a library file they are specifically designed to be reusable by other modules and program currently there are about 108,000 modules that are readily available for your use so what is CPAN? CPAN is this particular thing basically it is a comprehensive Perl R-time network I want you to go and like look for it because the CPAN function CPAN modules are widely used and pretty much it comes with every Perl installation and your installation if you have this and one thing to also notice most Perl modules are written in Perl but some use XS and they are they require C compiler modules may also have dependencies on other modules almost always on CPAN and cannot be installed without them so or we need all the without a specific version in the programming form so and then the CPAN itself in the modules in CPAN they require a reason for Perl which is Perl 5.8 so this is another location essentially like meta CPAN.org where you can search the 108,000 modules I mentioned on CPAN so think of these modules as now your apps only thing is like these apps are actually connected to each other so that I mean we can write a super app by just meshing these apps into that super app so essentially like and then so this and CPAN you can think of it as the iTunes for Perl and that has about 108 modules ready 108,000 modules ready for you to develop the new apps so I think like I mean this gives you like a good overview essentially now we will look at the one code segment basically we have one today here essentially like I am trying to list the contents of a directory so the first statement you know basically like you go to Perl use strict your warnings so here we are actually now exporting a module essentially called path and then basically like it contains the package class so here essentially like I mean we are we are defining the directory basically so it is full bar essentially that goes by full slash bar you can have like many many more and then they are all like we are opening it as a directory we are calling it as a direction and now we want to iterate over the contents of the full slash bar basically we are saying here this particular scalar variable is taller der next essentially the next file first we see basically like if it is a directory then we will skip basically so we do is there a test essentially on this particular file and essentially we just break the loop and then go to the next one if it is if the test becomes true if the test test is not true then what we do is we basically and we print that particular line into the as a string and with the new line character so the string if I is another function that is specified in the in this one basically which essentially like takes this content of the file which is the full slash bars first one and then it it creates a string out of the whole characters and then it prints out that particular string so I think like I mean it is a very simple program as you can see like I mean so this is you can think of this as the LS command essentially so LS and the directory name you get or in this full you get this particular per command you can see that 1 2 3 4 5 6 7 8 9 within less than 10 lines you can actually make this command work so this is how powerful you can see the programs and then how powerful it can get so I think let me just recap this whole session basically for you so we started by talking about how to execute commands inside of Perl using the three main methods one is the exe c which is not very useful because it executes a command and then it comes off so you can put it as a like last command if you want to send an email or something you can execute the email command then just come out of something like that then we also looked at system the system comma system specification is essentially it executes the command but it continues from where it is where it looked off and and until the Perl script itself is finished but the return code is actually the exit status of the command it is nothing it is not the output of the command but the exit status of the command is true or false so you have to use it with a pinch of salt here basically if you do not want any output coming from the particular command to be further processed then you can use a system call okay and then finally the back tick essentially is essentially like a back tick is the way that we can actually capture the results or capture the output of a particular command into various variables and this can be used within the Perl script itself so there are a couple of examples that we quoted here you can think of this basically you can have it in a scalar context or and list or another context so these things you can do then the next one that you talked about was how to write and use the Perl modules this is one of the advanced topics essentially like I mean there is also known as the objector in the Perl or Perl 5 one of the function that we talked about which is key to all these things is the qv qw or the quote word and this is used to extract words out of the string and using the embedded white space as the delimiter and it returns the words as so a simple command is shown here essentially and so then we went into the packages and the modules essentially the modules essentially is the separate namespace in a separate file with related functions and variables so we wanted to understand what the namespace is and the namespace is something that provides a context to the functions and variables so if you can distinguish between ace sort function these sort functions and then we explained the namespace with an example essentially like where we have two two individuals jack and harm who are trying to develop the same program or shuffle and how do we distinguish with them between them so if you just put the function name with the those two implementation as functions that does not work so one way to do the come up with a solution is basically embed the names into the functions so shuffle harm and shuffle jack yeah we can do this but this is again cumbersome and basically it will be all the it's a very subjective so we cannot really implement this so in order to find the happy medium what we said was basically the individual concept of the package where we start the harm block with what is called the package specification or package keyword and then harm and then we write a sort of shuffle and then for the jacks basically jack also starts in the package jack and then the is shuffle and then once we use it basically we just say harm colon colon and jack colon column so this is this is the way how we can import those packages into our program so but the package can be turned into modules by just using the one followed by semi-colon at the end of the program or the last line of the program should have this and then if you want to use the package in the script we use the keyword use followed by the package name and then we don't have to specify the extension PM here so here basically how we use it we do the define the harm dot PM with one semi-colon and then we use it inside the thing basically this harm you give like this keyword use harm to make sure that this package is exported so another way to do it is essentially like I mean then actually export it into so this is a simple export essentially living this harm and jack but you can also give it a main context essentially or you can export into the main works or namespace so that you can avoid every time typing these some names followed by column will have a name the main context to do that we use an exporter so we specify use exporter and then we basically like specify these commands and essentially the we say that export okay is shuffle and this actually gives the the the command actually like I mean how to do it so the we say basically use harm and then again the maybe the shovels as the subroutine essentially like so once we say that this is a subroutine and with this package that one we don't have to specify the harm anymore basically because it's it's also match of the exporter and then we continue with that process then we talked about the locations essentially like how to look at the libraries and then we talked about the why we use modules again the three reasons that we outline are one is to organize the components of your own module your own programs then to promote the code reuse where you develop once and share it and then use it many times and then sharing the code with others essentially this is the key thing why how the collaborative design environment can be developed and these are all like the the functions are all available in cpan.org which is an archive essentially the cpan itself stands for the components of Perl archive network and currently it has about 108,000 modules and one thing to note is like not all the modules are written in Perl some of them are written in the XS and they also require a compiler and then we also want to note that the modules themselves basically all other modules and so essentially like we need to install the whole thing to get all the modules and then the recent version of Perl is that's another thing that we need for accessing cpan cpan modules so the Perl version has to be 5.8 or above okay so I think and then we event into this one example where we just wanted to list the contents of a directory this doesn't check other things or does not have any other things basically this is kind of like an LS-L so it does not check whether there are any like dot files things like that or any other further checks on that only like directory is it avoid and it only prints the all the regular files so even LS-L actually prints all the directories and everything this only prints all just the files so I think this is pretty much it for Perl on the next class onwards we will start the tickle programming language I hope this is this was enjoyable we will have some quizzes coming up I think the PA will organize some of the quizzes and tests for Perl so thank you very much