简体   繁体   English

如何在 perl5 的格式语句中使用子程序参数?

[英]How do I use subroutine arguments in a format statement in perl5?

I want to be able to do something like:我希望能够执行以下操作:

sub prline {
        format STDOUT =
@<<<< @>>>>>> @>>>>>>
@_
.
write;
}

prline("EPIC", "BUY", "SELL");

but it doesn't work.但它不起作用。 What's the fix?有什么解决办法?

Update 1:更新 1:

I find the format statement still to be completely mystifying.我发现格式声明仍然完全令人困惑。 How comes the following outputs a blank line?以下如何输出一个空行?

my ( $epic, $buy, $sell ) = qw( EPIC BUY SELL );
write();

format STDOUT =
@<<<< @>>>>>> @>>>>>>
$epic, $buy, $sell
.

You define the format (as you have done), fill in package variables, then use write to make it happen.您定义格式(正如您所做的那样),填写包变量,然后使用write来实现。 I have something close to what you want in an article I wrote for The Effective Perler .我为 The Effective Perler 写的一篇文章中,我有一些接近你想要的东西。 You still have to use package vars, but you can localize them:你仍然需要使用包变量,但你可以本地化它们:

foreach my $record ( @cats ) {
    local( $id, $name, $food ) = @$record;
    write( $fh );
    }

Learning Perl used to have a whole chapter on formats during the Perl 4 days, and I've reproduced that in Formats on the book's website.在 Perl 4 天期间,学习 Perl过去有一整章关于格式,我已经在本书网站上的格式中复制了它。

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

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