 Hello students again, welcome to the the class so this is about the Linux programming and scripting today we will be talking more about the Perl today's class actually in today's lecture I am going to talk mostly about the things that we have already talked about I want to reinforce some of the concepts and also give you some more additional items with respect to Perl as we know Perl is a very versatile language used very widely across many applications not just limited to the software side it is also used in the chip design side of things a lot of them using Perl as the main scripting language so last week we covered some of the things about functions I am going to go through it one more time just to reinforce the concepts we talked about the function declaration how to call a function passing the parameters local variables and returning modules so these are all the main things that we talked about and I hope that you have already understood this just in case I am going to go through these things in more detail so today we can think of today's session is largely a recap session but there will be like few more items that will be covered again those things like control structures that we talked about briefly when we address the the data structures themselves we talked about in context of the data structure here we will be more formalizing talking about how the control structures work what are the different control structures etc. So again on the functions we talked about the function declaration I just want to reinforce function declaration we use the keyword sub to describe the function these parameters are required so the function should always start with the keyword sub it should preferably either in the end of end or in the beginning of the main program this improves the readability Perl does not have any such requirement as to where the functions declarations should be so you can you can use it in the beginning of the program or at the end of the program but use it in one place consistently all the time so that that improves the readability and then how do we use functions essentially like I mean there is a function pass so here the get name is the function and then we provide this ampersand and then once we specify that that becomes Perl understand that this is a function and it goes for the particular function declaration and then it looks for where the function is actually defined and then it runs that function so the symbol and percent should always proceed the function call in any function call the parameters essentially like I mean the always the parameters are passed as a list so even if you have array that you are passing that is expanded and the entire list is passes parameter one of the thing about Perl is that the Perl keeps this parameter list in this particular variable which is an array variable ampersand or at underscore and this essentially like I mean means that it is this list and then you can directly access each of the parameter by accessing the elements of this list so if you pass only one parameter the size of the this particular list is just one variable but if you pass two parameters then it will be two and then these can be at this size dollar underscore zero and one underscore one and then if you have more number of parameters this just goes on and the array thing is a very nice feature so that you always like I mean it does not really constrain you to have only like fewer number of variables of pure number of parameters when performing in any form I mean performing a function call the variables to be glad in the main program are by default global variables so they will continue to have their values in the function also what this means is essentially the scope of the variable is extended into the extends into function okay so if there are any local variables the local variables are declared by using my in the beginning to declaring the variable so these money types of variables will have the context specified in that level so if you are using my inside the functions and so that has a scope only within that function does not go outside right off-site of it and we also saw the return values essentially like I mean the result of the last operation is written unless you have an explicit return statement in the function in which case whatever the return value that specified inside the statement that is returned one thing to notice there are no pointers in Perl but we can manipulate and even create complicated data structures okay so the other thing that we just touched upon was is used to form a list from a scalar data depending on the delimiter so if so here one example is R 101 and space Tom space 89 percent and then we declare this as basically like I mean so specific and array and then we just say split and then that means that basically the split happens the space the blank space here and it is on this variable which is dollar underscore so the resulting value is essentially as an array like beta dollar beta 0 equal to 101 dollar beta 1 and then 2 89% so the default delimiter is the space and the default array is dollar so here so the dollar actually we should be so the data 0 will have 101 data 1 will have Tom and then 2 will have 99% so as I mentioned the default is dollar underscore if you want to perform on another scalar variable then the syntax is split with the delimiter and then the actual variable so here the delimiter is against still space and then the line value but in general the syntax is the delimiter and then the variable itself there which one you want to split and we also saw this in the last class actually but whenever we have a split the dollar ampersand so the value of the matched pattern or this is it for the matching five not split and then this one the dollar over takes stores the value that is after the pattern and then the dollar back takes stores the value that is before the pattern so now let us look at the join the join actually does exactly opposite job as a split it takes a list and then joins up into all its values into a single scalar variable and this also use the delimiter provided no matching and replacing this is also something that we saw in the last class the last lecture if you need to look for a pattern and replace it with another one we can do it with this s command this is actually a lower case s with replacement pattern again by default this acts as a on the dollar underscore variable you can act you can make it work with other other variables and say like I mean okay in this new valve this is what we want I want such to this pattern now what is pattern and what is pattern matching and this is something that is crucial also like this is not we do not talk about it in a big way this lectures the pattern is a sequence of characters that can be searched for in a characteristic and usually the pattern is represented by those flash and it is in between two flashes and then we know that equal to a Tilda tests if the pattern is matched and there is also a way to do a test with it is another the pattern is not matched and that is bang tilde so what are the patterns so here typical pattern one pattern is just a set of characters in those within the flashes and this will match even defined because the F is part of the define so it will match that if you specify two blank spaces then it only matches the blank on either side of death here the carrot actually anchors the death of the first three characters in the line so here it matches this death and then if you specify like I mean the dollar denotes end of the line so if you are saying like carrot and carrot death and then dollar then it has only one word in the line which is BF so only the BF will match and then the question mark sign is basically used for any in the middle so here the question mark F matches BF BF like I mean so there is nothing here the E is basically like it may or may not exist now the B and then like in square brackets E or uppercase E this matches either BF with the lower is E or BF and then if you do a carrot before the EE then if there is EE then it won't match and anything other than EE match for example a V be everything then if you put a dot and then that means that it can match any character in the middle basically that denotes like any character so it can be space non-space things like that so all these will be matches and then the play plus denote basically it is one or more so it is the ABCF will be matched and then if you put a star then that is zero or more so BF will be matched and then the BA F F F F F so all these things will result in a match now this is an interesting one it is a double braces on one and three and it is BEF what this means is the E can occur one to three times so essentially it can be the EF or the tripling number in the double braces and then it will see if those many items are there and the three comma the empty spaces three or above so it can match any number of ease which is three or above and then 0 to 3 matches only up to be F which is on 0 to 3 of E so now let us look at the character ranges themselves as to how to specify and what do each of them denote so in Perl we use like 1 2 3 4 5 6 different letters or denoting the pattern so these are you can think of this escape sequence as a short-hand notation so what I mean is the square brackets 0 to 9 means like any digit matching backslash D escape D and then an uppercase D denotes anything that is a not digit which is same as putting the carrot in front of here which is do not match 0 to 9 but match anything other than that now the W is essentially a word character which is an underscore 0 to 9 uppercase A to Z and then lower case A to Z so all these are considered as a word characters so if you specify backslash W that is the short-hand notation for this pattern so instead of that we can just use the box and then uppercase W is basically it matches anything but a word character so that means that the same pattern with the carrot in front that denotes not of the pattern backslash lower case S this is kind of counter intuitive here basically opposite of the or end of file any non-space characters are matched using the backslash S and then same thing like backslash uppercase S will match anything but but a vice so that is why we have a carrot in front so these things are used sometimes you can get extract them out so here there is a pattern which Fred and Barney and then basically okay so actually the we can use the parenthesis as a memory so here when we match the Fred Barney and then basically give a character in between and then when we say like this slash one the one represents the first parenthesis so that means that it is this one so any replacement that will happen will happen on this first character and then you can you can put many other parenthesis and then you can denote like which one that you want to replace with the character as I mentioned actually this kind of using the parenthesis as memory and using it to do the replacement you can actually try it in VI because it uses the same very similar syntax for doing the replacement. Let us talk about the control structures in Perl we have the following control structures if unless statements so if unless statements we will see some examples of how do we use these things and then while until the while is probably like I mean one of the most popular statements in Perl then we also have for these are formal for statements and then there are for each statements which can operate on this so we see how we can do that and then we also have some of the control altering commands last next and we will see how these things are used and then finally like I mean there is a logical operators actually these are more like of operators but they can also be used as control structures and let us see how they are getting used so let us start with if the use of if is very similar to that of see we already saw this deeply in one of the earlier lectures the way to use is if then followed by condition then we open the double braces and then we will write our program and then we close it and then we can say else if or else and then also like put the condition the unless is a command which also has very similar syntax as if and here the syntax is shown you are basically unless and then the condition and then the double braces so when you want to leave the then part and have just an else part we can just use unless so this is basically equivalent of so you have if and then else and we do not have anything here then we want to use else and then the else of the same condition basically instead of else we use unless unless okay so this is basically like for if and unless we will see a program with all these various constructs and then we will try to decipher what the program does and how do we document that so and then the next one is while until and for the while is very similar to the while of C usually like while is the one that we use for doing an infinite loop. So while some condition and then we double use the double braces to open and close for all the program elements here the while is kind of it is an infinite loop as I mentioned essentially like until the while that condition is satisfied so for example if you are going to read a file particular file we can always say while the file is still not empty then we do some operations we read the file the line by line and then we do it and then the way to establish the while the files no longer not empty is while as there is as long as there are lines in the file so we just look for that condition basically like the dollar line or the lines are still valid in that in that file and then if we want to counter that basically and then that is we use until so until essentially is in the why how we use and then followed by the condition and then the pants and then the double braces so here say like while condition and then this is same as until not condition so I think if and unless work the same way as while and until so that is how you should look that look at it so if if is the equivalent to while then unless is equivalent to until until is equivalent to so the statements are executed till the condition is met and then the far or is also very similar to the C implementation here for I equal to zero or dollar I zero dollar I plus plus then the block of code so this executes this loop for 21 times or 20 times rather now the for each statement the for each statement actually takes a list of values and assigns them one at a time to the scalar variable executing the block of the code with successive assignment again if this is also a loop but here the argument is a list of values so we can say like for each dollar war the list that means that the dollar war will inherit list one list two list three etc until the list is exactly and then we can perform operations on all the elements of that list now let us look at the last last is similar to the break statement of C so if you want to quit from the loop in the middle we will just use the last so and then if you want to skip the current loop and go to the next statement essentially we use the next statement and then this basically like I mean exists the current loop value but still is in the loop and then goes to the next value and then keeps going we saw that the the ampersand as a percent and the the two bars as if we have a statement unless condition one then condition two we can just replace it by condition one and condition two because that is what they point to and then if you want to open a file and if you want to put a message that if the file operation is failing then we typically use the double bar operator to actually specify that so we fail a condition and then double bar and then the print the file cannot be opened so when we use this kind of modifications it makes the compost structure smaller and more efficient.