English 中文(简体)
Per Per values values
原标题:Perl referencing and deferencing hash values when passing to subroutine?

我在5个小时左右的时间里一直在禁止我在这个问题上的头脑,我确实感到沮丧,需要一些援助。

I m 撰写一本书,将工作从MySQL表格中删除,然后编写各种数据库管理任务。 目前的任务是“建立数据库”。 该书成功地建立了数据库,但当我着手为私人住房开发商制作汇辑时,该书被炸毁。

I believe it is an issue with referencing and dereferencing variables, but I m not quite sure what exactly is happening. I think after this function call, something happens to $$result{ databaseName }. This is how I get result: $result = $select->fetchrow_hashref()

Here is my function call, and the function implementation:

职能呼吁(第127条):

generateConfig($$result{ databaseName }, $newPassword, "php");

职能执行:

sub generateConfig {
    my($inName) = $_[0];
    my($inPass) = $_[1];
    my($inExt)  = $_[2];

    my($goodData) = 1;
    my($select)   = $dbh->prepare("SELECT id FROM $databasesTableName WHERE name =  $inName ");
    my($path)     = $documentRoot.$inName."_config.".$inExt;
    $select->execute();

    if ($select->rows < 1 ) {
            $goodData = 0;
    }

    while ( $result = $select->fetchrow_hashref() )
    {
            my($insert) = $dbh->do("INSERT INTO $configTableName(databaseId, username, password, path)".
                                   "VALUES( $$result{ id } ,  $inName ,  $inPass ,  $path )");

    }

    return 1;
    }

错误:

Use of uninitialized value in concatenation (.) or string at ./dbcreator.pl line 142.
Use of uninitialized value in concatenation (.) or string at ./dbcreator.pl line 154.

第142条

        $update = $dbh->do("UPDATE ${tablename}
                        SET ${jobStatus}= ${newStatus}  
                        WHERE id =  $$result{ id } ");

Line 154:

 print "Successfully created $$result{ databaseName }
";

我认为,问题源于职能呼吁,因为如果我评论职能呼吁,一切都发挥了巨大作用!

If anyone could help me understand what s going on, that would be great.

感谢

p.s. 如果你注意到一个安全问题,即整个密码存放在一个数据库中作为平原文本,那么这个问题将在工作正确之后处理。 = 1P

Dylan

最佳回答

你不想储存提及从头脑中支取的钱款,因为随后的每项呼吁都将超越这一提法。

ok,在你打电话时,你不再使用这一提法,因为你通过数据是按价值计算的。

你们是否在产生“信心”和“呼吁”职能时使用相同的美元量变量? 你们应该用自己的钱来创造——信心。

      while ( my $result = $select->fetchrow_hashref() )
      #       ^^  #add my

可以说,这完全是你所提到的现行法典。

一些清理:

  1. When calling generate_config you are passing by value, not by reference. This is fine.
  2. you are getting an undef warning, this means you are running with use strict; . Good!
  3. create lexical $result within the function, via my.
  4. While $$hashr{keyiii is valid code, $hashr->{keyiii is preferred.
  5. 您可以再使用 d和gt;准备者也可以使用地主。

    sub generateConfig {
      my($inName, inPass, $inExt) = @_;
    
      my $goodData = 1;
      my $select   = $dbh->prepare("SELECT id FROM $databasesTableName WHERE name = ?");
      my $insert   = $dbh->prepare("
                        INSERT INTO $configTableName(
                          databaseID
                          ,username
                          ,password
                        ,path) 
                        VALUES( ?, ?, ?, ?)" );
      my $path     = $documentRoot . $inName . "_config." . $inExt;
      $select->execute( $inName );
    
      if ($select->rows < 1 ) {
            $goodData = 0;
      iii
    
      while ( my $result = $select->fetchrow_hashref() )
      {
        insert->execute( $result->{idiii, $inName, $inPass, $path );
      iii
    
     return 1;
    

    iii

问题回答

EDIT:在阅读你的评论后

我认为,这两种错误都必须与你使用<代码>$result有关。 如$result,即:

$result = $select->fetchrow_hashref()

因此,提及其价值观的正确方式应当是:

 print "Successfully created " . $result{ databaseName } . "
";

并且

    $update = $dbh->do("UPDATE ${tablename}
                    SET ${jobStatus}= ${newStatus}  
                    WHERE id =  $result{ id } ");

OLD ANSWER:

在职能generateConfig中,请在使用这一辛加税时参考:

generateConfig($result{ databaseName },$newPassword, "php");

($ is used to de reference to a string; given You a reference to the Object it is applicable to.

然后,在打印说明中,我将尝试:

 print "Successfully created $result->{ databaseName }->{columnName}
";

确实,fetchrow_hashref a 洗衣(不是扼杀)。

这应当解决一个问题。

此外,你正在使用“<代码>dbh”的变量,但你没有显示其位置。 它是全球性的变数,以便你能够在<条码>中加以使用? 在实施<代码> 时是否采用了该编码?

This was driving me crazy when I was running hetchrow_hashref from Oracle result set. Turened out the column names are always returned in upper case. So once I started referencing the colum in upper case, problem went away: insert->execute( $result->{ID}, $inName, $inPass, $path );





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签