如何在YAML中声明列表并使用PERL YAML :: XS

时间:2019-04-11 20:37:16

标签: perl yaml

我可以这样在YAML中创建一个值:

MYVAL: 1

我可以将其加载到我的PERL中,如下所示:

my $settings = YAML::XS::LoadFile...
my $number_mine = $settings->{'MYVAL'};

我现在想在YAML中创建一个字符串数组。 我尝试使用-和---,但没有看到

YAML?

MYARRAY: str1,str2,str3

PERL:

my @array_mine = $settings->{'MYARRAY'};

2 个答案:

答案 0 :(得分:3)

此:

MYVAL: 1

是一个YAML映射,与您的

相同
MYARRAY

是YAML映射。区别在于,键str1,str2,str3的值是纯(即未加引号)标量字符串MYVAL,而值1的值是标量整数MYARRAY: [str1,str2,str3]

如果要将三个字符串的序列作为一行的值,则需要执行以下操作:

str1

(可选,在逗号之前和/或之后带有空格)。这是三个简单标量的流样式序列:str2str3MYARRAY: - str1 - str2 - str3

一种替代方法是使用块样式:

class MainSliderComponent extends Component<any, any> {
  slider: any;
  constructor(props: any) {
    super(props);
    this.state = {
      slideIndex: 0,
    };
  }
  goToFirst = () => {
    this.slider.slickGoTo(this.state.slideIndex);
  };
  render() {
    let settings = {
      infinite: false,
      centerMode: true,
      centerPadding: '60px',
      slidesToShow: 3,
      responsive: [
        {
          breakpoint: 768,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '40px',
            slidesToShow: 3,
          },
        },
        {
          breakpoint: 480,
          settings: {
            arrows: false,
            centerMode: true,
            centerPadding: '50px',
            slidesToShow: 1,
          },
        },
      ],
    };

    return (
      <div className="container">
        <Opened {...this.props} goToFirst={this.goToFirst} />
        <button onClick={this.goToFirst} value={this.state.slideIndex} />
        <Slider ref={(slider) => (this.slider = slider)} {...settings}>
          {RoomsService.rooms.map((room) => {
            return (
              <div className="rooms_slider">
                <Link to={`/rooms/${room.id}`}>
                  <img src={room.img} />
                </Link>
              </div>
            );
          })}
        </Slider>
      </div>
    );
  }
}

Children.tsx;
class Opened extends Component<any, any> {
  render() {
    return (
      <div className="Opened">
        <button
          className="opened_button"
          onClick={() => {
            this.props.goToFirst();
          }}
        >
          Открыты
        </button>
      </div>
    );
  }
}

在语义上与上面的流样式示例等效。

答案 1 :(得分:2)

转储列表并查看其外观:

$ perl -MYAML -E 'say YAML::Dump( { MYARRAY => ["str1","str2","str3"] })'
---
MYARRAY:
  - str1
  - str2
  - str3