Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8576423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:55:15+00:00 2026-06-11T19:55:15+00:00

I have two modules: test1 and test2 and I need to run their subroutines

  • 0

I have two modules: test1 and test2 and I need to run their subroutines in sequence. They need to modify the same variable $var and the outer script needs to print the final result. This unfortunately doesn’t work, and seems to be very similar to this S.O. Question.

#test1.pm
use strict;
package test1;
my $var;
sub step1 {
  $var = "Hello";
}
1;

#test2.pm
use strict;
package test2;
my $var;
sub step1 {
    $var = $var." World!\n";
}
1;

#execTests.pl
use strict;
require test1;
require test2;
&test1::step1;
&test2::step1;
print $test2::var;

How do I make $var be included in the same scope of both required packages?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:55:16+00:00Added an answer on June 11, 2026 at 7:55 pm

    AFAIK it is not possible to have two variables always share the same value. Here are some Ideas that could help you:

    Package variables:

    {
       package test1;
       our $var;
       sub step1 {$var = "Hello"}
    }
    {
      package test2;
      our $var;
      sub step1 {$var .= " World!\n"}
    }
    
    test1::step1();
    $test2::var = $test1::var;
    test2::step1();
    
    print $test2::var;
    

    Comment: Ugly

    References:

    {
      package test1;
      our $varref;
      sub init {
        my ($ref) = @_;
        $varref = $ref;
      }
      sub step1 {$$varref = "Hello"}
    }
    {
      package test2;
      our $varref;
      sub init {
        my ($ref) = @_;
        $varref = $ref;
      }
      sub step1 {$$varref .= " World!\n"}
    }
    
    my $var = "";
    test1::init(\$var);
    test2::init(\$var);
    
    test1::step1();
    test2::step1();
    
    print $var;
    

    Comment: Better, but requires more code.

    An extra shared module:

    {
      package test::shared;
      our $var;
    }
    {
      package test1;
      sub step1 {$test::shared::var = "Hello"}
    }
    {
      package test2;
      sub step1 {$test::shared::var .= " World!\n"}
    }
    
    test1::step1();
    test2::step2();
    print $test::shared::var;
    

    Comment: Has some advantages.

    Explicit state

    {
      package test1;
      sub step1 {
        my $state = @_;
        $state->{var} = "Hello";
        return $state;  # superfluous, but helps understanding
      }
    }
    {
      package test2;
      sub step1 {
        my $state = @_;
        $state->{var} .= " World!\n";
        return $state;  # superfluous, but helps understanding
      }
    }
    
    my $state = {};
    
    $state = test1::step1($state);  # equiv: test1::step1($state), because of references
    $state = test2::step1($state);
    
    print $state->{var};
    

    Comment: this is almost object oriented programming. I would prefer this pattern, as it is explicit about what shared is given when. Explicit state makes it easy to scale your application, in terms of multithreading and reentrancy.

    Cost: expliciteness. verbose syntax.

    Benefit: Reentrancy, clear train of thought via expliciteness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two different modules that need access to a single file (One will
In my code base I find that two modules have structures with the same
I have two selenium tests: #{selenium 'test1'} deleteAllVisibleCookies() openAndWait('/url1') #{/selenium} #{selenium 'test2'} deleteAllVisibleCookies() //
I got two modules (compile units), both using a module variable with the same
Basically I have written two modules for my Python Program. I need one module
Ok I have two modules, each containing a class, the problem is their classes
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port
I am working on a transliteration tool. I have two modules lexer and translator.
I have these two modules : package G1; sub new { my $class =
I have an application (CC) containing two modules: CC-ejb and CC-war. CC-ejb contains JPA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.