Diamond运算符如何使用数组作为参数 [英] How does diamond operator work with array as argument

查看:66
本文介绍了Diamond运算符如何使用数组作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组 @address ,其零元素包含一些字符串.我找不到使用@array作为参数的示例Diamond运算符.(如何拆分"字符串?)

I have got an array @address whose zero element contains some strings. I can not find an example diamond operator works with @array as argument. (how it 'split' strings?)

use Mojo::Loader qw/ data_section /;

my @address =  data_section 'main', 'address_strings';


while( my $line = <@address> ) {
  print $line;
}


1;


__DATA__
@@ address_strings
"010101, УУУ обл., м. Тернопіль, вул. ВВВ, буд. 0101, 01"
"020202, ЛЛЛ обл., ААА район, село ФФФ, ВУЛИЦЯ ШШШ, будинок 01"
"030303, м.ЮЮЮ, ЮЮЮ р-н, вул. ЛЛЛ, буд.01, офіс 01"

UPD
从doc <> 运算符读取允许使用GLOB或文件句柄,但在我的情况下,这是只是一个字符串的数组.

UPD
Reading from doc <> operator allow GLOB or filehandle, but in my case that is just array of one string.

my @arr = <<TEXT;
many
lines
TEXT

while( my $line =  <@arr> ) {
    print ">>$line<<\n";
}

在这种情况下,<> 做一些魔术吗?您会注意到,行被分割了

Does <> do something magic for this case? You can notice, that lines are splitted

推荐答案

这不是...钻石运算符所做的,这就是为什么您找不到任何示例的原因.

That's ... not what the diamond operator does, which is why you can't find any examples.

您只需要:

foreach my $line ( @address ) { 
  print $line;
}

<> 表示从文件句柄读取,而 @address 不是文件句柄.它甚至不是一个文件句柄数组.

The <> denotes reading from a file handle, and @address is not a file handle. It isn't even an array of file handles.

理论上您可以做到

while ( my $line = <DATA> ) { 
   print $line; 
}

因为 DATA 是文件句柄,所以<> 触发从该文件句柄读取的一条记录.(记录边界默认为换行,但是您可以更改 $/)

Which because DATA is a file handle, <> triggers a single record read from that filehandle. (record boundary defaults to linefeed, but you can change $/)

鉴于您对Mojo所做的事情,这没有任何意义,我仅以它为例,说明它如何工作.

This wouldn't make sense given what you're doing with Mojo, I merely give it as an example of how it should work.

这篇关于Diamond运算符如何使用数组作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆