2017-09-12 12 views
-5
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 = {};は何ですか?プッシュを使って何が起こるのですか?perlのサブルーチンを理解する

+0

https://perldoc.perl.org/perlreftut.html&https://perldoc.perl.org/functions/push.html –

+0

しかし、このサブは私にはほとんど役に立たないようです。 $ recはブロックの終わりにスコープから外れ、subはpush()によって返された値を返します。 – ulix

+0

@ulix: '$ rec'がサブルーチンの終了時にスコープから外れるという事実は問題ではありません。その参照は' @gCol'に格納されています。問題は '@ gCol'もスコープの外に出てくるのと全く同じです:-) –

答えて

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. 
} 

、あなたのコードは、多くの作業を行いますが、それが使用する変数は、すべてのサブルーチンが終了すると捨てられるように、すべてのその仕事が無駄になります。したがって、このサブルーチンを呼び出す正味の効果は全くありません。

+0

サブルーチンが変数の範囲を制限している場合、サブルーチンを使用する利点は何ですか? –

+0

変数の範囲を制限するサブルーチンを使用するのではなく、サブルーチン内では 'my'を使用します。しかしそれは良い考えです。サブルーチンはサブルーチン外の変数を使用しないでください。サブルーチンが必要とするすべてのデータはパラメータとして渡され、サブルーチンの外部で共有される必要があるデータは 'return()'ステートメントによって返されます。 –

0

$rec= {}anonymous hash referenceと呼ばれます。そして、name,type,lengthは、キーです。BATIDVARCHAR214はこれらのキーの値です。

$rec->{ref_col}このキーの値はanonymous arrayです。

1
{ LIST } 

は、ハッシュにLISTによって生成スカラー(存在する場合)を割り当て、その後、ハッシュへの参照を作成し、

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

それはハッシュを作成する基本的に同等です。全体がその参照に評価されます。


次のようにサブが同じように簡単に書かれている可能性が:

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; 
    } 
} 
+0

プッシュ@gcol、\%rec行が使用されていますか? –

+0

'push'は1つまたは複数の要素を配列に追加します。私はあなたの[研究]の不足(http://perldoc.perl.org/functions/push.html)に腹を立てました。 – ikegami

-1

サブルーチンでは3行目で、あなたは、中括弧で示されるブロックを持っています。ブロックには独自のスコープがあります。 @gCol配列は、ブロックの同じスコープ内で宣言されています。それから私の$ rec = {};連想配列(別名ハッシュ)を宣言し、次にそのハッシュのキー値ペアの定義を示します。 @gColは、$ recで%recのスカラー値に追加(プッシュ)されます。

More on hashes in scalar context:

関連する問題