cammac has asked for the wisdom of the Perl Monks concerning the following question:

How do I share a structure between perl modules without having to reference the perl module in which it's defined?

I use struct T_Row in two different modules, but I get an error saying that "function 'new' already defined in package T_Row at CreateXML2.pm line 80" which is where the structure is defined. CreateXML and CreateXML2 both use the T_Row struct and I have "use CreateXML" and "use CreateXML2" in the main program.

example of how it is defined and used in the two CreateXML programs:
struct T_Row => {t_rownum=> '$', t_SEARCH_COL => '$' , t_IMAGE=> '$' } ; my @t_list = qw(t_SEARCH_COL t_IMAGE) ; my %t_hash = ( "SEARCH_COL" , SEARCH_COL, "IMAGE" , IMAGE ); $extractedRow = T_Row->new (t_rownum => $row) ; # begin next row $extractedRow->t_SEARCH_COL ( "$scol" );

Replies are listed 'Best First'.
Re: sharing structures
by dsheroh (Monsignor) on Apr 01, 2008 at 16:36 UTC
    You're defining T_Row in more than one place. There are two possibilities here:

    1) Both versions of T_Row should be identical (and hopefully are). If this is the case, then it should be moved out into a separate module (perhaps named T_Row.pm?) which is used by both CreateXML and CreateXML2. Or it may work better in your case to merge CreateXML and CreateXML2 into a single source file. The important thing (at this point) simply being that T_Row only gets defined in one place, although you may wish to consider what will make it easier if you later need to add CreateXML3, CreateXML4, etc.

    Aside from fixing your current problem, this also has the advantage of ensuring that it will always be the same everywhere it's used, since something only defined in one place can't get out of sync with itself nearly as easily as something defined multiple places.

    2) The two versions of T_Row should be different. In this case, they should have different names to avoid confusion, both for Perl and for the eventual maintenance programmer (who may or may not be you).

Re: sharing structures
by Narveson (Chaplain) on Apr 01, 2008 at 16:36 UTC

    If you want to share something between modules, defining it in one place and referencing that definition elsewhere is the way to go.

    Why not replace your second struct T_Row definition in CreateXML2 with require CreateXML?