1) my:
- Using this you can declare any variable which is specific within the block. i.e. within the curly braces.
- Since the scope of local variables are limited to the block, you can use same name local variables in different blocks without any conflicts.
- The variable declared using my keyword, at the beginning of the programs right after pragmas would be accessible throughout the program.
- Lexical variables are created using 'my' keyword. They are private variables.
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/perl use strict; use warnings; my $age =29; if (1<2){ print "Age = $age\n"; } print "Age = $age\n"; |
2) local:
- Using local we can mask the same variable values to different values without actually changing the original value of the variable.
- suppose we have a variable $var for which the value is assigned 5, you can actually change the value of that variable by re-declaring the same variable using local keyword without altering the original value of the variable which is 5.
1 2 3 4 5 6 7 8 9 10 | #!/usr/bin/perl $var = 5; { local $var = 3; print "local,\$var = $var \n"; } print "global,\$var = $var \n"; |
Output:
local,$var = 3
global,$var = 5
local,$var = 3
global,$var = 5
This comment has been removed by a blog administrator.
ReplyDelete