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.
In reply to Error with using structs as subroutine parameters by dellnak
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |