LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2012, 12:07 PM   #1
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Rep: Reputation: 61
odd number of elements in hash


I'm trying to debug some perl code written by someone else. It has a line:
Code:
Daemon::Generic->newdaemon(
   progname    => 'mycach',
   configfile  => '/ekb/mycach.conf',
   pidfile     => '/var/run/mycach.pid'
);
When run, I get:
Code:
Odd number of elements in hash assignment at /usr/local/share/perl5/Daemon/Generic.pm line 22.
Line 22 is:
Code:
my (%args) = @_;
Run under debug it shows:
Code:
Daemon::Generic::newdaemon('Daemon::Generic', 'progname', 'mycach', 'configfile', '/ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
which seems to show 'Daemon::Generic' is thrown in there with no element for some reason. The system has Daemon::Generic 0.82 installed. Anyone have some suggestions?

Thanks!
 
Old 11-16-2012, 01:38 PM   #2
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Hi,

it seems that you have to use an even number of arguments to the "constructor" function. Namely pairs of key and value, look here http://perldesignpatterns.com/?NamedArguments
Quote:
my (%args) = @_;
means to use a hash for the parameters.

An explanation of the module is here at cpan http://search.cpan.org/dist/Daemon-G...E_USAGE_OUTPUT

Markus

Last edited by markush; 11-16-2012 at 01:41 PM.
 
Old 11-16-2012, 10:16 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bradvan View Post
I'm trying to debug some perl code written by someone else. It has a line:
Code:
Daemon::Generic->newdaemon(
   progname    => 'mycach',
   configfile  => '/ekb/mycach.conf',
   pidfile     => '/var/run/mycach.pid'
);
When run, I get:
Code:
Odd number of elements in hash assignment at /usr/local/share/perl5/Daemon/Generic.pm line 22.
Line 22 is:
Code:
my (%args) = @_;
Run under debug it shows:
Code:
Daemon::Generic::newdaemon('Daemon::Generic', 'progname', 'mycach', 'configfile', '/ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
which seems to show 'Daemon::Generic' is thrown in there with no element for some reason. The system has Daemon::Generic 0.82 installed. Anyone have some suggestions?

Thanks!
I think you should write

Code:
my %args = @_;
.
 
1 members found this post helpful.
Old 11-17-2012, 03:06 AM   #4
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
I think you should write

Code:
my %args = @_;
.
I was missing this when I wrote my above reply. So the problem here is that the parenthesis put the hash %args into a scalar context which makes no sense for Perl.

Markus
 
Old 11-17-2012, 05:38 AM   #5
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Ah, good catch! That means there must be an error in the CPAN module. Thanks!
 
Old 11-19-2012, 07:59 AM   #6
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Rats, that wasn't the problem. Adding or removing parenthesis doesn't make a difference. The problem seems to be when the call is made:
Code:
Daemon::Generic->newdaemon (
   progname    => 'mycach',
   configfile  => '/ekb/mycach.conf',
   pidfile     => '/var/run/mycach.pid'
);
perl is passing:
Code:
('Daemon::Generic', 'progname', 'mycach', 'configfile', 'ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
and not
Code:
('progname', 'mycach', 'configfile', 'ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
Notice the 'Daemon::Generic' is at the head of the list and thus makes an odd number. Any ideas on why 'Daemon::Generic' is included and how to get rid of it?

Regards,

Brad
 
Old 11-19-2012, 08:21 AM   #7
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
I temporarily got around it by adding in the Daemon::Generic sub newdaemon procedure (before the my (%args) line:
Code:
if (($#_ + 1) % 2) {
   shift(@_);
}
which makes it work. I'd still like to find out why the script is adding the 'Daemon::Generic' to the call if anyone knows.

Regards,

Brad V
 
Old 11-19-2012, 01:01 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by bradvan View Post
Rats, that wasn't the problem. Adding or removing parenthesis doesn't make a difference. The problem seems to be when the call is made:
Code:
Daemon::Generic->newdaemon (
   progname    => 'mycach',
   configfile  => '/ekb/mycach.conf',
   pidfile     => '/var/run/mycach.pid'
);
perl is passing:
Code:
('Daemon::Generic', 'progname', 'mycach', 'configfile', 'ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
and not
Code:
('progname', 'mycach', 'configfile', 'ekb/mycach.conf', 'pidfile', '/var/run/mycach.pid')
Notice the 'Daemon::Generic' is at the head of the list and thus makes an odd number. Any ideas on why 'Daemon::Generic' is included and how to get rid of it?

Regards,

Brad

Perl is doing "the right thing" (tm).

My point is that the "->" notation prescribes this behavior - this is how Perl OO model works. The idiom is:

Code:
sub foo
  {
  my $self = shift;
  # deal with the rest of args in @_
  # the code doing the job is here
  # methods are called as $self->method_name(...)
  }

If you do not want this behavior, you should use this:
Code:
Daemon::Generic::newdaemon(...)
form of call.
 
1 members found this post helpful.
Old 11-20-2012, 04:50 AM   #9
bradvan
Member
 
Registered: Mar 2009
Posts: 367

Original Poster
Rep: Reputation: 61
Ahh, OK, thanks for the education! I'll make the change.

Thanks Again!
 
  


Reply

Tags
perl



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell; number of elements in array Wim Sturkenboom Programming 7 07-10-2012 11:39 PM
How do I get the number of elements in a char*[] in c++? chinho Programming 7 01-28-2011 01:30 AM
How do I get number of elements in a hash of arrays? RavenLX Programming 2 12-10-2008 12:44 PM
odd behaviour of array elements in c++ markhod Programming 4 03-14-2005 09:58 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration