Hello! My problem is that I want to use a struct as a parameter of a subroutine like this:
use Class::Struct; struct ( 'Structname', { value1 => '$', value2 => '$' } ); my $mystruct = new Structname; #here we set value1 and value2 $value = &myfunction($mystruct);
and in mystruct I need to get value1 and value2 of this $mystruct. I tried to do this:
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.