Wednesday, August 1, 2007

Function (Part 6)-- Perl Study Notes

Function (Part 6)-- Perl Study Notes

 Arguments
  @_ is a array of all arguments passed to the subroutines
  $_[0],$_[1],$_[2]is the first/second/third/... arguments passed to the subroutines
 
 Define local variable
  my ($localScalar); #define one local scalar variable localScalar
  my (@localArray);  #define one local array variable localArray
  my (%localHash); #define one local hash variable localHash
  my ($localScalar,@localArray,%localHash); #define 3 local variables,localScalar, localArray and localHash
  my ($localScalar,@localArray) = ("Scalar",qw(list1 list2 list3)); # define the local variable and initial the variable

 
  Notes, operator local can be used to define the local variable. But we should prefer to use my over local because it is faster and safe

  local $_; # can not write like my $_;
  local $1; # can write like my $1;
  local @ARGV; # can write like my @ARGV;
 
  Perl pragma
   use strict; # write this in the top of the code file can keep from using the variable without declaration
   The advantages of forcing variable declarations are as below
   1. Will run slightly faster.(variables created with my are accessed slightly faster than ordinary variables)
   2. Can keep from variables typing error

No comments: