8/30/2014

02-perl Variables and Values



Variables represents the storage space in memmory.In perl variable are represented by beginning with a ‘$’ sign followed by variable name.for example $varible_name. values are assigned to this variables.

1
 $variables_name = "Hello World"


we can store any kind of data type in varibles including null.Each statements in perl terminated by putting semicolon ‘;‘ at the end. Each subroutines in perl ,other languages considered as method starts with a curlybraces “{}” and ends with curly braces.
lets walk though a n example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
      $variables_name = "Hello World"
      print "$variables_name\n"
}


In this program its starts with two imports of libraries from perl strict and warnings. strict is used for forcing perl to not alllow any bare words,un referenced declaration.Warnings helps you to find out errors on run time for example if you missed any semicolon or typo mistake in keyword declaration etc.

next is the main function main(@ARGV) as like in c and java.We can pass command line argument through main(@ARGV) here @ARGV is array type.we will look more details in array post.For now we can look how command line argument can be done.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/perl
 
use strict;
use warnings;
 
main(@ARGV);
 
sub main
{
    foreach my $arg (@ARGV)
    {
      print("$arg\n");
    }  
}


In this program we are iterating each element in the array @AGRV using foreach keyword in side the sub routine and each element are printed by using instance variable $arg.To run command line argument,copy the program and save in a file as some_name.pl.Type the perl command followed by argument perl same_name.pl Hi how are you.

Lets talk about variable scope. variable scope allows us to limit the scope of the variable in several places in side our program.There are global variables and method depended variables.Method depended variables scope are limited to the method they are not visible outside the method.Unlike method variables global variables scope has no limit.They are scoped every where.For example @AGRV is a global variable thats why they are available inside the method,but method variable which is declared by my key word means this is limited to this method only.For example my $arg this variable cannot be access out side the method.If you want make sure thats you self clear just run the program by printing $arg value outside the method.It will gives you a error

Global symbol “$arg” requires explicit package name at sample.pl

Then what about global variable, lets walk though an example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#!/usr/bin/perl
 
use strict;
use warnings;
my $arg = "This is a global scope test";
main(@ARGV);
 
sub main
{
   my $arg = "method variable scope";
   print("$arg\n");
}
print("$arg\n");


in above example there is $arg variable is declared in global and method level scope,so this time the print after the sub routine will not produce any error because there is a global variable for that call.if we run this program this will result with printing

op:

method variable scope
This is a global test


next what happens if there is no variable declared in side the subroutine for example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/perl
 
use strict;
use warnings;
my $arg = "This is a global scope test";
main(@ARGV);
 
sub main
{
  print("$arg\n");
}
print("$arg\n");


if there is no matching variable for the print call it will look for the global variable.This will result in printing

op:

This is a global scope test
This is a global scope test


No comments:

Post a Comment

installing perl dancer in ubuntu

For installing perl dancer you need curl utility for installing curl ,   sudo apt-get install curl Now we can install Perl-Dancer by thi...