开发者

How to pass parameters when getting "Odd number of elements in hash assignment"

I'm doing some template programming in RT (http://bestpractical.com/rt), and it uses Perl. Unfortunately, I've only dallied with Perl very occasionally.

I'm trying to call a sub procedure that starts off with:

sub PrepareEmailUsingTemplate {
    my %args = (
        Template => '',
        Arguments => {},
        @_
   开发者_如何学Go );

Since this is a part of the lib, I don't get to change it.

The call I'm making to it is:

my ($template, $msg) = RT::Interface::Email->PrepareEmailUsingTemplate( 
    Template => 'CCReplyFirstMessage' );
return (0, $msg) unless $template;

And I'm getting "Odd number of elements in hash assignment at /opt/rt4/sbin/../lib/RT/Interface/Email.pm line 552. (/opt/rt4/sbin/../lib/RT/Interface/Email.pm:552), with is the first line of the sub.

I know I'm doing something whacky in passing the parameter. How should I be passing it?


PrepareEmailUsingTemplate is not a class method, it is a simple function. You want to call it like this:

my ($template, $msg) = RT::Interface::Email::PrepareEmailUsingTemplate( 
    Template => 'CCReplyFirstMessage' );
return (0, $msg) unless $template;

When you call it with the ->, your @_ will end up with three values: your two for the hash and the class name at the beginning. The result of calling it as a class method will be something like this:

my %args = (
    Template => '',
    Arguments => {},
    'RT::Interface::Email::PrepareEmailUsingTemplate',
    Template => 'CCReplyFirstMessage'
);

And that's where your "odd number of elements in hash assignment" error comes from.


Try:

my ($template, $msg) = RT::Interface::Email::PrepareEmailUsingTemplate(Template => 'CCReplyFirstMessage');

The function isn't written to be called with ->.


If you are going to call the sub as a class method, you need to expect the additional implicit class argument:

my $class = shift;
my %args = ( ..., @_ );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜