Mansoor Siddiqui
Mansoor Siddiqui

Reputation: 21673

In Perl, how do you create a reference to the output of a subroutine without using an intermediate variable?

Suppose I have a list called @emailList, and I would like to pass a reference to that list to a subroutine called sendEmail. I know I can do it this way:

my @emailList = split(/[$EMAIL_DELIMS]+/, $emailListStr);
sendEmail(\@emailList);

But if I want to create a reference to the output of split directly without using the intermediate variable @emailList, what's the correct syntax? I have already tried:

sendEmail(\@{split(/[$EMAIL_DELIMS]+/, $emailListStr)});

… as well as many subtle variations of this, but perl always complains. Suggestions?

Upvotes: 1

Views: 134

Answers (1)

Sinan Ünür
Sinan Ünür

Reputation: 118148

sendEmail([ split(/[$EMAIL_DELIMS]+/, $emailListStr) ]);

will create an anonymous array populated using the list returned by split and pass it to sendEmail.

Also, you might want to use Email::Address->parse.

Upvotes: 9

Related Questions