2017-09-22 5 views
1

だから私の質問は、このSO質問のオフに基づいています。 How to pass a subroutine as a parameter to another subroutine、またパラメータであるサブルーチンにパラメータを渡す

質問です:私もある(サブルーチンへの引数/パラメータを渡すことができますそれ自身のパラメータ)?

sub question { 
print "question the term"; 
return 1; 
} 

my $question_subref = \&question; 
answer($question_subref); 

sub answer { 
    my $question_subref = shift; 
    print "subroutine question is used as parameters"; 
    # call it using arrow operator if needed 
    $question_subref -> ($PassSomethingHere,$SomethingElse); 
    return 1; 
} 

これは可能ですか?

$ question_subref - >($ PassSomethingHere、$ SomethingElse);

相続人

実際のコード:

my $SelectResults = sub { 
      my @results; 
      $sql = $_[0]; 
      $sth = $_[1]; 
      $sql =~ s/select/SELECT/gi; 
      if(StrContains($sql, "SELECT")) 
      { 
       @results= $sth->fetchrow_array(); 
       foreach my $tab (@results) { 
        print $tab . "\n"; 
       } 
      } 
      return @results; 
     }; 



sub MySQLExe 
{ 
    my @results; 

    my $db = "fake"; 
    my $usr = "user"; 
    my $pw = "password"; 

    $db_handle = DBI->connect("dbi:mysql:database=$db;mysql_socket=/var/lib/mysql/mysql.sock;", $usr, $pw) \ 
     or die "Connection Error: $DBI::errstr \n"; 
    my $sql = $_[0]; 
    print $sql . "\n"; 



    #Prepare SQL query 
    my $sth = $db_handle->prepare($sql) 
     or die "Couldn't prepare query '$sql': $DBI::errstr\n"; 



    $sth->execute 
     or die "Couldn't execute query '$sql': $DBI::errstr\n"; 


    # I can't seem to get this to work... 
    # optional Function here - get rows from select statement or something else. 
    # pass in the function holder as the second parameter 
    my $Func = $_[1]; 
    @results = $Func -> ($sql, $sth); 


    #disconnect from database 
    $sth->finish; 
    $db_handle->disconnect or warn "Disconnection error: $DBI::errstr \n"; 


    return(@results); 
} 

と実際の使用:

my @tables = MySQLExe("SELECT table_name FROM information_schema.tables where table_schema='$table';", 
    $SelectResults); 
+0

実際のアプリケーションは** dbi-> mysqlサブルーチンに**変更するだけです**。私はすべての接続の開閉のものが常に同じであり、決して変わらないようにしたい。しかし、私は私のSQLステートメントと結果の配列(時には)を返す関数が必要です。アプリケーションがアプリケーションに応じて異なる機能を果たすことができれば便利ですが、その周りに道を見つけることはできません。 – m1m1k

+1

あなたはどんな問題を抱えていますか? – ikegami

+2

注射バグ!!! '$ table'schema = '$ table';' '' 'SELECT ... WHERE table_schema ="。$ dbh-> quote($ table) ' – ikegami

答えて

5

リンクの質問への答えでほのめかしたとして、あなたはおそらくしているものの後closure(ありますPerl.com articleWikipedia entryも参照):

sub make_questioner { 
    my ($text) = @_; 
    return sub { 
    my ($politeness) = @_; 
    print $text, $politeness, "\n"; 
    my $answer = <>; 
    chomp $answer; 
    $answer; 
    }; 
} 

my $questioner = make_questioner("What... is your name"); 

my $name = $questioner->(', please'); 
print "Your name is '$name'.\n"; 

ここでのデモコードには、クロージャの作成時に渡される情報が組み込まれており、クロージャに渡されるパラメータも使用されています。

+0

または_currying _... – simbabque

+0

彼らは実行する前に散歩をしましょうか? –

+0

彼らは何をしようとしているのかまだ分かりません。私が質問のタイトルを読むとすぐに_curry_と思ったが、それは本当に彼らが望むものではないと思う。コードは、このような混乱です、私はちょうどそれを通過しています。クロージャーのためのwikiリンクを含めることもできます。それはPerlのrefcountのものよりも理解しやすいでしょう。 – simbabque

関連する問題