 Okay, again welcome students to this another lecture of Linux programming and scripting today we will be continuing the Perl last time we started talking about some of the concepts of Perl just slightly advanced than what we talked about before so we will be continuing the same discussion today what I will do is I will start with the recap and then I think you remember I assigned a quiz a simple problem to you which I wanted you to experiment and find out the result so I will explain that problem again and then we will continue on with today's topics today we will be focusing mainly on further some of the strings and some scalar operations and some more some answers again let's look at what we did in the last lecture we studied about the operators essentially and then in Perl the operators are superset of the C I'll go and pass go operators there is an automatic conversion between strings and numbers the strings basically the number part of the string is actually kept and the remaining portion is this turn to null and then the operators wise actually there are many operators the main ones are addition subtraction multiplication division modulus and these are all the arithmetic operators then we have a bunch of relational objects that go between the numbers and string these are less than greater than equal to not equal to less than or equal to greater than or equal to things like that we already saw that and in the table as to how to do the operation operations operators and then we also like to basically what is the difference between we have a number as a variable or string and then there is also this concatenation operation operator which is pretty much this dot so that's that's another one and then we also saw the repetition operator x essentially this one actually when we say like they say like you assign the string to name and then the roller name times three is like a name name name and then we saw like I mean how that works with me I think one of the key concepts that I introduced in last lecture was this concept of the operator presidents table here the presidents the first one between the various operators it goes from top to bottom this is the highest presidents essentially operators and then basically then lower president so if these operators come together first the the higher president of evaluated before evaluating any of the lower so that is the first rule that we saw and then between the operators it goes by this associated so for example like I mean to think about exponentation operation and you have multiple ones like the six exponential two exponential one or something like the wrong zero so how the way that it evaluates is from right to left so basically evaluate this first and then it evaluates this whereas an operation like plus or minus or multiplication they will be like I mean so plus two and then so that's the other other key aspect that we learn with this we are able to solve so some of the things that are that why I want you to pay attention to one is this non-associative ones this is the increment decrement which is what we will be talking about today also like these kind of things list operators so those are like the non-associative one so pay attention to these essentially like I mean this will become important as you go so anyway so now we will look into the next one so we actually like I mean I asked you one question where we define the dollar a at five and then the B is actually more using this binary operator which is two operators combined plus one equal to which traditionally represents a dollar a equals dollar a plus two but now if you do this why is this bad and what is the value of this is the question that I asked during the last lecture I want to give you the answer today the number one is essentially why is that is because what you expect will never be the result of the expression so here you may expect that basically these actually seven times three should be 21 but that's not the case so why is it not the case and what is that value so before that I mean if you look at the presidents would like I mean say is basically in this kind of scenario the order does not matter so since the order does not matter assignment can and in fact will happen before the input so what that means is a is assigned a value of 2 and here also is a value of 2 whereas the increment operation is just omitted it happens but those results are not safe and the actual answer is this 25 which is tolerate and tolerate for people who have already experimented with this and got this answer that's great for people who haven't gotten this answer I will try to I will ask you to try to experiment this and see whether you are getting this answer if you don't get this answer please let me know like a send me an email with what the answer that you want and then we can discuss what is the but anyway the bottom line is doing these kind of things are very tricky and never use these kind of syntax unless you are ultimate you are absolutely sure that that's what are intended to so that pretty much covers what we did last time so in today I am going to actually talk about more sort of binary operator we will talk about unary operator which is essentially like auto increment and auto increment and then I will talk about some of the caveats on that and then we will go into more interesting topics and I will talk about some of the things as to how you can manipulate programs for programs so without much I do let's look at this lecture so the first one is the auto increment call it auto increment though like both plus plus as well as minus minus both of them of the auto decrement but these are all the unary operators meaning that they operate only on one variable you do need the second variable and these kind of operations are very similar people you already saw like I mean see there are several of things like there are I plus plus things like that that we always give within the loops and things like that so they are very similar to see again two forms are supported one is a prefix and the other one is a suffix the prefix is when you have the operator in the front and then followed by the variable or the suffix is essentially like a question or the operator is after the variable so here if you look at it basically and then one thing to note is these operators are applied only to the variables you cannot apply this to an expression name like $x $y and then you cannot say like I'm okay something like that so you need to make sure that you reduce this thing and then do that or we can do like that extra x plus plus and then plus y plus plus this is a valid syntax but when you write these kind of syntax there are some issues so we will learn about that but before that let's look at the simple example so a suffix 70 minus minus dollar a gives actually this should be six so it implements by one and this should be five sorry for the typo so and then these five so here essentially like I mean it is decremented by one and then in another so this is the result but now if you use this and actually there is a difference between the prefix and the suffix so let's see like what is the difference between the prefix and suffix so here are some of the rules basically like a use within the an expression there is a difference between these two expressions these two values so if you are just using it as a standalone like this it is okay basically like it still retains the value then we decrement it and put the value in me so the a six and here it is basically like a finally end of the operation is actually like this decrement for the decremented values assigned to be so it is still five but if you are using inside an expression then there is a difference between like one the first case essentially that's listed here which is the value of a before it is implemented will be used in the expression while for the second one the value of a after the implementation is actually used so let's look at one small example here is assigned as for and then we basically ask it to print dollar a minus-minus and then print minus-minus dollar a so what we get is actually like 42 or we expect that this should be like 3 2 so the reason why this for this form unchange is because it basically like this treats it as an expression and it prints this value before actually like doing the decrement so basically like it prints the value of 4 and then it determines the a so now the dollar a is actually 3 at the end of this expression but now when we go into the print next one the the the two negative sign actually like decrements be further to two and then basically like that the value that a retains and then that's what it gets printed out which is to so it's instead of 3 2 actually get 4 2 so again understand the consequence understand how per behaves and then you will be able to actually exactly do what you are intended to do okay we will again revisit this concept program but I want to leave it leave it leave you with this essentially so now there are two important functions that we will learn I just wanted to highlight the difference between these two functions what exactly do they do first of all and then what are the differences between those two functions and then how to use them in various code segments so I will illustrate through the code segments the two functions that are kind of integral to Perl are this chop and charm so essentially like I mean they are listed here the chop essentially removes the last character of the operand or of this argument and then returns the removed character string essentially whereas a charm actually removes the end of line character this is very important it has this end of line character and then it remain it returns the number of characters removed so if you do like I mean say like a dollar str equal to charm dollar string the str will have the string with the last one removed whereas if you do the dollar str equal to charm dollar string this one you will get just either str will be a zero based on whether charm removed so that is one of the key key differences essentially so one question I have is what is this end of long character this fixed one like I mean for example this backslash in that we used in previous classes how do we even change it so can we redefine the end of line character because one of the things that is kind of we will go into is to read the file mainly the Perl programs have very similar syntax basically they will open a file using a file handle so open any file and then file name once you open the file name then we will start reading the the lines inside the file which is using a while come on my HD so it reads line by line so now we have this line by line that is what is clearly defined but can we change this to some blocks actually so that we can input the whole block inside the program how do we do that so in Perl actually provides a lot of special characters one of them is dollar slash the dollar slash is actually that stores the end of line character so by default dollar slash equal to all of it oh sorry backslash in so now you can see actually if it is backslash in you can change whichever character that you want say like I mean you have the semicolon delimited file you simply change this to semicolon so then it treats semicolon as the end of line characters and it reads all the way up to that semicolon so processing like blocks of data like say like a death file or something these commands are much more useful and then basically like if you understand that the dollar slash is the character that denotes the end of line then you can easily go there and modify that and say that okay my end of line character is actually this period that means that it will read every sentence whether it it doesn't matter whether it confirms to just one line or multiple lines it reads every sentence so again easy way to actually alter the behavior so now let's see like some of the examples of charm again I didn't want to actually put examples for the chop function itself the reason is even though the chop is also used fairly closely like mean similar to charm charm is more interesting chop is essentially like just removes the last character and gives you back this train so let's see like some of the examples of charm so here is basically it's like a simple texture it as I mentioned the same thing basically like I mean in this case like I mean you are actually going to read from the standard in this we will see in like later on as to how we can do it and then essentially the sports segment says essentially like I have a own variable that we are signing to the standard in so that it waits for the standard in input until we press the new line that's when goes into the next come out so here the charm dollar text is essentially like I mean it is taking this form and then just removing the end up line character from then as you know like I mean feed into the standard in we have to press the enter in order to go to the next step and then in this one prints out the dollar and this continues on until we put nothing and then this same so this is the one way of actually looping this is most common loop which is the while loop per and for anything and everything will most likely use a while because you don't know then the file will end so we operate on line at the time until the file is so we still use that while in as one of the main work courses of Perl program so now let's look at another example there this job is used in an array basically so in this array you can see that actually Bob has a new line character and Fred has a new line character so when we do like a print before chunk and then then we print array it just means exactly the same only thing is Jill is coming in the next line and same as Fred and then this one line so it will be like Bob Jill and Fred so this forms one array definition so this is the same as this here so now let's see like I mean what happens when you do a jump on this array when you do the jump on the array actually like these two new lines are taken up and the array will simply be Bob Jill and Fred so and this is probably what we want to do so that's basically like using the charm so now there is a third use model which is actually using the the charm in the hash again in this case actually like hash is something that we haven't talked about hash is very also like similar to in concept to the C language a hash essentially labeled or a link list or not even links just a list basically which has an ordered pair concept which is basically it's similar to an associative array you can say in fact it is an associative array so here for example in associative array we saw like I mean actually like you have to specify even number of elements and then there's a first thing in the second second the third I mean sorry third with the fourth and 56 kind of the thing so here you can say like basically first is actually has this one as the its main value the hash itself is the first and then the second has to as value and then the third has 13 I mean three as the value and everything is followed by the new line character and then once we do the charm of the hash then we can directly get this number basically so it will tell you like I mean first all in one and then second so you know second all in two and then these things will be in the same line that we have in modified line and then after that there will be a print new line character so I hope this one is clear essentially like I mean so the charm essentially like I mean can it's a versatile function you can use it in many many many ways and main inside the the program the three main base of using charm I will mention here and then essentially basically what it does is it removes the new characters from the within the thing and it gives you the result after removing the new one concept what it returns itself is it's only like how many characters it's removed but at least you will the updated string is kept in the original I hope this section is clear now let's talk about the interpolation of scalars and strings so the so in a within a string like I mean if you are using a variable with a scalar or string that's evaluated inside a double coated string so if you use the double coated that we already know basically then you know that basically like the double coated strings inside any like dollar variable in all these values if the variable has not been assigned that value the variable is replaced with an empty string so if there is no value to this just basically the string nothing now there is no double substitution within the interpolation so if you have like nested thing basically the dollar x y v then actually like further down all those things basically those nested ones are not allowed so you cannot have like another quote and then something else and then end quote and use it this as a path this is that it is not allowed so that is another rule and then finally like I mean the longest possible name will be used actually sorry I missed one thing so we can use the parenthesis to separate a variable from the surrounding text so for example you have like the name of my this is the long variable or you want to just say like I mean actually I want the name variable so if you want to just ask for the name then you can just say like dollar in parenthesis name and then off my top so this is one way of doing it and we will we will actually like go through some of the examples that will make this concept very clear and then the other thing to note is also like the longest possible name will be used as the variable inside the string so if it's cannot stop it just goes on and on until time so now let's look at some of the examples of strings and the scalar variables so here there are some examples so dollar a is assigned to thread and note the quotations here and then we say basically like some text a for what will happen so here is assigned to read so we say like these some text and dollar a that is some text read so this is you must wait it now when you say like now what since the dollar what is not defined that's left as an empty string so it's basically just now now what we are doing is we are using a single code to assign thread to two dollar X so one thing to notice in you're using the single code only the backflash and the single code itself can be escape all the others you don't need to escape so you can even if you keep that dollar it's going to print dollars now we say like okay dollar Y is high as high dollar X in this case what it prints out is the whole dollar friend because understood the dollar thread as the value of that particularly and then here again a single code with a space all the space is munched basically then you get the double quotes and more high and here it's a concapnation operation essentially so basically like push test followed by a blank and then dot and then probably like followed by another one here so there are two blanks within before test and then the last but not the least test escaping the dollar basically it prints out as dollar yes so let's look at some more examples so this is kind of interesting once actually oh you are saying dollar son to pay and dollar Sunday is to wrong so then we say like I mean dollar X equal to it is a Sunday it will just say like it's wrong now as the next one is Sun space day then it will say it's a pay day because Sun is assigned to pay and if it is this is the if you put that inside the parenthesis this will have the same effect the only thing is like I mean now the blank spaces are all gone so right next to it so basically there is no blank space at all so it just prints out as it's Sunday and so here the this this variable is being replaced and then now the concapnation operation essentially like I mean the dollar son because within the double quotes that is expanded as day and then since there is a concapnation operation it's just day so we still get it's a pay day from this one so so that's one thing and then finally like I mean we can also split this in multiple lines and this operator can be used in many multiple times concapnation operation so that means that it's it's and then quote which is uncapnated with dollar son just the day so finally you will get this result it's a pay day or it's paid in so now let's get some more examples essentially these are other escape characters that you kind of touched upon it but not really learn one is the escape upper case you this stands for convert that everything in this particular string into our case so that's what happens and then we assign B to just hello hello and then if we apply that same transformation like the backslash you on the entire variable B then we get the same way this is hello and then if you use a lower case with you then it only changes the first one and then it continues on with just one H e let's see and then finally like I mean if you do a replacement of you uppercase L and then the same as this other hello hello here basically like we find that actually the same hello using this okay then now we come to another important item in Perl which is the standard in as I mentioned essentially standard in for getting the values or getting the input from the terminal itself so the key thing is like every time you use the standard in in place of a scalar in place where the scalar is expected the first read the next line from sredine so this is one way of doing an interactive session where you can use sredine to import a value from outside works so the typically the sredine reads as a line and so it reads the everything until the new line is it so and it includes that new line character so you need to do like a chop or a chop and the to remove that new line character and then usually the default in steady in is the terminal that you are used to and familiar with so that's for the standard in let's see like I mean so some of the key examples here one is we assign the $x or sredine standard it into $x and then we do like a chop $x and then here what we get is essentially the sredine without the last character so we like in another short hand form for writing this is we directly do the chop so in this one basically like we have to look into the presence metrics but first assigns this and then chops resulting value and many people just write like this and require practice because this also gets the line by line and then for every line immediately like this loop and then it executes a loop so the next operator is the print operator this is also like something that we are familiar with so we will introduce all the familiar topics first before going into much more complex and advanced topics so here a print operator observe that they have a parenthesis and we actually put this parenthesis but it's kind of redundant basically like the standard practice people don't put this parenthesis they directly write whatever they want to write so here basically like I mean this particular program just print out hello world whether we use the parenthesis or not the outcome is still going to be just hello world pretty much like I mean print is fairly easy there is one more thing which is the format we will talk about this in one of the other lectures at this point I want you to understand just the basic print command and how to use it within the the file itself essentially so so on one hand we use the STDIN for taking the input from a terminal and on the other hand we also use for print to print to the terminal so those two commands go hand in hand now the next one next topic is this undefined concept essentially so essentially like I mean the it is a numerical variable we saw that actually like a string basically it is an empty whereas in a numerical value the variable has the value set as undefined if you use it before it is given a value so what is what is mean by use before it is given along so every variable has some value that we need to initially so as a standard practice we can say like a dollar XYZ equal to 0 if you do not do this and then say like I mean using dollar dollar ABC plus dollar XYZ equals so this element equal to dollar so this is maybe this is one of the things and then the XYZ and then at this point like if dollar XYZ is not defined and only the dollar ABC is defined then you get an undefined value for this in this case actually like we have defined this as 0 maybe like okay but ABC is not defined so this people and undefined is typically treated as 0 for all the number of operations so even though it is undefined the resulting answer like I mean this will be just 0 plus XYZ by an 20 so this is whatever the value is then for string operation it is treated which is already told you the double corp string so it is used as that one now the STD in actually returns this undefined there are no more data so essentially it goes into the loop it reads all the files and basically puts on the list and it reads that essentially whatever we are providing it from the STD IN and as we hit enter it goes on until we hit like nothing basically then that returns an undefined value okay so I think like I am going to stop at this point and just to do a recap of what we did today and then we can actually come back next week or next class and then we can pick it up from this point so today we focus mainly on I hope you remember all the things there are two main topics essentially one is we talked about this auto increment and the auto increment and how do we use them in the overall programs so this is a handy way of doing it most people use it inside the form loop actually so all looks looks like something like this for $ I $ I plus plus and then you know the difference between those two so then so those are part of this rule and then we also went through the chop and the charm functions chop mainly works with strings and basically it can do it for any string whereas the chomp actually removes the new line character from within the page so and then the new line character itself can be modified so as I mentioned the new line character backslash n is actually set to this variable $ slash so we can capture this $ slash and move to whichever one that we want basically and then we can use that for as the end of line character and then we saw the examples of charm across many many different disciplines for example here it's basically just the standard way of using the charm just applied to a particular string variable here the same charm is used inside an array how we can make it better and then the finally like I mean the how do we use charm inside a hash function and then I briefly introduce this hash to you essentially it is an ordered pair basically like I can always think of like hash and then first equals and this can go like I mean basically not because the hash of the first but you can you can think of ways of using this and we will visit some of the main concepts and then we also talked about the interpolation of scalars and strings essentially where we said that basically the variable interpolation occurs inside the double coated string so that those are the main things and then if the variable is not been assigned a value then it is replaced with an empty string and there are no double substitution meaning like you cannot apply the transform many times or different transform different times within the same string and then this parenthesis will be used to denote the which one is the actual variable so here you can say like name of my past something like that and then you you are only interested in the color name then you can just say basically this parenthesis on the side and off whatever and that essentially gives only like the name as the variable and not the whole lot of other other and then one thing is also like I mean once you don't have this then it uses the longest possible name to be used as a baby for example in this one if you don't use this parenthesis then we go like all the way down from this and then we saw some of the examples of this string manipulation I hope you remember these things and then we also saw some the substitution characters like backslash u which stands for uppercase throughout so it takes the hello in lowercase and then just push it out to be okay so this is something like the translation example that we saw a couple of classes ago and then here again the same thing basically like it doesn't matter whether keeping the follow or not and then here essentially it will basically like only change the first character of the methodology the remaining are left in time and here essentially like I mean this will change all the lowercase numbers to LSE or the Lusian or actually like sorry it's the it replaces the u with L so basically that's that's the effect of this one so let's see what happens here so it basically keeps the same value as this because originally it was all like uppercase no it basically like it's the first character as the same of age uppercase age and then it went in lowercase on the remaining one which is using uppercase L if you use the lowercase L then it will only like lowercase the first meter first character and then the remaining ones it will leave them as is so I want you to understand that also so and then we also saw about the CDN how we use the CDN and the the basically like in different ways of combining the a CDN with chop and then this will be how we print at the hello world using the print operator now this is the another one so in the print operator right now we study basically the just how to print a string there are also like formatting options which are available and then we also can assign a return code help the further so these two topics we will be talking about it as we go along because these are important for the other purposes and then we also saw about this undiff basically like what happens in particular variable is not defined how does Perl react and basically like how it calls it as an undefined value so I think that's pretty much it for today's lecture you will pick it up from next week I will be assigning some more assignments essentially so that you can practice your Perl so far whatever you have learned in depth of how to build better programs using Perl language okay thank you very much