Background

Three test perl scripts:
tryStuffAlpha.pm : a small test "hello-world" module that is part of a pair.
tryStuffBeta.pm : a small test "hello-world" module that is part of a pair.
tryStuff_Caller.pl : a script that uses two the test modules.
(NOTE: Alpha and Beta are essentially identical modules, just 'mirror images' of each other with different names.)
Goals:
The Alpha and Beta modules need the capability to share one or more scalar variables with each other.
The Alpha and Beta modules need the capability to share subroutines with each other.
The Caller script needs the capability to access all the common and combined functionality of the Alpha and Beta modules.
All three scripts are supposed to demonstrate the bare-minimum amount of code necessary to reach the desired goals in perl.

Original Source Code

The source code of the three scripts is as follows.

tryStuffAlpha

### begin_: tryStuffAlpha.pm package tryStuffAlpha; use strict; use warnings; use Exporter; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use vars qw( $PermaLink $OppositePermaLink ); $VERSION = 1.00; # Or higher @ISA = qw(Exporter); use tryStuffBeta; ### begin_: init vars $PermaLink = "PERMALINK-ALPHA\n"; $OppositePermaLink = "AOppositeLink=" .$tryStuffBeta::Permalink." +\n"; ### begin_: subroutines push @EXPORT_OK, qw( SayHello ); sub SayHello { my $strOut; $strOut = "Alpha Says Hello World! \n"; return $strOut; }###end_sub push @EXPORT_OK, qw( SayOpposite ); sub SayOpposite { my $strOut; $strOut = "Say Opposite Alpha =".tryStuffBeta::SayHello(); return $strOut; }###end_sub ### begin_: end perl 1; __END__

tryStuffBeta

### begin_: tryStuffBeta.pm package tryStuffBeta; use strict; use warnings; use Exporter; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use vars qw( $PermaLink $OppositePermaLink ); $VERSION = 1.00; # Or higher @ISA = qw(Exporter); use tryStuffAlpha; ### begin_: init vars $PermaLink = "PERMALINK-BETA\n"; $OppositePermaLink = "BOppositeLink=" .$tryStuffAlpha::Permalink. +"\n"; ### begin_: subroutines push @EXPORT_OK, qw( SayHello ); sub SayHello { my $strOut; $strOut = "Beta Says Hello World! \n"; return $strOut; }###end_sub push @EXPORT_OK, qw( SayOpposite ); sub SayOpposite { my $strOut; $strOut = "Say Opposite Beta =".tryStuffAlpha::SayHello(); return $strOut; }###end_sub ### begin_: end perl 1; __END__

tryStuff_Caller

### begin_: tryStuff_Caller.pl use strict; use warnings; use tryStuffAlpha; ### <a href="./tryStuffAlpha.pm" /> use tryStuffBeta; ### <a href="./tryStuffBeta.pm" /> ### begin_: main print "1: ". tryStuffAlpha::SayHello(); ### OK print "2: ". tryStuffBeta::SayHello(); ### OK print "3: ". tryStuffAlpha::SayOpposite(); ### OK print "4: ". tryStuffBeta::SayOpposite(); ### OK print "5: ". $tryStuffAlpha::PermaLink; ### OK print "6: ". $tryStuffBeta::PermaLink; ### OK print "7: ". $tryStuffAlpha::OppositePermaLink; ### FAILS print "8: ". $tryStuffBeta::OppositePermaLink; ### FAILS

Problem

The three scripts are not behaving as expected and/or desired:
Run Result Alpha : (unexpected1) warns about uninitialized value, apparently b/c it does not "see" the scalar from the other module.
Run Result Alpha : (unexpected2) warns about redefined subroutines, not sure why.
Run Result Beta : (unexpected3) same problems as Alpha.
Run Result Caller : (expected but not wanted) warns about uninitialized value, apparently caused by unexpected1.
Run Result Caller : (wanted but not explained) successfully outputs the result of all subroutines as expected, even though there is the 'redefined' warning when Alpha and Beta are run individually.
### Run Result Alpha (tryStuffAlpha.pm run all by itself) Use of uninitialized value ... at tryStuffAlpha.pm line 29. Use of uninitialized value ... at tryStuffBeta.pm line 29. Subroutine SayHello redefined ... tryStuffAlpha.pm line 35. Subroutine SayOpposite redefined ... tryStuffAlpha.pm line 42. Use of uninitialized value ... tryStuffAlpha.pm line 29. ### Run Result Beta (tryStuffBeta.pm run all by itself) Use of uninitialized value ... at tryStuffBeta.pm line 29. Use of uninitialized value ... at tryStuffAlpha.pm line 29. Subroutine SayHello redefined ... tryStuffBeta.pm line 35. Subroutine SayOpposite redefined ... tryStuffBeta.pm line 42. Use of uninitialized value ... tryStuffBeta.pm line 29. ### Run Result Caller (tryStuff_Caller.pm run all by itself) Use of uninitialized value in concatenation (.) or string at tryStuffB +eta.pm line 29. Use of uninitialized value in concatenation (.) or string at tryStuffA +lpha.pm line 29. 1: Alpha Says Hello World! 2: Beta Says Hello World! 3: Say Opposite Alpha =Beta Says Hello World! 4: Say Opposite Beta =Alpha Says Hello World! 5: PERMALINK-ALPHA 6: PERMALINK-BETA 7: AOppositeLink= 8: BOppositeLink=

Questions

The main question is what is the proper syntax to get the two modules Alpha and Beta to share their scalar variable with each other? What is the reason for the subroutine 'redefined' warnings? What is the way to make all the 'redefined' warnings go away when the modules are run separately? This is apparently a very simple problem of variable scope, any clarification on this issue is welcome.

=oQDlNWYsBHI5JXZ2VGIulGIlJXYgQkUPxEIlhGdgY2bgMXZ5VGIlhGV

In reply to Sharing variables and subroutines between modules: unexpected warnings and errors. by dimar

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.