Sivananda Ganapathy
Sivananda Ganapathy

Reputation: 25

How to use global variable in perl?

I will write a pseudo code

f1 {
..
..
if (count==1) {
count=0;// set it back to 0
return $variable;
}


f2
{
count=1;
var2=f1(..); // 
}

ie. if f1 is called from f2 alone, I want the f1 to return a variable. otherwise no need. I want to achieve this using global variable. can u help . or is there any other way also? Thanx in advance.

Upvotes: 0

Views: 175

Answers (1)

Dada
Dada

Reputation: 6626

Using global variables is usually a bad idea, as they make the code harder to maintain (as mentioned by @choroba in the comments) and to write.

As mentioned by @Borodin, if f1 should only return something if called by f2 and nothing otherwise, the simplest is to let f1 return something, regardless of whether it was called by f2 or not, and just ignore that return value if you are not in f2.

If however, f1 needs to return a special value when called from f2, and something else when called from somewhere else, there are several possibilities: You could pass a parameter to f1 that would identify who called it. Or you could use caller to know who called f1. Or, if you really want to, you can use a global variable. Bellow are examples of each way.

(You can replace my say with your return.)


Using caller:

use strict; 
use warnings qw( all );
use feature qw( say );

sub f1 {
    my $caller = (caller(1))[3];
    if ($caller && $caller eq 'main::f2') {
        say "Called from f2";
    } else {
        say "Not called from f2";
    }
}

sub f2 {
    f1();
}

f1();
f2();

caller returns an array containing information about where the current function was called from (line, package, function, etc.). The 4th element of this array is the function who called it, fully qualified with its package, hence the use of main::f2 and not just f2.


Using an additional parameter:

use strict; 
use warnings qw( all );
use feature qw( say );

sub f1 {
    my $caller = shift // '';
    if ($caller eq 'f2') {
        say "Called from f2";
    } else {
        say "Not called from f2";
    }
}

sub f2 {
    f1('f2');
}

f1();
f2();

In this example, the parameter to f1 is optional. However, if f1 has other parameters, you might consider making it non-optional.


Using a global variable :

use strict; 
use warnings qw( all );
use feature qw( say );

our $f1_flag = 0;

sub f1 {
    if ($f1_flag == 1) {
        say "Called by f2";
    } else {
        say "Called outside f2";
    }
}

sub f2 {
    local $f1_flag = 1;
    f1();
}

f1();
f2();

You'll noticed that I renamed your $count for $f1_flag: it's already bad enough to have global variables, so better try to give them significant names. (That way, you shouldn't have to much trouble knowing what this variable is for, and you probably won't be tempted to reuse the same name for another variable).

our declares a global (or package) variable. Then, instead of setting it to 1 inside f2, and then back to 0 inside f1, I prefer to use local: $f1_flag will be 1 for every function or instruction inside the dynamic scope of f2.

Upvotes: 4

Related Questions