 So we are talking about some of the control structures, I also wanted to highlight one of the things about sort we learned about sort in the previous lecture we can use sort to order a list and assign the result to another variable for example here in this one the let one is all these various letters and then we assign the sorted form of let let one to the array let two one thing to remember with sorting is when we just use a regular sort it is always an ASCII sort but what that means is it results in the lower case of this appearing after the upper case letter also the number will sort in a different order than you expect for example the 12 will be after one or two because each position will be sorted basically the numbers will be sorted on the positions it is not on the numerical value itself. So if you want to start a different way like I mean for example the real numeric sort there you want to have 12 before 102 you need to have different sorting orders so for that we use we need to use separate ones separate parameters we saw this briefly in the previous lecture so here I am going to elaborate it a little bit more as to how to change the sort order so essentially as I mentioned basically to change the sort order you need to add an argument to the sort function the argument is a block of code which contains instructions as what is the new sort of so inside the block of code we define two variables they which are dollar a and dollar b and these are the elements these are the variables used to indicate the elements in a list and then we define a logical operation between these two to denote what is the real sort order the logic returns one of the three values when comparing a and b that is one if a is less than b or one if a is greater than b or zero they are equal so assume that I mean this is the given condition so we use now a specific operator called a spaceship operator which is less than equal to greater than all three combined it looks like a spaceship so hence it is a spaceship operator this performs the comparison of a and b the spaceship operator essentially returns negative one if the left is less than the right and one if left is greater than the right and zero when they are equal so here is how to use it basically so the new list is sort a spaceship operator B and then the list so this is real numeric value sort essentially like so essentially this works with numbers and it is does not work with strings so just take care when you are using it but we saw this operator and this how to do a sorting of numerical arrays at the time we didn't specify the spaceship operator in the detail so that is the reason why I introduced this these slides so in order to reverse essentially like our sort from the highest to the lowest all we got to do is this what this is also we saw in the previous one basically like dollar B and then you use the spaceship operator dollar a if sorry a and if this is the condition then the list or the new sorted the sorted list will be from the biggest one to the lowest so in other words like in 102 followed by 12 which is I mean not the the number order but essentially the numerical value essentially just keep in mind also Perl has the grep function the grep function typically searches for patterns in an array and then the syntax is essentially like the prep followed by the pattern and then the list so for example if you want to find all the elements with pattern day in an array which is array is defined as Monday Tuesday and Friday essentially like I mean when we say like the array list equal to grep the pattern the pattern is always enclosed within the the slashes that we already know about and then this is the way that we are specifying here this will populate the array result with the matching elements in this case all the three will be left in this so why is so let's see like I mean how it works how that works basically the grep works by proceeding to an array one element at a time assigning the element to the default variable dollar underscore this we know about the dollar underscore it's pretty much common then the pattern to be found is compared against the government so it goes through these two operations number one is to assign one element to the default variable and then it compares the pattern against the dollar underscore and then once that that is successful then it is getting the pattern is found then the expression is true and then the element is written by the pattern is not found then the element is not written by them so so so this way we can accumulate that element or in an array so why is grep important is because now we can do like hash intersections essentially so one common task in Perl is to find intersection of two hashes or arrays so means like we built one array from one file and then we grab to see like whether there is any much so to find an intersection the grep function is very useful so how do we do this essentially what we do is so here the problem is to find the intersection between these two arrays so here we declare another hash called temp and then we for each of the elements of array we actually create the hash or essentially like add the elements into the hash with the index as the arrays element and then the value of the actually the key as the array element and the value as one now the intersection is fairly simple the array intersect is same as grep of the temp dollar underscore and then of all the elements in attitude so for each of them basically like it finds out what is the the value there so essentially like the code start by setting up an empty hash for each stores each element in array one in dollar underscore one at a time and fills the templates with them and sets the value to one the last line examines the act array one element at the time and sets it to dollar underscore which is grep in the asher a temp if there is if there it is an intersection element and that is added to the set so each value each one from array two that is stored as dollar underscore and that is searched in that array in basically in the hash array and if it is there then it is returned that particular value and which is actually stored in more instant so this is a very easy way to do an intersection what if you want to do a difference of two ways so all we got to do is we just negate the the search condition essentially so we build the same array only thing is like we do the grep we do a bang in front of the temp underscore temp dollar underscore so this will give you the intersection or essentially the difference between the two arrays so whichever the element that is in array one or array two that is not an array one or both of them that though those elements will show up in the at the array intersect now I want to introduce you to a new variable which is a dollar bank so when an error gets recorded by Perl that error gets stored in a special variable called dollar bank so when we examine when it is examined numerically dollar bank shows a number but if you are examining as a string it shows the error message from the operating system we can use this as part of the dice string essentially like so if hand open and then dollar bank within your line so this statement will display the message can open followed by the error string from the operating system when the value is triggered so instead of even bombing out of a program this in die we can simply use a warning to the user and then the warning messages are conveyed through this one so this is again another subroutine with the message and then it basically gives the warning message the one command will display an error message but the program will keep running so you can use the one codes to do the error codes with the one basically that is the same dollar bank along with one so we talked about the file open file close one thing that we did not do in the last lecture was how to test various files essentially so what this means is essentially like and we can also test whether the file is read only write only or whether we are opening a directory things like that so this is the similar kind of test that the same let us perform by units essentially both can be done inside of Perl as well so the usually like I mean the this test is done as if that are file and then this file is predetermined file handle and then we do some operations so this particular test basically like I mean the read only essentially test for whether this particular file handle is a read only file and this condition has one valid option followed by the file handle to be tested alternatively we can use a filename or a full path of the filename instead of a file handle so here we do not we are not restricted to just using the file handle we can also use the full name of the file so the test essentially like I mean one of them we can perform and what are the tests essentially there is a whole bunch of tests in Perl dash uppercase B is true if it is a binary file dash D tests for whether it is a directory dash E just tests for whether the file exists in the whole tree and dot dash F checks if it is a regular file dash uppercase M returns the age in days since last modification dash R basically to the readable or not dash S will return the file size dollar uppercase T is true if it is a text file dollar W is a writable file and then the I mean that sorry dash W is a writable file and dash Z is true if the file is existing but it is an empty file so these are all the various tests that we can do in Perl so we can actually use these tests to verify files when opening or writing and if you are prompting the user for a file name you can also check to make sure that the file exists and it is the correct type of data that you want to use it in the program you can also test to make sure that you are not overwriting so now the other operator that you will always find in Perl programs is what is called strict the Perl's keyword strict is essentially like I mean it tells the interpreter to use much more care while evaluating or when evaluating statements and to display the warnings and error messages for everything it finds questionable essentially like to one thing to note is usually Perl will let you get away with quite a bit of quite a bit before actually complaining about something using strict is a good way to enhance your programming abilities to use this one basically just would use strict at the top of the code so now let's talk about the Perl debugger so the Perl debugger of the parts of the debugger is Perl interpreter is a debugger that you can use to examine the execution of the Perl scripts the debugger allows step-by-step execution of scripts examination of variable values and the use of a breakpoint so these are all like typical use of a debugger which is also like available in Perl the debugger is built into every Perl interpreter and it's activated using the dash D option and launching the interpreter for example Perl minus D my program dot text activates the debugger when you launch a program with the debugger option you will see the version information for the Perl interpreter and then help prompt enter H or quote H H for help then the first line of the script you'll see a message showing which file name the statement is in and what the line number was finally the debugger prompt DB will come up so at this point it is waiting for your command so when the debugger shows you a statement it's in the cache already to be executed but it has not been executed that is the key thing basically so it is not executing the statement it is waiting for you to tell that to execute each statement read by the debugger can be examined and manipulated prior to it being run this allows for some changes or a combination of the before each statement is executed this is ideal for debugging the script if so any word my valuable command can be used at the debugger prompt you can get help from within the debugger at any time using the H or help command usually followed by the command that you want to mission about for example the help on breakpoint command is simply type HB the command H H shows the summary of available commands and their syntax so that is like help on help and think of it that way to page the output from the help system put the the line line operator essentially in front of the command such as bar H H this lets you to page down the system commands to list the next 10 lines in the pulse script use the bar command every time you issue the bar command the next 10 lines will be shown listing the lines does not affect the line that is being executed it simply shows you the next 10 lines of the next line to be executed is shown like 3 equal to greater than sign you can also specify which lines to show by using a range that is L 10 through 15 is essentially like shows line form 10 through 15 I think this is the L to run each line one at a time in the debugger we can use the next command or the end command each line show is shown on the screen before it gets executed to see the value of any variable use the print command at the front which is print $ bar 1 and we give you the the particular value of the variable and then the current values can be shown without affecting the program and similar to other debuggers we can use the end command to step through each line of the program when a subroutine call is encountered by the debugger in the script it executes the subroutine as a single column does not allow does not show the lines in that subroutine to jump into the subroutine and move through line by line we use the s or the step command when you issue the step command the debugger shows each line one at a time executed inside the subroutine and all valid debugger commands can be used inside so so using the break points essentially actually break points let's look at the break points we can use the end command to step through the program let the debugger run all the lines until the something is not this is the break point and it is set with the command we can set a break point at any line number by specifying that particular line number so for example be 10 will set the break point at line 10 and see or the continue command will let's you continue executing after a break point it has been today so how how do we use or why do we use break points so we can set a break point on any line of the script except those that have just the curly braces or closing balances a blank line or a command usually break points are used after a loop subroutine written or any complex command so that you can verify the actions taken you can set the break point anywhere except those listed above to show all the break points that are set in the script use the uppercase l commands to remove the break point we can use the command powered by the line number or the subroutine number if the break point is set to the subroutine for example d37 will delete the break point that is set d37 and then the reset command you can reset the debugger to clear all the break points and the variables and restart the execution of the script from the top just one command which is the uppercase r command and reset is executed any defined variables lose their value and then the first line of the script is to be executed is to be executed line so the built-in debugger is acceptable for simple phrasing and debugging it is all it is not suitable for very complex debugging tasks also it is not graphical but there are many GUI based debuggers available in the market some bundle with Perl distributions and some are standalone debuggers the active state Perl distribution as a Windows debugger in the package for example and there are several available or Unix and Linux so I think like having that pretty much concludes this half an hour this lecture you will continue from this point in the next one okay thank you