 Hi everyone once again welcome to this lecture in the classes the programming class, today we will be continuing our discussion on tickle. So last time we saw we actually like went through a set of commands basically the list command the string commands global expressions actually it is a regular expression in the globbin style operation essentially or globbin style matching and also we talked about the exact matching using the string commands these are all like they all fall under the string basically and then we also looked at the globbin command which is actually different from this globbin style and then we also looked at scan and format commands for reading in information and also like formatting the outputs then we went into the control structures essentially like while loop six loops all those things we looked at for loops and things like that so the key thing about tickle is again I want to re-emphasize this is the first lecture that we did tickle has two parts there is a interpreter interpreter and then there is a parcel so as you all know tickle is nothing but set of words separated by white space and it is also like separated by the new line characters or every line every line is detected as one command each line of tickle script otherwise like I mean the command delimiter is semi column so anything that is either a semi column or a new line character is treated as one command and then that command is again further split into white spaces basically like so each word is split by white space and then that particular command is sent to the parcel so interpreter itself does not play any part in understanding whether it's a command whether it's an argument example once it parses once it passes all the words to the parser then it's the parser's job to understand okay the first word is interpreted as a command this is the command and then once the command is interpreted then it actually like now looks at all these arguments so for each command it knows like how many arguments so once it understands that there is a command that's when the arguments are decided and that's when it actually like goes through in detail as to what exactly is each command can do and how many arguments does it take whether you filled out all the arguments or more and things like that so this concept is very important because this is how like I mean we will be writing our programs and examining them so this will help you in debugging all the the programs again so you can avoid like a lot of common mistakes if you understand this structure and this is how things will be done once you understand this then you can avoid a lot of issues okay so just looking at simple programming actually like there will be like more examples that will be in your labs so this is a quick program to generate a random number can anyone tell what exactly it does there are like different things basically one I mean I want to draw some attention to the first lecture and the second lecture actually especially the second lecture where we talked about two major things one is the command substitution and then the variable substitution so this is command substitution variable substitution so in this one in this particular command at least like this that one thing is happening it is a command substitution if you look at the square brackets here so anything within that one that square bracket is interpreted as a command so here we have this command so the pearl I mean for the tickle interpreter when it goes through this it separates it out basically set random number and then it substitutes this command and then basically once this is again recursively called so it goes to the parser now the expr has been parsed and it knows that it is a command and that needs certain arguments so here we have an int rand and then with nothing and then times 10 so now here we do not need that so it knows that actually like this argument is in rand with just parenthesis so this rand is further called as a function and that rand is actually defined as rand will get a value random number between 0 and 1 so that is what this returns and surely enough 0 and between 0 and 1 exactly some it is not an integer it is just a value basically it is a floating point number now this that gets multiplied by 10 so that gives you a number between 0 and 10 now and this number can be a floating mobile and then now the integer is applied on top of it and which converts this number into a regular integer in 0 and 10 and then that is how this expression evaluated and that is assigned to this random number so the random number actually equals random number is any number between 0 and 10 and it is an integer so I hope like I mean this simple example but it has enough complexity in it to explain how this works so now that we are seeing this this example this actually sets the the topic for today which is essentially the tickle procedures so we will cover a number of things in the tickle procedure today and this is probably the most important thing to learn and understand in tickle so let us look at the procedure so before I go that in tickle pretty much like all the programming is developed as procedures and then basically like the top level program will just call these procedures for to get the values essentially so if you look at the look at Perl programming language we had functions that pretty much did a lot of these things but here it is the procedures so how do we define a procedure that is the first thing so first we put this keyword called proc and then the next word is the name of the procedure and then we have list of argument names then the body so a proc command how many arguments it take if you look at here we have sub 1 this is argument number 1 this is argument number 2 and here we have one at least two different words but if you look at this the curly braces this makes it into a single word as we know and that this is the third argument so a proc command will take three arguments the first being the name of the proc the second being the list of arguments and then the third is the body of the proc so I want you to remember these things basically so that you can easily identify a tickle script and easily identify parts of the tickle script and also you can understand what exactly the script is trying to do and procedures behave just like built-in commands so here basically here we have a the same thing basically with an argument of 3 and then that gives to basically sub 1 3 gives the value as 2 and then the arguments can also have default values so for example but again they are all like in just one word so we will be putting a lot of these calibrated for example here the proc dcr this has again like I mean after proc it will be 1 2 and 3 but if you look at the second one we have not just one variable we have one one variable here and one way two arguments to it and then the first argument does not have a default value the second argument has a default value of 1 and then the expression is basically x minus y which is this x minus this y that what is will be passed back to the program that calls this procedure so so fairly simple we will introduce some more additional concepts later on which is the proc arguments and our proc attributes and things like that we do not want to impose on you with all those other details so the complex details simply put you have a procedure with essentially all you got to remember is the proc statement or proc command has three arguments the first argument being the name second argument the arguments and then the third one is the body of the the procedure so now let us look at a procedure so here we have a top level which sets is 0 and then we call the procedure F there we set I to 2 again once we do this it is not okay why is that not okay because we have defined already I here but if you use it inside the procedure what happens the second one is now like if it is 1 then set j is 0 and then increment j increment j even though like we are setting j as 0 this is okay why is this okay so in procedures and in tickle in general there is a concept of scoping which is what we will talk about in great detail today the scoping is essentially to understand what is the life of a variable for example here the life of the variable is only up to here so when you go here it cannot be this is a different procedure starts and then this is a variable that is already here so tickle will be erroring out whereas here the j scope is all the way to the end of the procedure start here and goes all the way to the procedure so if you increment j if this is okay so this is okay this is not okay that is because I does not have the scope into the procedure and they are using it something which is not defined so now the first question is how do we get I into this program can we do something so that the program recognizes as recognizes I and actually moves forward one quick way to do it is put it in the argument right which is which is what like I mean in you can do essentially and then once it is in the argument then you can it's a fair game as to like what you're going to do with it and then you can but oftentimes we won't be able to do it and so tickle actually provides some rich commands it again the main thing is there are local and global variables and the interpreter knows variables by their name and the scope each procedure introduces a new scope so the scope is what was making the one that is not able to read so here is another example which is very similar to the previous one that we saw we set x to 10 and then we say like the procedure is set x expression dollar x minus dollar d here it is even more severe now we are using this x setting the outside procedure and using it inside this is also not allowed so if you try to run the program with delta x delta x 1 it will just come up with an error message saying that cannot be x no such variable so how do we correct this issue so tickle actually supports another syntax called global once you specify the global then automatically the scope is transferred into that procedure as well so if you say proc delta x d and then immediately say global x and then you do the set x expression dollar x minus dollar d it understands and then now it comes up with a fact so this is one which is delta x 1 equal to 9 because this particular one is actually just subtracting the number from number 10 okay and the global itself is an ordinary command essentially you mean it is again it is governed by its own command means so global is a command actually it is not a syntax specified in the language so again want to make the distinction again and again so in the global here if you say now we are named and then that is what you are declaring here as global then we set the variable name the dollar variable name to passing by reference essentially so this is a tricky one there you can see that actually it is an ordinary command where you are actually passing the same thing basically into that command and then you are actually also like setting that variable name like to the dollar I mean dollar variable name as passing by reference so to do like some of these things there you can also pass now the variable or path the scope which is defined inside the procedure to outside so the global is pretty much takes the scope from outside into inside rock now how do we communicate inside proc to outside oftentimes we may want to implement based on a procedure some global name or even like a local name which we need to pass it to another another one so for this actually we have two commands which are up or and up level so the level naming is another one basically so the top level is named as zero and then the zero is actually also global and then one is just one called the two is two called the extra extra so what that means is if you have a top level and then you have a proc a and then inside the body you have proc B and then inside proc B you have proxy and then you close it so this kind of scenario actually from the top level so in dentures proc a let us level one of B is level two and Oxy is like okay so here there is one more example which is zero is current one is caller two is caller etc etc so here is one simple procedure where we have increment bar name we say up var one var name var and then set var equals var plus one so there is a tie up now between var name and var from outside and all you got to do is if you increment automatically get some data so what does up level do up level does for the code what up what does for the variable so in up or essentially we are spending like the variable name to other procedures whether it is higher or lower in the high level whereas in the up level you are actually now transferring code to the other levels essentially so how do we do it essentially the theory is the one example the first procedure for loop from to script now again how many arguments are there for the proc it's one two three and four I mean actually not only one it's one two so that's always the same now if you look at from to script that's another command so essentially like I mean it's basically we take the I from both from and then that I to two we will do like up level the script and then implement I so here again like I mean the script is sent to the next level so here and as an example that is empty and then loop 15 that s is $ s star and then puts $ s it puts these actually this is loop one two five so five stars it generates now how do we capture variable length argument lists and oftentimes it's not a fixed length so you need to capture the variable length but here there is a quick example the name is some there are arguments and then basically the body is shown all the way and then inside we say like set x is equal to zero and then for each I args increment s by one and then return s so first argument is one so it is only once then the second argument is two then it is twice and then it basically computes the sum of all the integers that is specified from as from until so some one two three four five becomes 15 some just some is zero so again this args is another keyword that we will use to capture a variable length argument list so whenever we say args that means that the interpreter already assumes that it will be like not one but more than one which will come now let us look at some of the error handling so errors do occur and they normally about the commands in progress and then the application will display an error message so here a quick example basically there is no like $ I or $ I here so we set this expression and then we so here it is easy to spot this error and then correct it sometimes you have to look into the particular error and look for more information so one way to get more information is this set error info which provides more information regarding what went wrong so here you can see basically like this syntax error in this expression that you wanted to be actually put together for testing and then and it also says basically like I mean it's a syntax error in this expression while it is here this and it is invoked within which command it's getting invoked from and look at how the command is represented here it's actually the complete command where this one here and then it also indicates the for each body now essentially like from where that this one is called so there are some advanced error handling features so there is a global variable already declared for you it's called error code that holds the machine readable information about errors but it can interpret errors and then it can do some exception handling so for example here there is a simple one which is catch XPR 2 plus message so the catch returns 0 for okay and then 1 for error and then you can say like set message for getting the message and then that now gives you the syntax error in expression 2 plus and then the other one is like I mean using these commands you can generate errors by yourself for example you can say like error what is the error and then you can have like a return as a code error and with bad argument now let's look at some of the IO files so we will have like few more items on the procedures which I will plan to do it next time so so file IO commands essentially there are many of them one is open gets see flash blog use read tell cd we f configure f blog file event puts source EOS pwd and clock name so all these are file IO commands actually a file name so the file commands use tokens to refer to files so here the opening my file or text that gives back this token file for and so that I mean you can use that in further usage so here like we can say like dog web write the text into file so this gets printed out into my file or text and then finally once you are done we just close the file handle or the token which is just close the dollar now the other commands gets and puts basically they are line oriented so when we say like gets $f it reads one line of $f into x the read command and the specific number of bytes so we can say like I mean read only the 100 bytes from the file $f then seek tell and read and do random access of IO so they can go into a particular sector and then read from the things like that so here we just say like that f is open database form are in read only mode and then we do a seek basically of the 100 and 1024th element and then we can read from the 100 I mean actually this should be 1 1 2 3 so we can read all the way up to that much actually sorry this is actually 100 because this is in the number of bytes so read $f 100 reads 100 bytes starting at 1024 so that becomes 1 1 2 final ending of the other 5 so that's what is shown here there is there are some nifty commands within tickle that can help you with further processing of file here for example the open file called log with read only access and then we can actually do file event as a command basically and then we can say like if $f is readable then we can do certain things basically so the file event is such a command which is useful to you can watch the file without actually getting involved but there are some issues I don't know whether this is already addressed or not and try it out and then see what are these issues that are addressed in world which means then there are two other commands the f block f block and f configure that gives you the fully control over these files so f f f configure minus bucking it's basically like you can say line full and then f configure minus blocking through or false if configure minus translation and then f block typically returns the boolean it's on it it's on it