3

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 => {},
        @_
    );

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?

Reuben
  • 4,136
  • 2
  • 48
  • 57

3 Answers3

11

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.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thanks for the additional explanation of what @_ means in the context of ->. – Reuben Jul 01 '11 at 01:14
  • @Reuben: The [`@_` array](http://perldoc.perl.org/perlvar.html#%40ARG) is the argument list for the function, functions are variadiac in Perl (with exceptions of course) so you get all your parameters in `@_` and then you unpack them by hand. – mu is too short Jul 01 '11 at 03:30
8

Try:

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

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

0

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 = ( ..., @_ );
ysth
  • 96,171
  • 6
  • 121
  • 214