简体   繁体   English

我在Perl中的散列内声明散列有什么问题?

[英]What is wrong with my declaration of a hash inside a hash in Perl?

I am struggling with the following declaration of a hash in Perl: 我在Perl中为哈希的以下声明而苦苦挣扎:

my %xmlStructure = {
            hostname    =>  $dbHost,
            username    =>  $dbUsername,
            password    =>  $dbPassword,
            dev_table   =>  $dbTable,
            octopus     =>  {
                                alert_dir       =>  $alert_dir,
                                broadcast_id    =>  $broadcast_id,
                                system_id       =>  $system_id,
                                subkey          =>  $subkey
                            }
 };

I've been googling, but I haven't been able to come up with a solution, and every modification I make ends up in another warning or in results that I do not want. 我一直在使用Google搜索,但无法提出解决方案,而我所做的每一次修改都会导致另一个警告或我不想要的结果。

Perl complaints with the following text: Perl的投诉内容如下:

Reference found where even-sized list expected at ./configurator.pl line X.

I am doing it that way, since I want to use the module: 我这样做,因为我想使用模块:

XML::Simple

In order to generate a XML file with the following structure: 为了生成具有以下结构的XML文件:

 <settings>
  <username></username>
  <password></password>
  <database></database>
  <hostname></hostname>

  <dev_table></dev_table>

  <octopus>
      <alert_dir></alert_dir>
      <broadcast_id></broadcast_id>
      <subkey></subkey>
  </octopus>
 </settings>

so sometthing like: 像这样:

my $data = $xmlFile->XMLout(%xmlStructure);
warn Dumper($data);

would display the latter xml sample structure. 将显示后一个xml示例结构。

Update: 更新:

I forgot to mention that I also tried using parenthesis instead of curly braces for the hash reference, and eventhough it seems to work, the XML file is not written properly: I end up with the following structure: 我忘了提一下,我也尝试使用括号而不是大括号作为哈希引用,尽管它似乎可以正常工作,但XML文件编写得不正确:我最终得到以下结构:

<settings>

 <dev_table>5L3IQWmNOw==</dev_table>
 <hostname>gQMgO3/hvMjc</hostname>

 <octopus>
  <alert_dir>l</alert_dir>
  <broadcast_id>l</broadcast_id>
  <subkey>l</subkey>
  <system_id>l</system_id>
 </octopus>

 <password>dZJomteHXg==</password>
 <username>sjfPIQ==</username>

</settings>

Which is not exactly wrong, but I'm not sure if I'm going to have problems latter on as the XML file grows bigger. 这不是完全错误,但是我不确定随着XML文件的增大,以后是否会出现问题。 The credentials are encrypted using RC4 algorith, but I am encoding in base 64 to avoid any misbehavior with special characters. 凭据是使用RC4算法加密的,但是我正在使用base 64进行编码,以避免使用特殊字符的任何不当行为。 Thanks 谢谢

{} are used for hash references. {}用于哈希引用。 To declare a hash use normal parentheses () : 要声明哈希,请使用普通括号()

my %xmlStructure = (
            hostname    =>  $dbHost,
            username    =>  $dbUsername,
            password    =>  $dbPassword,
            dev_table   =>  $dbTable,
            octopus     =>  {
                                alert_dir       =>  $alert_dir,
                                broadcast_id    =>  $broadcast_id,
                                system_id       =>  $system_id,
                                subkey          =>  $subkey
                            }
 );

See also perldoc perldsc - Perl Data Structures Cookbook . 另请参见perldoc perldsc-Perl数据结构手册

You're using the curly braces { ... } to construct a reference to an anonymous hash. 您正在使用花括号{ ... }来构造对匿名哈希的引用。 You should either assign that to a scalar, or change the { ... } to standard parentheses ( ... ) . 您应该将其分配给标量,或者将{ ... }更改为标准括号( ... )

For your second issue, you should keep in mind that XML::Simple is indeed too simple for most applications. 对于第二个问题,您应该记住XML :: Simple对于大多数应用程序确实太简单了。 If you need a specific layout, you're better off with a different way of producing the XML, say, using HTML::Template . 如果需要特定的布局,最好使用另一种生成XML的方法,例如使用HTML :: Template For example (I quoted variable names for illustrative purposes): 例如(出于说明目的,我引用了变量名):

#!/usr/bin/env perl

use strict; use warnings;
use HTML::Template;

my $tmpl = HTML::Template->new(filehandle => \*DATA);
$tmpl->param(
    hostname    =>  '$dbHost',
    username    =>  '$dbUsername',
    password    =>  '$dbPassword',
    dev_table   =>  '$dbTable',
    octopus     =>  [
        {
            alert_dir       =>  '$alert_dir',
            broadcast_id    =>  '$broadcast_id',
            system_id       =>  '$system_id',
            subkey          =>  '$subkey',
        }
    ]
);

print $tmpl->output;

__DATA__
<settings>
  <username><TMPL_VAR username></username>
  <password><TMPL_VAR password></password>
  <database><TMPL_VAR database></database>
  <hostname><TMPL_VAR hostname></hostname>

  <dev_table><TMPL_VAR dev_table></dev_table>

  <octopus><TMPL_LOOP octopus>
    <alert_dir><TMPL_VAR alert_dir></alert_dir>
    <broadcast_id><TMPL_VAR broadcast_id></broadcast_id>
    <subkey><TMPL_VAR subkey></subkey>
    <system_id><TMPL_VAR system_id></system_id>
  </TMPL_LOOP></octopus>
</settings>

Output: 输出:

<settings>
  <username>$dbUsername</username>
  <password>$dbPassword</password>
  <database></database>
  <hostname>$dbHost</hostname>

  <dev_table>$dbTable</dev_table>

  <octopus>
    <alert_dir>$alert_dir</alert_dir>
    <broadcast_id>$broadcast_id</broadcast_id>
    <subkey>$subkey</subkey>
    <system_id>$system_id</system_id>
  </octopus>
</settings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM