dellnak has asked for the wisdom of the Perl Monks concerning the following question:
and in mystruct I need to get value1 and value2 of this $mystruct. I tried to do this:use Class::Struct; struct ( 'Structname', { value1 => '$', value2 => '$' } ); my $mystruct = new Structname; #here we set value1 and value2 $value = &myfunction($mystruct);
sub myfunction { use strict; use warnings; use Class::Struct; struct ( 'Structname', { value1 => '$', value2 => '$' } ); my $mystruct = new Structname; $mystruct = $ARGV[0]; return $mystruct->value1." ".$mystruct->value2; }
but the error I get is:
function 'new' already defined in package Structname at filename.pl line 3. 3 is the "struct ( 'Structname', ..." line in subroutine file (which is required in main script).
If I remove the line, then the subroutine can't find the value1. I guess I have to tell the subroutine to find the definition of the struct elsewhere instead of redefining it, but I can't seem to understand how to do that.
I tried googling with the error message but didn't find anything helpful.
I am quite new at perl, so there might also be better ways of doing what I'm trying to do. I have one specific struct I'm using in many places and I want to create a outputstring-formatting subroutine for it instead of always rewriting the same code. In reality the struct has more than two values so I'd really like to be able to pass the whole struct as a parameter instead of having to pass each value separately.
Thank you in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Error with using structs as subroutine parameters
by almut (Canon) on Jun 08, 2009 at 08:44 UTC | |
by dellnak (Initiate) on Jun 08, 2009 at 08:53 UTC | |
by almut (Canon) on Jun 08, 2009 at 09:08 UTC | |
|
Re: Error with using structs as subroutine parameters
by cdarke (Prior) on Jun 08, 2009 at 09:03 UTC | |
by dellnak (Initiate) on Jun 08, 2009 at 09:08 UTC | |
|
Re: Error with using structs as subroutine parameters
by jethro (Monsignor) on Jun 08, 2009 at 08:56 UTC | |
by JavaFan (Canon) on Jun 08, 2009 at 10:43 UTC | |
by jethro (Monsignor) on Jun 08, 2009 at 12:45 UTC |