1 00:00:01 --> 00:00:04 Welcome to the Spoken Tutorial on special variables in Perl. 2 00:00:04 --> 00:00:13 In this tutorial we will learn about Global special variables Special command line variables Global special constants 3 00:00:13 --> 00:00:27 For this tutorial, I am using Ubuntu Linux 12.04 operating system Perl 5.14.2 and the gedit Text Editor You can use any text editor of your choice. 4 00:00:27 --> 00:00:32 As a prerequisite, you should have working knowledge of Perl Programming. 5 00:00:32 --> 00:00:38 If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website. 6 00:00:38 --> 00:00:41 What are special variables? 7 00:00:41 --> 00:00:46 Special variables are predefined variables that have a special meaning in Perl. 8 00:00:46 --> 00:00:50 These do not need to be initialised before use. 9 00:00:50 --> 00:00:58 These are used to hold the results of searches, environment variables and flags to control debugging. 10 00:00:58 --> 00:01:02 First, we will learn about Global special variables. 11 00:01:02 --> 00:01:06 $_: (Dollar Underscore). This is a widely used special variable. 12 00:01:06 --> 00:01:14 $_ - Dollar Underscore is the default parameter for lot of functions and pattern-searching strings. 13 00:01:14 --> 00:01:20 Let us understand the usage of $_ variable using a sample program. 14 00:01:20 --> 00:01:26 I will open the special dot pl file which I have already created. 15 00:01:26 --> 00:01:32 Go to the terminal and type gedit special.pl ampersand and press Enter. 16 00:01:32 --> 00:01:42 special.pl file is now open in gedit. Type the code as displayed on the screen. Let me explain the code now. 17 00:01:42 --> 00:01:49 There are 2 foreach loops here. Both these foreach loops will execute the same result. 18 00:01:49 --> 00:01:54 In each iteration of the loop, the current string is placed in $_. 19 00:01:54 --> 00:02:03 And it is used by the print statement, by default. $_ saves the use of one extra variable $color. 20 00:02:03 --> 00:02:06 Press Ctrl+S to save the file. 21 00:02:06 --> 00:02:13 Then switch to the terminal and execute the Perl script by typing perl special dot pl and press Enter. 22 00:02:13 --> 00:02:18 Here both foreach loops give the same output. 23 00:02:18 --> 00:02:27 Now, let us see another example to demonstrate how $_ variable is implicit. Go back to the special dot pl file. 24 00:02:27 --> 00:02:30 Type the piece of code shown on the screen. 25 00:02:30 --> 00:02:40 This program reads a text file first.txt line by line. Then it loops through the DATA file, till all lines are read. 26 00:02:40 --> 00:02:51 print $_ variable prints the contents of the current line from the first.txt file. In the 'while' loop, the use of $_ is implicit. 27 00:02:51 --> 00:02:55 We will see more about this in future tutorials. 28 00:02:55 --> 00:03:01 At the rate underscore is the special variable used to store subroutine parameters. 29 00:03:01 --> 00:03:06 Arguments for a subroutine are stored in this array variable. 30 00:03:06 --> 00:03:13 Array operations like pop/shift can be done on this variable, as we do in normal arrays. 31 00:03:13 --> 00:03:19 I will show an example for this. Let us switch back to special dot pl file once again. 32 00:03:19 --> 00:03:22 Type the code as displayed on the screen. 33 00:03:22 --> 00:03:35 This program will return the maximum value between two numbers. @_ - At the rate underscore is a local array which stores the two arguments dollar a comma dollar b. 34 00:03:35 --> 00:03:43 That is, it is stored under dollar underscore index of zero and dollar underscore index of one 35 00:03:43 --> 00:03:47 The print statement prints the maximum of the two given numbers. 36 00:03:47 --> 00:03:51 Press Ctrl+S to save the file. 37 00:03:51 --> 00:03:58 Switch to the terminal and execute the Perl script by typing perl special dot pl and press Enter. 38 00:03:58 --> 00:04:02 The maximum value is displayed as output. Let’s move on. 39 00:04:02 --> 00:04:10 Environment variables are represented by percentage followed by capital ENV. 40 00:04:10 --> 00:04:17 Environment variables contain a copy of the current environment variables, such as the following. 41 00:04:17 --> 00:04:23 Let us understand%ENV variable using a sample program. 42 00:04:23 --> 00:04:26 We will switch back to the special dot pl file. 43 00:04:26 --> 00:04:30 Type the following code as displayed on the screen. 44 00:04:30 --> 00:04:37 Press Ctrl+S to save the file. Switch to the terminal and execute the Perl script. 45 00:04:37 --> 00:04:42 Type perl special dot pl and press Enter. 46 00:04:42 --> 00:04:51 We can see the current environment details such as PWD (present working directory), username, language etc. 47 00:04:51 --> 00:04:55 Next we will see about another special variable dollar zero 48 00:04:55 --> 00:05:02 The special variable dollar zero ($0) contains name of the current Perl program that is being executed. 49 00:05:02 --> 00:05:05 This is generally used for logging purpose. 50 00:05:05 --> 00:05:14 For example: I have a file named First.pl within which I am using $0 variable as shown here. 51 00:05:14 --> 00:05:19 On executing, it will print the filename First.pl. 52 00:05:19 --> 00:05:24 Perl has a built-in function called sort that sorts an array. 53 00:05:24 --> 00:05:30 A comparison function will compare its parameters using the numerical comparison operator. 54 00:05:30 --> 00:05:38 This operator is represented by lesser than equal to greater than symbols, as shown here. 55 00:05:38 --> 00:05:40 Let us see an example for this. 56 00:05:40 --> 00:05:47 Open the terminal and type gedit sort.pl ampersand and press Enter. 57 00:05:47 --> 00:05:56 sort.pl file is now open in gedit Text Editor. Type the following code as displayed on the screen. 58 00:05:56 --> 00:06:02 Let me explain the code. The first line declares an array of numbers. 59 00:06:02 --> 00:06:08 The numerical comparison operator will compare the two values as numbers. 60 00:06:08 --> 00:06:16 'Dollar a and dollar b are special package local variables in which the values to be compared are loaded. 61 00:06:16 --> 00:06:21 And this sort function will sort the numbers in ascending order. 62 00:06:21 --> 00:06:25 Let us now save and execute the program. 63 00:06:25 --> 00:06:31 Switch back to the terminal and type, perl sort.pl and press Enter. 64 00:06:31 --> 00:06:35 We can see that the numbers are sorted in ascending order. 65 00:06:35 --> 00:06:39 Let’s see another special variable dollar exclamation. 66 00:06:39 --> 00:06:48 'dollar exclamation if used in string context, returns the system error string. Here is an example of its usage. 67 00:06:48 --> 00:06:59 If the file hello.txt doesn't exist, it will print the error message,like: Cannot open file for reading: No such file or directory. 68 00:06:59 --> 00:07:04 Let’s now look at another special variable namely, dollar at the rate. 69 00:07:04 --> 00:07:12 'This is another widely used variable. It returns an error message, returned from eval or require command. 70 00:07:12 --> 00:07:17 'This example will print: could not divide Illegal division by zero. 71 00:07:17 --> 00:07:26 dollar dollar is yet another special variable. This holds the process ID of the Perl interpreter running this script. 72 00:07:26 --> 00:07:32 The diamond operator is used to read every line, from the files specified on the command line. 73 00:07:32 --> 00:07:35 Let us see an example for this. 74 00:07:35 --> 00:07:42 Open the terminal and type gedit commandline.pl ampersand and press Enter. 75 00:07:42 --> 00:07:46 commandline.pl file is now open in gedit. 76 00:07:46 --> 00:07:49 Type the code as displayed on the screen. 77 00:07:49 --> 00:07:51 Save the file. 78 00:07:51 --> 00:07:56 Let me show you the text that I have in a file named sample.txt file. 79 00:07:56 --> 00:08:07 Now, run the program from the command line by typing: perl commandline.pl space sample.txt and press Enter. 80 00:08:07 --> 00:08:11 This is the text we had in sample.txt file. 81 00:08:11 --> 00:08:17 If no files are specified, it reads from the standard input i.e. from the keyboard. 82 00:08:17 --> 00:08:27 Perl has an array at the rate capital A R G V special variable. This holds all the values from the command line. 83 00:08:27 --> 00:08:33 When using array at the rate capital A R G V, there is no need to declare the variables. 84 00:08:33 --> 00:08:37 'The values from the command line are automatically placed in this variable. 85 00:08:37 --> 00:08:41 Let’s now move on to Global Special Constants. 86 00:08:41 --> 00:08:50 underscore underscore E N D (in capital letters)underscore underscore. indicates the logical end of the program. 87 00:08:50 --> 00:08:55 Any text following this special variable is ignored after this statement. 88 00:08:55 --> 00:09:06 underscore underscore FILE (in capital letters) underscore underscore represents the filename of the program at the point where it is used. 89 00:09:06 --> 00:09:13 underscore underscore LINE (in capital letters) underscore underscore represents the current line number 90 00:09:13 --> 00:09:25 underscore underscore PACKAGE (in capital letters) underscore underscore represents the current package name at compile time, or undefined if there is no current package. 91 00:09:25 --> 00:09:30 We will see a sample program on how Global Special Constants are used. 92 00:09:30 --> 00:09:39 Open the terminal and type gedit specialconstant.pl ampersand and press Enter. 93 00:09:39 --> 00:09:44 specialconstant.pl file is now open in gedit. 94 00:09:44 --> 00:09:50 'Type the following code as displayed on the screen. Let me explain the code now. 95 00:09:50 --> 00:10:00 The special literals PACKAGE, FILE, LINE represent the package name, current filename and line number respectively at that point in the program. 96 00:10:00 --> 00:10:02 Let us execute the program. 97 00:10:02 --> 00:10:09 Switch back to the terminal and type: perl specialconstant.pl and press Enter. 98 00:10:09 --> 00:10:15 We can see the current package name, filename and line number of our program. 99 00:10:15 --> 00:10:19 This brings us to the end of this tutorial. Let us summarise. 100 00:10:19 --> 00:10:25 In this tutorial, we learnt about some commonly used special variables in Perl. 101 00:10:25 --> 00:10:34 As an assignment do the following. Write a Perl script to sort the following array of numbers in ascending and descending order. 102 00:10:34 --> 00:10:39 Note: For descending order, use the below code for comparison. 103 00:10:39 --> 00:10:45 Print the sorted result using ‘while loop’ and special variable $_. 104 00:10:45 --> 00:10:47 Save and execute the program. 105 00:10:47 --> 00:10:49 Now check the result. 106 00:10:49 --> 00:10:56 The video at the following link summarises the Spoken Tutorial project. Please download and watch it 107 00:10:56 --> 00:11:03 The Spoken Tutorial Project Team conducts workshops and gives certificates on passing online tests. 108 00:11:03 --> 00:11:06 For more details, please write to us. 109 00:11:06 --> 00:11:13 Spoken Tutorial project is funded by NMEICT, MHRD, Government of India. 110 00:11:13 --> 00:11:17 More information on this mission is available at this link. 111 00:11:17 --> 00:11:22 This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.