 Hi everyone again welcome to the next programming and scripting course today we will be continuing the lecture on Perl programming language today is lecture 8 of the Perl programming language so last lecture we covered several topics actually two main topics and then we went into details of those two I will recap that and then we will move to the topics of today so let us look at the last three last time what we covered the main the two topics that we covered last time was hash array of the associative array and the references referencing of variables the hash array of the associative array essentially this is defined we saw the definition is basically defined as with percentage and then the name then we also saw how to add the database objects into the array of to delete and how to modify the objects typically like I mean the modification is and then we use the double braces essentially to denote like I mean which object that we want to remove one key thing that we noticed was actually when we define actually we define it as key value pair and usually we see the association by using an equal to and greater than symbol to form the association so typically like I mean all these forms of a key value key value etc etc and then that is how we can define this door of new array so each of this is an value we can access and the modification is simply by calling the key and then changing that into a new value so that is funny it changed and then we also saw that to initialize the hash array we will just simply do percentage name equal to just open close parenthesis and that will initialize the array itself and then when we use the hash arrays we use it with either the keys or the values and when we we can actually like just get all the keys or all the values one key thing key difference between the hash array on the regular array is that hash array has only the association between the key and the value it does not have any association with the key key one and key two for example so there is no relation or value one and value two in a regular array we know that actually the value gets implemented by 0 1 2 3 etc so each object will have its own index here the index is essentially like I mean to know the key and then the value so and then again we can access the just the keys basically and then we can start on the keys sort on the values and then we can get the various for a sorted keys what is the value for a sorted value one of the keys those kind of things we can easily get from this array this is very useful for as a two dimensional array as I mentioned more we can denote like points of in a plane things like that essentially we also saw one example which is kind of a dictionary creation is either a dictionary creation more even a phone book creation actually like what the example that we saw was a phone book creation there we can have the indexes as indices as the name of the people and then the values are the corresponding phone numbers so and then couple of other things that we saw was actually like the addition is not actually it does not follow any rule it just gets added an element so we cannot say that okay this is goes in the end of the queue or end of the array or beginning of the array we cannot say anything like that it can go anywhere in the middle because it is it is almost like a random access so essentially like as soon as we give the keys that particular element we can access directly we do not have to see really go through one two three four kind of the keys before accessing that element so there is a lot of good features and one thing that I mentioned last time also of that a lot of programming actually revolves around the hash arrays in fact you will see any kind of practical application there is a whole bunch of hash arrays that we could use in order to actually do this do a meaningful programming on me to use of both and the two popular features of Perl that gets used more often one of them is the hash array the other one that we talk about it today and anyway like I mean once you master these two elements you you can pretty much write any program inside Perl of course there are some data structures that we will cover in the next lectures public lectures also the other thing that we noticed or know we learned in the last lecture was referencing and be referencing the variables so what is a reference of a variable it is nothing but the address where it is so so you know that actually the memory structure is usually there is all these it is almost like a table you can think of this and then on one side is all addresses and then this is the value so a memory with like a 32 gig will have like 32 gigs of addressing space and each one and store a byte or a bit essentially 32 based on the 32 gigabyte or 32 bit you can have that many storage capability so in a bit system maybe like every bit can be at this individually which is not meaningful here say like assuming a byte 8 8 bits can be at this one shot so and the 8 bits will have a unique address corresponding to it so variable is nothing but one of this chunk of data which it does an address and a corresponding value and typically we only like when we say like the dollar X equal to 5 you only have the dollar X as a value now what is the address where this five is stored in order to get that that is what we have been mean by referencing and the referencing we can achieve it by an escape character like a backslash dollar X this gives a scalar value and this scalar represents the address of that particular variable so like arrays and more things like that so we can easily get these references we also saw like wanted to show you a couple of things so here essentially like I mean the list can be created by actually these square brackets the reference to hash is created by imposing the list in a curry braces essentially so these are the some of the things that we already saw and then all of them are we notice that actually like the dollar because the way it is a kind of a variable which is a scalar quantity so it is denoted by just dollar so once we have this the how do we be referenced a reference already given reference in order to do that we use to select a double dollar with X and then that gives us basically back the provided the X is a scalar quantity which is an which is a reference itself then we can get back the actual value so then we can use this the dollar X to modify the the values of the reference structure and that also we saw here actually and for an array actually like I mean to modify one of the variables we use the dash arrow to or dash greater than to actually modify the element the particular element and one thing that we noticed was in this concept of dollar R 3 is not equal to dollar R arrow I think like I mean we have to notice that basically what the given R here the R is not changed in basically so the same here the R is used as an array variable here the R is used as a reference and that is the big difference why they are not equal so I wanted to understand this one so I think like I mean with this we can introduce today's topics today we will be talking about functions which is the natural progression for the next topic and then we will also talk about some of the the other biggest topic of the mostly most widely used item in Perl which is the regular expressions so let us look at functions essentially function is a short combination for repeatedly used code so essentially you can so if you are using something basically like that you are over and over using over and over and in order to compute certain things you can put it under a function and then call this function whenever you need it so this reduces the ease of programming in Perl as you know like I mean there is more concept of compilation so in a regular programming language like C or C++ when you write functions the functions are actually compiled during the compile time and then they are they come into the actual programs and they can be replaced with the various parameters similar to like subroutines and here in Perl since it is not a compiled language we only have functions there are no subroutines as such so the function consists of a function header and a body so the body is the block of the code that executes and the function is called the identification of the header is through this keyword a Cb and then the name of the function so when we call like sub and then followed by the name that means that it is it is a function and then so so there again the same thing basically for the function declaration consists of the keyword sub and then the function name the declaration promises the full definition somewhere else so you can just define it and then basically like you can actually write the function somewhere else and then the function call itself this is like when we actually use the function in a in a particular code this can be part of an expression as well so in this case the function must return a value and that is that we get used in the expression the function call can also be a standalone statement in this case a return value is not required if there is one then that is pretty much discarded so sometimes I mean there is a return value which is just used for testing purposes like I mean whether the function was executed correctly or not so whenever a function is called the body begins executing at this at the first statement of this function the return statement in a function body causes the function body to immediately stop executing that particular function and then return the control back to the original program and then the return statement is also has an expression if it is the written statement also has an expression the value is written as the value of function itself so return statement is another key thing basically like I mean so we terminate the function by using the return statement and then if the return statement just has a just mainly just it is a vanilla return statement without any expression that is the function that returns the value and if the execution of the function reaches the end of the body without encountering a return statement then the return value is the value of the last expression evaluated in the function so Perl assumes an implicit return essentially like so you know you got to be careful so if you do not specify anything and then basically like we have a chunk of code the last expression evaluated will become the return value for that particular function so when you are using it we got to be careful so the variables so here there is a concept of a local variable one thing is essentially you know in a function the variables that are not declared explicitly but simply assigned to have a global scope so you do not have to have actually like define declare it explicitly and they all have a global scope which means that actually it spans across the whole program in order to use a local variable this is something that we saw in the previous example also the keyword my declaration is used to declare the variable in a function body and that is a local function if the local variable has the same name as a global variable the global variable is still not visible within the function body it is only used that the local variable is what is what gets used it also supports a form of dynamic scoping using a local declaration so and then the my declaration is what is the keeping and then that is that has a lexical scope it works similar to the rules of C++ and Java we will also see like some of the other types of actually declaring the variables in the future some of the other programming concepts basically how we can pass variables between the subroutines or the function calls now let us talk about another key thing that is the parameter so we saw like I mean actually this one we know that it is a block of the code that is basically like that is what is getting executed the header actually contains the sub and then the name of function and we can also declare parameters at that point the parameters that are used in the function call are called the actual parameters the formal parameters are names used in the function body and refers to the actual parameter so actual parameters is linked to the formal parameters so you can say declare a function sub XYZ, A, B, C and then when you are using XYZ, $ E, $ U, $ R you are like the same $ this just execute this within the code so if you look at this one basically like I mean so this subroutine essentially like I mean has these are the actual parameters and these are the formal parameters so in Perl the formal parameters are not named in the function header so we do not have to actually like specify this even without this they will get passed and the Perl actually supports both pass by value and pass by reference as means of passing the parameters into the function so there is a special array which is like the array underscore is initialized in the function body to list to the list of actual parameters an element of this array is a reference to the corresponding parameter changing the element of the array changing the corresponding parameter so again we already talked about the references and the derepensers so think about this one basically in this array we only store the references so as we change the elements that corresponding the those parameters are actually the corresponding actual parameters changed as well so the values of this particular array are assigned to the local variables which corresponds to the pass by value okay so these things will be like clear when we go through an example I do not know that we have time to do an example today if not we will be doing an example in the next lecture okay so here here is some some other examples here so here we declare a plus 10 as the function and then basically like here we do not have to specify the formal parameters so here we just specify $ underscore 0 plus equal to 10 this means that basically like it can be expanded as you return a value which is whatever the parameter that you get plus 10 so that is the name of the function and then the usage is listed here basically so this becomes the actual parameter so when we do the plus 10 of dollar a the dollar a becomes whatever the previous value plus 10 so previously like dollar a you are assigned to 0 then after plus 10 dollar a dollar a will have a value of 10 so the first line of this function copy the actual parameter to the local variables so there is this function that we can think of which takes basically like values essentially and it basically like assigns local variables from these from the array underscore okay so let us look at how do we pass the structures as parameters couple of things that to note here are one is an array or a hash will be flattened if the included directly in the actual parameter list so it won't keep it as is basically they not get flattened the reference to the hash or array will be passed properly since the reference is just a scale along so if you are passing it by reference the those references will be passed correctly so let us look at a sort function the sort function can be called with the first parameter being a block which contains a numerical value based on the comparison of two variables dollar a and dollar b and this parameter is not followed by a comma for example like using the sort that is a less than equal to greater than b the and to sort and numerical array this will sort the array the NUM using the numerical comparison whereas when we do this and be in the other way then it will sort in the reverse order so this way basically okay so we can actually now write a function essentially using whatever we have learned this could be a good way to actually find out how to write a function we will discuss this example in the next lecture but meanwhile you can try actually writing this function test median essentially like to find a median of an array passed as a reference value okay so this could be an example that we will discuss in the next lecture so I think like I mean so this is mainly the functions how much we define the function that now let us go to the pattern matching this is another this is the second most important topic of topic in Perl once you master the pattern matching and the hash arrays pretty much you got 80% of Perl covered already so let us look at the pattern matching some of these things that we talked about this in earlier lectures so I will I would like you to remind some of them and essentially Perl has a very powerful pattern matching facilities built in these have been imitated in a number of other system the regular expressions that describe in JavaScript chapter and this is also like from Perl that JavaScript has borrowed this regular expressions so the pattern matching itself is indicated by this M operator M we can think of M as a match operator and this is used with delimiters like the Q and QQ but the enclosed characters form a pattern essentially so if the delimiter is just the slash then the M itself is not required the match itself is indicated by the equal to tilde operator with a string on the left and then the pattern or string variable it should be string variable on the left and then the pattern on the right actually like I mean the variable containing string or even just a nascent string is okay in this case actually so okay so if you just give a pattern the default string that it assumes is the brahava underscore which is current line that you can think of and then a split function can take the pattern as the first argument rather than a character so even for a split function we can declare the pattern now the pattern itself the pattern specify the pattern of characters used to split the string into multiple pieces so let us look at some of the example actually like I mean like I think like the example itself here it is probably like not there we will come back to this so a simple match essentially like I mean we can think of this as $ underscore equal to tilde we can specify M operator or then flash and then the particular pattern and then followed by the flash so this is one matching structure essentially like I mean matching pattern and whenever this pattern is matched it can generate like no it we can do some things with this matching essentially so how do we remember the matches essentially like I mean so we can use parenthesis parenthesis and then we can also denote variables string variables to explicitly contain matches what this means is essentially like you can say $1 equal to $x equal to some match XYZ and then ABC so the parenthesis elements will be remembered into this particular ones and then in fact if you if you do not even specify this just specify just XYZ, ABC, EQR, DEF so this particular matching pattern is always as $1 and this is a smaller 2 so that is if the pattern matches a string the variables $1 $2 refers to the part of the string matched by the parenthesis sub patterns so for this sub pattern 1 it is remembered as $1 for the sub pattern 2 let us remember as $2 so this is basically like I mean gives you look the way to actually do it and then essentially like you can do a you can write a match $ underscore equal to and then you can say like $1 if $1 is whatever and then you can execute a code so you can actually tap into the matched component and then do processing based on that most component so if a match is successful on a string there are three strings that are available to give the context of the match so the first one is the $ ampersand that gives the part that actually match the pattern and then $ this mactic is the part of the string before the part that much and then $ the normal take which is the part of the string that the after the part that but most so in this example like even if you do not specify like $1 actually it is still available to you because XYZ ABC TQR and then this is the match that you wanted essentially like I mean so or this is the string and then basically like you are matching you see then the this will contain ABC this will contain XYZ and then this will contain TQR so these are all like some of the shortcuts how we can make use of the matches so matching itself is the way to identify a particular line or then you can do some processing part of line and then we can actually go into those matches and then identify how to actually do those matches and how to actually make use of the matches so if you are searching so the matches essentially like I mean you can think of it as way to search through a document for a given pattern now once we search and find out something this is like a in a Word document we are doing a fine moment finds various occurrences of a particular string but the next logical step for in your those kind of matching is so once you find something we need to actually replace it with new stuff so in word of word you can think of this as like I just find and replace commands but how do we do it in Perl that is what is known as the substitution so we know that we saw actually like M for matching now we introduce a new operator to the S operator which is used for substitution again in substitution we can actually find the pattern and then replace it with a new string and then the new string will replace the part of the string matched by this particular pattern so if you say basically like ABC replace it with one two three and then the initial string is actually one s is the dollar so dollar s is equal to say xyz abc d2r then when we do this basically the color s is equal to or even this slash abc slash one two three this abc will be replaced by this one two three so the new string is actually it's dollar s is xyz one two three four and then that's what this stands for again with the equal to tilde operator is used to apply the substitution to a string if the operator is not used then dollar underscore is operated upon by default so if you don't specify anything like a moment just say that s abc one two three then the string the dollar s it is actually assuming it to be dollar underscore and then so now there are things that we can attach to the end I think again this is this is something that we saw like briefly we talked about this one of the earlier earlier lectures of and there are several modifiers essentially like I mean that we can add it to the end essentially one such modifier is the G modifier the G modifier on a substitution causes all the substrings matching pattern will be replaced so G stands for low and then there is also another modifier I I modifier makes it is insensitive so when you use an I think of insensitive or essentially it's case insensitive so an uppercase abc will be same as low so you can put it as I G there is also like I mean so these are all like different base to substitute now we also talked about this operator in the previous lecture in the earliest lecture this is the transliterate operator operator also known as TR essentially in the in transliterate operator essentially we can replace a character list with another character list so it is not only like actually warm so if you think about here we this is a string the first one is a pattern but the second one has to be a string so we are actually taking a pattern and then replacing it with a string here actually it can be a just a capital list and the capital list and this can be just a pattern essentially so when we apply this translator operator to a string it causes each character of the string that appears in the first list to be replaced with a corresponding character in the second list if the second list is empty then the characters from the first list are just deleted from the string so use this operator with caution again here also like I mean we use so equal to till they as a prefix operator to apply the transliteration and if you don't specify any operator then it is basically like $ underscore the software performed by default so now let's look at some of the file input and file output we have this concept we introduce the concept of a file handle in the previous lectures when even when we talked about Perl we talked about that we talked about file handles even the context of Unix in order to carry out a file input and output a file handle must be created for each of the file the open function is used to create a file so there are two functions in Perl one is called open the other one is close so the open function is used to create the file handle the file handle is similar to the file handle in Unix essentially this is an address of the files but here we have this address generated by the Perl inside of the Perl program that now allows that particular program to access the file so it is not the same as what is specified in the Unix file system but it is very similar to that context that concept so the first parameter to open function is the name of the file handle essentially the the convention basically like we keep the name as all the parameters just so that to distinguish between a normal variable and then the second parameter to open is the string value naming the file and optionally including the characters the more of opening the file so typically this will be like open FH my file so this particular declaration now opens my file for V and with the file handle of FH so read means it is basically it is only for input and one thing that note is basically I do not put any characters here that is because the input is the default so you do not have to specify this or explicitly we can specify open FH so this particular declaration there will be class specifically my file as only a freedom if you want to write to the my file then we open the open the my file as open FH arrow my file this one will enable the my file to become an output or essentially we can write it into the my file and here the my file will be completely overwritten by this statement so any previous content will get deleted the third option is essentially we can do open FH double arrow and then followed by my file this essentially like I mean which indicates the open for output appending to the file which is already existing so then we do this double amp sand I mean double arrow or double greater than this actually causes the file to be appended at the end by any new changes at this point so the file will be open but previous content will be preserved and then any new content will be added so now how do we actually write it into the file essentially for that we need to use the file handle in the statement print so by default actually like I mean it is going into the standard out but when we can also specify given file handle and then to write a particular pattern or a variable into that one essentially so in this example basically like the out handle is something that is already defined in which we are actually adding the data and more data into it so couple of things to notice there is no comma after the out handle and or there is no special parenthesis or anything or the print function has these parameters essentially this file handle followed by the string and essentially like I mean we omit this file handle because standard out is the default for print now there is also another operator called the input operator which can be used on the input file handle the read function reads the number of characters into a given array essentially the function returns actual number of characters read the function parameters can indicate that characters are to be stored in an array somewhere than other than at the begin so these are all the two different other functions so the input operator itself is used to read from this file so you can think of it as basically like I mean so typical perl programs are open file handle with my file and then we will say while we can say file handle this implicitly calls reading this file and then reading it line by line and this delimiter is essentially like I saw this is already mentioned it is dollar slash so if you modify then you can change the default is actually that fashion so this dollar slash is actually that fashion and it reads up to that value and then so that is how we write programs essentially and for specific read essentially like we can actually use the read function itself this reads number of characters into a given array the function returns actual number of characters read the function parameters can indicate that characters have to be stored in the array or somewhere somewhere other than in the array somewhere other than at the begin so you can actually do a man read more information and then the seek function this is can be used to position the file handle first are at different positions in the file these are all some of the advanced functions you can typically read this line by a line and then if you are not if you do not want to process any line you can actually go with next as a simple way to actually move past or move the file handle past the current position location instead of using the seek function seek function is much more wrong autonomy and you can actually use it to really fine tune where you want to put the put the cursor essentially so to so to just to a doing a recap essentially today we saw some key topics essentially like I mean number one but we saw also the function calls essentially the function essentially like this consists of the function header and the body and then the it is basically like the keyword the sub identifies the function the declaration essentially like I mean so the function itself essentially like I mean it can be written anywhere you can declare the function but have the actual function classification somewhere else the function call is essentially like I mean it should it can be a part of the expression if it is part of the expression then the return value is actually used inside that expression if the function call is made stand alone then there is no return value and there is even the function return something is just discarded and when the function is getting called the body begins executing at a first statement the written statement is whenever it encounters the written statement the function stops executing one keeping to notice is basically if you do not have an explicit return statement the last expression that is evaluated will be returned as the return value of function so use it with caution use always and then we also talked about the local variables usually the variable declaration in Perl actually like variables need not be declared explicitly and everything will just have the local scope but the my declaration is used to declare the variable within the function and which are local to the function and then the mine is also like having so all these variables will have panamics poking inside Perl and so it understands the local declaration and if the local variable has the same name as the global variable the global variable itself is not visible within the function body and only the local variable used and then the parameters essentially like I mean these are the parameters for the function and then the function call has the parameters that should have the parameters and those parameters are called the actual parameters those are always needed the formal parameters are essentially used when you define the function at the latest stage and they have a one-to-one correspondence with the actual parameters and in Perl the formal parameters are not named in the function header you do not have to specify them we can directly start using them inside the functions and Perl supports both pass by value and pass by reference you know how to initialize the array essentially like I mean so it is basically like I mean it getting assigned to various values and this is something that we saw in this example here where when we specify the the array underscore it is split into like all these various values so other function which is an implicit function then we talked about the start a little bit essentially we will come back to that example then we talked about the pattern matching we talked about the m operator the m operator is the match operator that actually like matches a particular regular expression to the to the particular given string and then we can actually use form if you omit this equal to tilde operator then implicitly it is assumed as matching is on the dollar underscore the matching gives it is a particular character set is present in the in any of the strings that we are searching on one thing to notice actually like we haven't really touched upon how to create the the patterns themselves what are the rules of the pattern which we will I will talk about it in the next lecture but at this time just understand that there is this m operator and then followed by that actually there is the ace operator but before we go into the ace operator the to memorize or to remember the matches essentially the match was successful or not well provides lot of good syntax help one is the part of the pattern can be parenthesized and then use it use basically so if you have like the dollar 1 dollar 2 refers to the part of the string that was matched by the parenthesized sub patterns but how to get to the the string itself the match string is essentially like by using the dollar ampersand dollar back taken the dollar over take so the back take essentially like dollar ampersand will have the actual part that matched the pattern so pattern can be specified and basically this could this is just a string within the string itself so the string is split into three and then one part is basically the matched pattern the back take is anything before the most pattern and then the other the regular normal take is part after the the match that happens now the ace operator is used for substitution essentially like so we can substitute one pattern with a new string I will underline this string here and using the the ace of the substitute pattern here also like I mean the same rules like the matching pattern applies with a equal to tilde for any string and then if you do not specify that equal to tilde then the dollar underscore is assumed as the input string and then we have a pattern