理解perl中的子程序

时间:2017-09-12 05:31:05

标签: perl subroutine

sub bat
{
   my ($abc) = @_;
   my @gCol ;
   {
      my $rec = {};
      $rec->{name}        = "BATID";
      $rec->{type}        = "VARCHAR2";
      $rec->{length}      = "14";
      $rec->{scale}       = "0";
      $rec->{label}       = "Id";
      $rec->{ref_comment} = "Shows bat type";
      $rec->{ref_link} ="$APPL_HOME/bat.cgioptions=Status&options=mfgDate&options=Details&options=RefreshAuto";
      $rec->{ref_col}     = [ ("BAT_ID") ];
      $rec->{nullable}    = "Y";
      $rec->{pk}          = "N";
      $rec->{print}       = undef;
      $rec->{visible}     = "Yes";
      push (@gCol, $rec);
   }
}

任何人都可以解释这个子程序在每一行中做了什么吗?是否使用哈希?什么是我的$ rec = {};?使用push会发生什么?

3 个答案:

答案 0 :(得分:1)

do { my %anon = LIST; \%anon }

基本等同于

LIST

它创建一个哈希,然后将sub bat { my ($abc) = @_; my @gCol; { my %rec; $rec{name} = "BATID"; $rec{type} = "VARCHAR2"; $rec{length} = "14"; $rec{scale} = "0"; $rec{label} = "Id"; $rec{ref_comment} = "Shows bat type"; $rec{ref_link} ="$APPL_HOME/bat.cgioptions=Status&options=mfgDate&options=Details&options=RefreshAuto"; $rec{ref_col} = [ ("BAT_ID") ]; $rec{nullable} = "Y"; $rec{pk} = "N"; $rec{print} = undef; $rec{visible} = "Yes"; push @gCol, \%rec; } } 生成的标量(如果存在)分配给哈希,然后创建对哈希的引用。整体评估该参考。

子可以很容易地编写如下:

SimpleDateFormat formatter = new SimpleDateFormat(" EEE dd-MM-yyyy HH:mm");

答案 1 :(得分:1)

你要求解释每一行发生的事情,我认为你还没有。

sub bat
{
   # Take the first parameter passed to the subroutine and store
   # it in a variable called $abc.
   # This value is then ignored for the rest of the subroutine, so
   # this line of code is pointless.
   my ($abc) = @_;

   # Declare an array variable called @gCol.
   my @gCol ;

   # Start a new block of code.
   {

      # Declare a scalar variable called $rec.
      # Initialise it with a reference to an empty, anonymous hash
      my $rec = {};

      # The next dozen lines are pretty much all the same.
      # Each of them inserts a key/value pair into the $rec hash.
      $rec->{name}        = "BATID";
      $rec->{type}        = "VARCHAR2";
      $rec->{length}      = "14";
      $rec->{scale}       = "0";
      $rec->{label}       = "Id";
      $rec->{ref_comment} = "Shows bat type";
      # This line is slightly interesting as it uses the value
      # of a global variable called $APPL_HOME.
      # Using global variables inside a subroutine is a really
      # bad idea as it limits the portability of the subroutine.
      $rec->{ref_link} ="$APPL_HOME/bat.cgioptions=Status&options=mfgDate&options=Details&options=RefreshAuto";
      # This line is also interesting as instead of setting the
      # value to a scalar value, it sets it to a reference to an
      # anonymouse array.
      $rec->{ref_col}     = [ ("BAT_ID") ];
      $rec->{nullable}    = "Y";
      $rec->{pk}          = "N";
      $rec->{print}       = undef;
      $rec->{visible}     = "Yes";

      # Having set up a hash with twelve key/value pairs, you
      # then push the hash reference onto the (currently empty)
      # array, @gCol.
      push (@gCol, $rec);

      # The next line marks the end of the block of code.
      # Because the $rec variable was declared inside this block,
      # It will now go out of scope and ceases to exist.
      # However, because the reference is still stored in @gCol,
      # the memory will not be recovered, so your hash still exists.
   }

   # This line marks the end of the subroutine. This is also the
   # end of scope for any variables declared within the subroutine.
   # That includes the @gCol array which ceases to exist at this
   # point. Unfortunately, this means that our carefully constructed
   # hash also ceases to exist at this point and all our work
   # is wasted.
}

总而言之,你的代码做了很多工作,但是所有工作都浪费了,因为它使用的变量都随着子例程的结束而被抛弃。所以调用这个子程序的净效果绝对没有。

答案 2 :(得分:0)

$rec= {}这被称为anonymous hash referencenametypelength是键BATIDVARCHAR214是这些键的值。

$rec->{ref_col}此键的值为anonymous array