Share variable between subroutines without global variable names collisions, declaration out-of-subroutine, or passing parameters

Omar

How could I share a variable declared in one subroutine to another subroutine, that's called inside it, without having global variable names collisions, or declaring the variable outside the mother caller subroutine, or using any subroutine parameters?

in other words: How could I call a variable defined in a subroutine from a sub-called subroutine?


More explanation: (in case the previous single-line question wasn't descriptive enough)

Please note that you don't need to read to the end to understand my question. Stop reading if you've got what I'm asking about to save your time.

Let's say I have subroutine1 and subroutine2 as so:

subroutine1 ();

sub subroutine1 {
  my $word1='hello';
  subroutine2();
}

sub subroutine2 {
  print $word1; #Prints nothing as subroutine2 is unable to access $word1 declared in subroutine1
}

My question, specifically, asks about how could I share a variable ($word1) declared in one subroutine (subroutine1) to another subroutine (subroutine2), that's called inside it (subroutine2 is called inside subroutine1), ...

  • without having global variable names collision — Which will let me freely name my variables simple names.
  • without declaring the variable outside the mother caller subroutine (subroutine1)
  • without using any subroutine parameters

For example we could simply achieved it using one of these three simple well-known ways:

Way 1: declare $word1 as global.

subroutine1 ();
sub subroutine1 {
  $word1='hello'; #$word1 is declared as global
  subroutine2();
}
sub subroutine2 {
  print $word1;
}

Way 2: declare $word1 outside the mother caller subroutine subroutine1 and limit the scope of it.

{
  my $word1='hello';
  subroutine1 ();
  sub subroutine1 {
    subroutine2();
  }
  sub subroutine2 {
    print $word1;
  }
}

Way 3: pass $word1 as a parameter to subroutine2.

subroutine1 ();

sub subroutine1 {
  my $word1='hello';
  subroutine2($word1);
}
sub subroutine2 {
  my $x = shift;
  print $x;
}

This example is really trivial and can be solved by any simple way including the three above. But building it simple this way and putting those limits in mind (no global variable names collisions, no out-of-subroutine declaration, no parameters usage) will definitely solve my real complicated problem that I couldn't simplify here for now.

ikegami

If the two subroutines aren't nested, then your request is impossible by definition. If it's visible by two non-nested subs, then it must exist outside of the subs. As such, it must be global, or a declaration outside of the subs must limit its visibility to some lexical scope that includes both subs.

So that leaves nested subs.

sub tree_visitor {
   my @rv;

   local *_helper = sub {
       my ($node) = @_;
       push @rv, $node->text;
       for my $child ($node->children) {
          _helper($child);
       }
   };

   _helper($node);
   return @rv;
}

Something closer to what you want is called a "dynamic-scoped variable". It uses a global variable, but the value of the variable is restored after the calling sub exits.

sub subroutine2 {
   our $x;
   print $x;
}

sub subroutine1 {
   local our $x = 'hello';
   subroutine2();
}

subroutine1();

It would be must easier to help you if we knew what problem you are actually facing. But it's either going to involve a global var or a closure.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Passing the value of a global variable inside a subroutine

From Dev

How to share variable between two classes without using global variable in Java?

From Dev

Share global variable between .c file

From Dev

Share global variable between .c file

From Dev

Closure confusion, share global variable between function

From Dev

How to access global variable in subroutine

From Dev

Alternative to a global variable for passing objects between activities

From Dev

Global variable declaration in mvc

From Dev

Share global Unity Container between two assemblies without passing a reference

From Dev

Calling a variable which is a function without passing parameters to it

From Dev

function variable not passing to global variable

From Dev

Passing a global variable to a function

From Java

Passing variable from subroutine in tk perl interface

From Dev

Can I use global variable instead of passing parameters?

From Dev

How C++ differentiates between a call to global variable and declaration of a global variable?

From Dev

How C++ differentiates between a call to global variable and declaration of a global variable?

From Dev

In VB with classic ASP will local variable hides the Global declaration if variable names are similar

From Dev

In VB with classic ASP will local variable hides the Global declaration if variable names are similar

From Dev

How to share a global variable between server and client code in Meteor

From Dev

C++ Global variable declaration

From Dev

Proper Global Variable definition/declaration

From Dev

Passing variable parameters in Java

From Dev

Global variable that is accessible in models without passing any information from controller

From Dev

Share a variable between classes

From Dev

Share a variable between classes

From Dev

Passing a Variable Between Views

From Dev

Passing a reference of a global variable to a function

From Dev

Using global variable for option passing?

From Dev

Variable Replacement in multiple subroutines

Related Related

  1. 1

    Passing the value of a global variable inside a subroutine

  2. 2

    How to share variable between two classes without using global variable in Java?

  3. 3

    Share global variable between .c file

  4. 4

    Share global variable between .c file

  5. 5

    Closure confusion, share global variable between function

  6. 6

    How to access global variable in subroutine

  7. 7

    Alternative to a global variable for passing objects between activities

  8. 8

    Global variable declaration in mvc

  9. 9

    Share global Unity Container between two assemblies without passing a reference

  10. 10

    Calling a variable which is a function without passing parameters to it

  11. 11

    function variable not passing to global variable

  12. 12

    Passing a global variable to a function

  13. 13

    Passing variable from subroutine in tk perl interface

  14. 14

    Can I use global variable instead of passing parameters?

  15. 15

    How C++ differentiates between a call to global variable and declaration of a global variable?

  16. 16

    How C++ differentiates between a call to global variable and declaration of a global variable?

  17. 17

    In VB with classic ASP will local variable hides the Global declaration if variable names are similar

  18. 18

    In VB with classic ASP will local variable hides the Global declaration if variable names are similar

  19. 19

    How to share a global variable between server and client code in Meteor

  20. 20

    C++ Global variable declaration

  21. 21

    Proper Global Variable definition/declaration

  22. 22

    Passing variable parameters in Java

  23. 23

    Global variable that is accessible in models without passing any information from controller

  24. 24

    Share a variable between classes

  25. 25

    Share a variable between classes

  26. 26

    Passing a Variable Between Views

  27. 27

    Passing a reference of a global variable to a function

  28. 28

    Using global variable for option passing?

  29. 29

    Variable Replacement in multiple subroutines

HotTag

Archive