Difference between revisions of "PERL/C3/Special-Variables-in-PERL/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 10: Line 10:
 
|-
 
|-
 
|00:04
 
|00:04
|In this tutorial we will learn about
+
|In this tutorial, we will learn about:
 
* '''Global special variables'''
 
* '''Global special variables'''
 
* '''Special command line variables'''
 
* '''Special command line variables'''
* '''Global special constants'''
+
* '''Global special constants'''.
  
 
|-
 
|-
 
|00:13
 
|00:13
|For this tutorial, I am using
+
|For this tutorial, I am using:
 
* '''Ubuntu Linux 12.04''' operating system
 
* '''Ubuntu Linux 12.04''' operating system
* '''Perl 5.14.2'''
+
* '''Perl 5.14.2''' and the
* and the''' gedit''' Text Editor
+
* ''' gedit''' Text Editor.
You can use any text editor of your choice.
+
You can use any '''text editor''' of your choice.
  
 
|-
 
|-
Line 37: Line 37:
 
|-
 
|-
 
|00:41
 
|00:41
|'''Special variables''' are '''predefined''' variables that have a special meaning in '''Perl'''.
+
|'''Special variables''' are predefined variables that have a special meaning in '''Perl'''.
  
 
|-
 
|-
 
|00:46
 
|00:46
|These do not need to be initialised before use.
+
|These do not need to be initialized before use.
  
 
|-
 
|-
Line 53: Line 53:
 
|-
 
|-
 
|01:02
 
|01:02
|'''$_ : (Dollar Underscore)'''. This is a widely used''' special variable'''.
+
|'$_': (Dollar Underscore). This is a widely used''' special variable'''.
  
 
|-
 
|-
 
|01:06
 
|01:06
|'''$_''' - '''Dollar Underscore '''is the default parameter for lot of functions and pattern-searching strings.
+
|'''$_''' - '''Dollar Underscore''' is the '''default parameter''' for lot of '''function'''s and pattern-searching '''string'''s.
  
 
|-
 
|-
 
|01:14
 
|01:14
|Let us understand the usage of '''$_ (Dollar Underscore) variable''' using a sample program.
+
|Let us understand the usage of '$_' (Dollar Underscore) variable using a sample program.
  
 
|-
 
|-
Line 69: Line 69:
 
|-
 
|-
 
|01:26
 
|01:26
|Go to the '''terminal''' and type''' gedit special dot pl ampersand '''and press '''Enter.'''
+
|Go to the '''terminal''' and type: ''' gedit special dot pl ampersand '''and press '''Enter.'''
  
 
|-
 
|-
Line 82: Line 82:
 
|-
 
|-
 
|01:49
 
|01:49
|In each iteration of the loop, the current string is placed in '''$_'''.
+
|In each iteration of the loop, the current string is placed in '$_'.
  
 
|-
 
|-
 
|01:54
 
|01:54
|And it is used by the '''print statement, '''by default. '''$_  (Dollar Underscore)''' saves the use of one extra variable '''$color'''.
+
|And it is used by the print statement, by default. '''$_  (Dollar Underscore)''' saves the use of one extra variable '''$color'''.
  
 
|-
 
|-
 
|02:03
 
|02:03
|Press''' Ctrl+S''' to save the file.
+
|Press''' Ctrl+S''' to '''save''' the file.
  
 
|-
 
|-
Line 98: Line 98:
 
|-
 
|-
 
|02:13
 
|02:13
|Here both '''foreach''' loops give the same output.
+
|Here, both '''foreach''' loops give the same output.
  
 
|-
 
|-
 
|02:18
 
|02:18
|Now, let us see another example to demonstrate how '''$_ (Dollar Underscore) variable '''is '''implicit'''.
+
|Now, let us see another example to demonstrate how '$_' (Dollar Underscore) variable is implicit.
 
Go back to the '''special dot pl file'''.
 
Go back to the '''special dot pl file'''.
  
Line 111: Line 111:
 
|-
 
|-
 
|02:30
 
|02:30
|This program reads a '''text file first.txt '''line by line. Then it loops through the '''DATA''' file, till all lines are read.
+
|This program reads a text file "first.txt" line by line. Then it loops through the '''DATA''' file, till all lines are read.
  
 
|-
 
|-
 
|02:40
 
|02:40
|'''print $_ '''variable prints the contents of the current line from the''' first.txt''' file.
+
|'''print $_ '''variable prints the contents of the current line from the 'first.txt' file.
In the ''''while'''' loop, the use of '''$_''' is '''implicit.'''
+
In the ''''while'''' loop, the use of '$_' is implicit.
  
 
|-
 
|-
Line 128: Line 128:
 
|-
 
|-
 
|03:01
 
|03:01
|'''Arguments''' for a '''subroutine''' are stored in this array variable.
+
|'''Argument'''s for a '''subroutine''' are stored in this '''array''' variable.
  
 
|-
 
|-
 
|03:06
 
|03:06
|'''Array operations''' like '''pop/shift '''can be done on this '''variable,''' as we do in normal '''arrays'''.
+
|'''Array operation'''s like '''pop/shift '''can be done on this variable, as we do in normal arrays.
  
 
|-
 
|-
Line 144: Line 144:
 
|-
 
|-
 
|03:22
 
|03:22
|This program will return the '''maximum value '''between two numbers.
+
|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.'''
+
'''@_ (At the rate underscore)''' is a '''local''' array which stores the two arguments, dollar 'a' comma dollar 'b'.
  
 
|-
 
|-
 
|03:35
 
|03:35
|That is, it is stored under '''dollar underscore index of zero'''  and '''dollar underscore index of one'''
+
|That is, it is stored under '''dollar underscore index of zero'''  and '''dollar underscore index of one'''
  
 
|-
 
|-
 
|03:43
 
|03:43
|The '''print statement '''prints the '''maximum '''of the two given numbers.
+
|The '''print''' statement prints the maximum of the two given numbers.
  
 
|-
 
|-
 
|03:47
 
|03:47
|Press''' Ctrl+S''' to save the file.
+
|Press''' Ctrl+S''' to '''save''' the file.
  
 
|-
 
|-
Line 169: Line 169:
 
|-
 
|-
 
|04:02
 
|04:02
|'''Environment variables '''are represented by '''percentage '''followed by '''capital ENV'''.
+
|'''Environment variables '''are represented by '''percentage (%) '''followed by capital 'ENV'.
  
 
|-
 
|-
Line 189: Line 189:
 
|-
 
|-
 
|04:30
 
|04:30
|Press''' Ctrl+S''' to save the file. Switch to the''' terminal''' and execute the '''Perl '''script.
+
|Press''' Ctrl+S''' to save the file. Switch to the terminal and execute the '''Perl '''script.
  
  
 
|-
 
|-
 
|04:37
 
|04:37
|Type''' perl special dot pl''' and press''' Enter'''.
+
|Type: ''' perl special dot pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 202: Line 202:
 
|-
 
|-
 
| 04:51
 
| 04:51
|Next we will see about another '''special variable''' '''dollar zero'''
+
|Next, we will see about another '''special variable''' '''dollar zero'''.
  
 
|-
 
|-
 
|04:55
 
|04:55
|The '''special variable dollar zero''' ('''$0''') contains name of the current '''Perl '''program that is being executed.
+
|The '''special variable dollar zero''' ('$0') contains name of the current '''Perl '''program that is being executed.
  
 
|-
 
|-
Line 214: Line 214:
 
|-
 
|-
 
|05:05
 
|05:05
|For example: I have a file named '''First.pl '''within which I am using '''$0 '''variable''' '''as shown here.
+
|For example: I have a file named 'First.pl ' within which I am using '$0' variable, as shown here.
 
|-
 
|-
 
|05:14
 
|05:14
Line 229: Line 229:
 
|-
 
|-
 
|05:30
 
|05:30
|This operator is represented by''' lesser than equal to greater than symbols, '''as shown here.
+
|This operator is represented by''' lesser than equal to greater than''' symbols, as shown here.
  
 
|-
 
|-
Line 237: Line 237:
 
|-
 
|-
 
|05:40
 
|05:40
|Open the '''terminal''' and type '''gedit sort.pl ampersand '''and press''' Enter.'''
+
|Open the '''terminal''' and type: '''gedit sort.pl ampersand '''and press''' Enter.'''
 
|-
 
|-
 
|05:47
 
|05:47
|'''sort.pl''' file is now open in '''gedit Text Editor'''. Type the following code as displayed on the screen.
+
|'''sort.pl''' file is now open in ''''gedit' Text Editor'''. Type the following code as displayed on the screen.
  
 
|-
 
|-
 
|05:56
 
|05:56
|Let me explain the code. The first line declares an '''array '''of numbers.
+
|Let me explain the code. The first line declares an array of numbers.
  
 
|-
 
|-
Line 251: Line 251:
  
 
|-
 
|-
|06:08
+
|06:08'
|''''Dollar a '''and '''dollar''' '''b '''are '''special package local variables''' in which the values to be compared are loaded.
+
|'''Dollar a''' and '''dollar b ''' are '''special package local variables''' in which the values to be compared are loaded.
  
 
|-
 
|-
 
|06:16
 
|06:16
|And this '''sort''' function will sort the numbers in ascending order.
+
|And, this '''sort''' function will sort the numbers in ascending order.
  
 
|-
 
|-
 
|06:21
 
|06:21
|Let us now save and execute the program.
+
|Let us now '''save''' and '''execute''' the program.
  
 
|-
 
|-
 
|06:25
 
|06:25
|Switch back to the '''terminal''' and type, '''perl sort.pl''' and press''' Enter'''.
+
|Switch back to the terminal and type: '''perl sort.pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 281: Line 281:
 
|-
 
|-
 
|06:48
 
|06:48
|If the file '''hello.txt '''doesn't exist, it will print the error message,like:  
+
|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.'''
+
"Cannot open file for reading : No such file or directory".
 
|-
 
|-
 
|06:59
 
|06:59
Line 289: Line 289:
 
|-
 
|-
 
|07:04
 
|07:04
|'This is another widely used '''variable'''. It returns an error message, returned from''' eval''' or''' require''' command.
+
|This is another widely used variable. It returns an '''error''' message, returned from''' eval''' or''' require''' command.
  
 
|-
 
|-
 
|07:12
 
|07:12
|'This example will print: '''could not divide Illegal division by zero. '''
+
|This example will print: "could not divide Illegal division by zero".  
  
 
|-
 
|-
 
|07:17
 
|07:17
|'''dollar dollar''' is yet another '''special variable.''' This holds the '''process ID''' of the '''Perl interpreter''' running this script.
+
|'''dollar dollar''' is yet another '''special variable.''' This holds the '''process ID''' of the '''Perl interpreter''', running this script.
  
 
|-
 
|-
 
|07:26
 
|07:26
|The '''diamond operator''' is used to read every line, from the files specified on the '''command line'''.
+
|The '''diamond operator''' is used to read every line from the files specified on the '''command line'''.
  
 
|-
 
|-
Line 309: Line 309:
 
|-
 
|-
 
|07:35
 
|07:35
|Open the '''terminal''' and type''' gedit commandline.pl ampersand '''and press''' Enter.'''
+
|Open the terminal and type: ''' gedit commandline.pl ampersand '''and press''' Enter.'''
  
 
|-
 
|-
 
|07:42
 
|07:42
|'''commandline.pl''' file is now open in '''gedit'''.
+
|'commandline.pl' file is now open in '''gedit'''.
  
 
|-
 
|-
Line 321: Line 321:
 
|-
 
|-
 
|07:49
 
|07:49
|Save the file.
+
|'''Save''' the file.
  
 
|-
 
|-
Line 337: Line 337:
 
|-
 
|-
 
|08:11
 
|08:11
|If no files are specified, it reads from the standard input i.e. from the keyboard.
+
|If no files are specified, it reads from the '''standard input''' i.e. from the keyboard.
  
 
|-
 
|-
 
|08:17
 
|08:17
|'''Perl''' has an '''array''' '''at the rate capital A R G V '''special variable.  
+
|'''Perl''' has an array '''at the rate capital A R G V '''special variable.  
 
This holds all the values from the '''command line'''.
 
This holds all the values from the '''command line'''.
  
 
|-
 
|-
 
|08:27
 
|08:27
|When using '''array''' '''at the rate capital A R G V, '''there is no need to declare the '''variables.'''
+
|When using array '''at the rate capital A R G V, '''there is no need to declare the variables.
  
 
|-
 
|-
 
|08:33
 
|08:33
|'The values from the '''command line''' are automatically placed in this '''variable'''.
+
|The values from the '''command line''' are automatically placed in this variable.
  
 
|-
 
|-
Line 358: Line 358:
 
|-
 
|-
 
|08:41
 
|08:41
|'''underscore underscore E N D ''' (all in capital )'''underscore underscore.'''  indicates the logical end of the program.  
+
|'''underscore underscore E N D ''' (all in capital )'''underscore underscore'''  indicates the logical end of the program.  
  
 
|-
 
|-
Line 370: Line 370:
 
|-
 
|-
 
|09:06
 
|09:06
|'''underscore underscore LINE '''(in capital letters) '''underscore underscore''' represents the current line number
+
|'''underscore underscore LINE '''(in capital letters) '''underscore underscore''' represents the current line number.
  
 
|-
 
|-
 
|09:13
 
|09:13
|'''underscore underscore PACKAGE '''(in capital letters) '''underscore underscore''' represents the current package name at compile time, or undefined if there is no current package.
+
|'''underscore underscore PACKAGE '''(in capital letters) '''underscore underscore''' represents the current package name at compile time or undefined if there is no current package.
  
 
|-
 
|-
Line 382: Line 382:
 
|-
 
|-
 
|09:30
 
|09:30
|Open the '''terminal''' and type '''gedit specialconstant dot pl ampersand ''' and press '''Enter.'''
+
|Open the terminal and type: '''gedit specialconstant dot pl ampersand ''' and press '''Enter.'''
  
 
|-
 
|-
Line 390: Line 390:
 
|-
 
|-
 
|09:44
 
|09:44
|'Type the following code as displayed on the screen. Let me explain the code now.
+
|Type the following code as displayed on the screen. Let me explain the code now.
  
 
|-
 
|-
 
|09:50
 
|09:50
|The '''special literals  PACKAGE, FILE, LINE ''' represent the  package name, current filename and line number respectively at that point in the program.
+
|The special literals  '''PACKAGE, FILE, LINE ''' represent the  package name, current filename and line number respectively, at that point in the program.
  
 
|-
 
|-
 
|10:00
 
|10:00
|Let us execute the program.  
+
|Let us '''execute''' the program.  
  
 
|-
 
|-
 
|10:02
 
|10:02
|Switch back to the '''terminal '''and type: '''perl specialconstant.pl''' and press''' Enter'''.
+
|Switch back to the terminal and type: '''perl specialconstant.pl''' and press''' Enter'''.
  
 
|-
 
|-
Line 410: Line 410:
 
|-
 
|-
 
|10:15
 
|10:15
|This brings us to the end of this tutorial. Let us summarise.
+
|This brings us to the end of this tutorial. Let us summarize.
  
 
|-
 
|-
Line 418: Line 418:
 
|-
 
|-
 
|10:25
 
|10:25
|As an assignment do the following. Write a '''Perl''' script to sort the following array of numbers in ascending and descending order.
+
|As an assignment do the following. Write a '''Perl''' script to '''sort''' the following array of numbers in ascending and descending order.
  
 
|-
 
|-
Line 426: Line 426:
 
|-
 
|-
 
|10:39
 
|10:39
| Print the sorted result using '''‘while loop’''' and '''special variable $_ (Dollar Underscore)'''
+
| Print the sorted result using '''while''' loop and '''special variable $_ (Dollar Underscore)'''.
  
 
|-
 
|-
 
|10:45
 
|10:45
|Save and execute the program.
+
|'''Save''' and '''execute''' the program.
  
 
|-
 
|-
Line 437: Line 437:
 
|-
 
|-
 
|10:49
 
|10:49
|The video at the following link summarises the Spoken Tutorial project.
+
|The video at the following link summarizes the Spoken Tutorial project. Please download and watch it.
 
+
Please download and watch it
+
 
|-
 
|-
 
|10:56
 
|10:56
|The Spoken Tutorial Project Team
+
|The '''Spoken Tutorial''' project team
 
conducts workshops and gives certificates on passing online tests.
 
conducts workshops and gives certificates on passing online tests.
 
|-
 
|-

Revision as of 15:04, 28 December 2015

Time
Narration
00:01 Welcome to the Spoken Tutorial on special variables in Perl.
00:04 In this tutorial, we will learn about:
  • Global special variables
  • Special command line variables
  • Global special constants.
00:13 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.

00:27 As a prerequisite, you should have working knowledge of Perl Programming.
00:32 If not, then go through the relevant Perl spoken tutorials on the spoken tutorial website.
00:38 What are special variables?
00:41 Special variables are predefined variables that have a special meaning in Perl.
00:46 These do not need to be initialized before use.
00:50 These are used to hold the results of searches, environment variables and flags to control debugging.
00:58 First, we will learn about Global special variables.
01:02 '$_': (Dollar Underscore). This is a widely used special variable.
01:06 $_ - Dollar Underscore is the default parameter for lot of functions and pattern-searching strings.
01:14 Let us understand the usage of '$_' (Dollar Underscore) variable using a sample program.
01:20 I will open the special dot pl file which I have already created.
01:26 Go to the terminal and type: gedit special dot pl ampersand and press Enter.
01:32 special dot pl file is now open in gedit. Type the code as displayed on the screen.

Let me explain the code now.

01:42 There are 2 foreach loops here. Both these foreach loops will execute the same result.
01:49 In each iteration of the loop, the current string is placed in '$_'.
01:54 And it is used by the print statement, by default. $_ (Dollar Underscore) saves the use of one extra variable $color.
02:03 Press Ctrl+S to save the file.
02:06 Then switch to the terminal and execute the Perl script by typing perl special dot pl and press Enter.
02:13 Here, both foreach loops give the same output.
02:18 Now, let us see another example to demonstrate how '$_' (Dollar Underscore) variable is implicit.

Go back to the special dot pl file.

02:27 Type the piece of code shown on the screen.
02:30 This program reads a text file "first.txt" line by line. Then it loops through the DATA file, till all lines are read.
02:40 print $_ variable prints the contents of the current line from the 'first.txt' file.

In the 'while' loop, the use of '$_' is implicit.

02:51 We will see more about this in future tutorials.
02:55 At the rate underscore is the special variable used to store subroutine parameters.
03:01 Arguments for a subroutine are stored in this array variable.
03:06 Array operations like pop/shift can be done on this variable, as we do in normal arrays.
03:13 I will show an example for this. Let us switch back to special dot pl file once again.
03:19 Type the code as displayed on the screen.
03:22 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'.

03:35 That is, it is stored under dollar underscore index of zero and dollar underscore index of one
03:43 The print statement prints the maximum of the two given numbers.
03:47 Press Ctrl+S to save the file.
03:51 Switch to the terminal and execute the Perl script by typing perl special dot pl and press Enter.
03:58 The maximum value is displayed as output. Let’s move on.
04:02 Environment variables are represented by percentage (%) followed by capital 'ENV'.
04:10 Environment variables contain a copy of the current environment variables, such as the following.
04:17 Let us understand %ENV variable using a sample program.
04:23 We will switch back to the special dot pl file.
04:26 Type the following code as displayed on the screen.
04:30 Press Ctrl+S to save the file. Switch to the terminal and execute the Perl script.


04:37 Type: perl special dot pl and press Enter.
04:42 We can see the current environment details such as PWD (present working directory), username, language etc.
04:51 Next, we will see about another special variable dollar zero.
04:55 The special variable dollar zero ('$0') contains name of the current Perl program that is being executed.
05:02 This is generally used for logging purpose.
05:05 For example: I have a file named 'First.pl ' within which I am using '$0' variable, as shown here.
05:14 On executing, it will print the filename First dot pl.
05:19 Perl has a built-in function called sort that sorts an array.
05:24 A comparison function will compare its parameters using the numerical comparison operator.
05:30 This operator is represented by lesser than equal to greater than symbols, as shown here.
05:38 Let us see an example for this.
05:40 Open the terminal and type: gedit sort.pl ampersand and press Enter.
05:47 sort.pl file is now open in 'gedit' Text Editor. Type the following code as displayed on the screen.
05:56 Let me explain the code. The first line declares an array of numbers.
06:02 The numerical comparison operator will compare the two values as numbers.
06:08' Dollar a and dollar b are special package local variables in which the values to be compared are loaded.
06:16 And, this sort function will sort the numbers in ascending order.
06:21 Let us now save and execute the program.
06:25 Switch back to the terminal and type: perl sort.pl and press Enter.
06:31 We can see that the numbers are sorted in ascending order.
06:35 Let’s see another special variable dollar exclamation.
06:39 'dollar exclamation if used in string context, returns the system error string.

Here is an example of its usage.

06:48 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".

06:59 Let’s now look at another special variable namely, dollar at the rate.
07:04 This is another widely used variable. It returns an error message, returned from eval or require command.
07:12 This example will print: "could not divide Illegal division by zero".
07:17 dollar dollar is yet another special variable. This holds the process ID of the Perl interpreter, running this script.
07:26 The diamond operator is used to read every line from the files specified on the command line.
07:32 Let us see an example for this.
07:35 Open the terminal and type: gedit commandline.pl ampersand and press Enter.
07:42 'commandline.pl' file is now open in gedit.
07:46 Type the code as displayed on the screen.
07:49 Save the file.
07:51 Let me show you the text that I have in a file named sample dot txt file.
07:56 Now, run the program from the command line by typing: perl commandline dot pl space sample dot txt and press Enter.
08:07 This is the text we had in sample dot txt file.
08:11 If no files are specified, it reads from the standard input i.e. from the keyboard.
08:17 Perl has an array at the rate capital A R G V special variable.

This holds all the values from the command line.

08:27 When using array at the rate capital A R G V, there is no need to declare the variables.
08:33 The values from the command line are automatically placed in this variable.
08:37 Let’s now move on to Global Special Constants.
08:41 underscore underscore E N D (all in capital )underscore underscore indicates the logical end of the program.
08:50 Any text following this special variable is ignored after this statement.
08:55 underscore underscore FILE (in capital letters) underscore underscore represents the filename of the program at the point where it is used.
09:06 underscore underscore LINE (in capital letters) underscore underscore represents the current line number.
09:13 underscore underscore PACKAGE (in capital letters) underscore underscore represents the current package name at compile time or undefined if there is no current package.
09:25 We will see a sample program on how Global Special Constants are used.
09:30 Open the terminal and type: gedit specialconstant dot pl ampersand and press Enter.
09:39 specialconstant dot pl file is now open in gedit.
09:44 Type the following code as displayed on the screen. Let me explain the code now.
09:50 The special literals PACKAGE, FILE, LINE represent the package name, current filename and line number respectively, at that point in the program.
10:00 Let us execute the program.
10:02 Switch back to the terminal and type: perl specialconstant.pl and press Enter.
10:09 We can see the current package name, filename and line number of our program.
10:15 This brings us to the end of this tutorial. Let us summarize.
10:19 In this tutorial, we learnt about some commonly used special variables in Perl.
10:25 As an assignment do the following. Write a Perl script to sort the following array of numbers in ascending and descending order.
10:34 Note: For descending order, use the below code for comparison.
10:39 Print the sorted result using while loop and special variable $_ (Dollar Underscore).
10:45 Save and execute the program.
10:47 Now check the result.
10:49 The video at the following link summarizes the Spoken Tutorial project. Please download and watch it.
10:56 The Spoken Tutorial project team

conducts workshops and gives certificates on passing online tests.

11:03 For more details, please write to us.
11:06 Spoken Tutorial project is funded by NMEICT, MHRD, Government of India.
11:13 More information on this mission is available at this link.
11:17 This is Nirmala Venkat from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya, Sandhya.np14