5

How can I transfer the subroutine variable value into another subroutine variable, Can I use global variable.

sub foo(){

my $myvar = "Hello";
} 

sub foo1(){
my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

I tried to use package and global variable, but failed.

Package Bar;
our $bar;

Thank you.

2
  • 2
    This smells like an X/Y problem. If you ask about your actual problem (X) instead of how you think you can solve it (Y) you'll get better answers. Commented Jun 4, 2010 at 13:37
  • 2
    package is spelled package not Package. As it is now, perl sees it as Bar->Package(); (calling the method Package of the package Bar) Commented Jun 4, 2010 at 14:24

5 Answers 5

15

You could declare the variable in a scope that includes the 2 functions:

{ my $myvar

  sub foo{
    $myvar = "Hello";
  } 

  sub foo1{
    my $myvar1 = $myvar;   
  }
}

That is not really elegant though, and can be hard to maintain, as it is not clear in foo1 where the value of $myvar was set. It is probably better to pass the variable as an argument.

sub foo {
    my $myvar = "Hello";
    return $myvar;
}

sub foo1 {
  my( $myvar)= @_;
  my $myvar1 = $myvar;
}

# calling code
my $myvar= foo();
foo1( $myvar);

Note that all 3 $myvar are different variables, in different scopes.

As a side note, using prototypes (sub foo()) is probably not a good idea, unless you really know what they are doing, which is likely not to be the case ( see The problem with prototypes for a discussion on prototypes)

Sign up to request clarification or add additional context in comments.

Comments

6

How can I transfer the subroutine variable value into another subroutine variable, Can I use global variables?

Yes, you can:

my $myvar;
sub foo(){
    $myvar = "Hello";
} 

sub foo1(){
    my $myvar1 = $myvar;   # how can I get the "Hello" from $myvar.
}

This works even with "use strict;" and "use warnings;".

I tried to use package and global variable, but failed.

Package variables are for variables you want to export outside your package, not for variables you want to share between two subroutines in the same package.

Comments

3

Just don't use my:

#!/usr/bin/perl

sub foo() {
  $myvar = "Hello\n";
}

sub foo1() {
  $myvar1 = $myvar;
    print $myvar1;
}

print "here we go!\n";
foo();
foo1();

However, I don't recommend this way of programming.

4 Comments

I'm a little unhappy with the downvoting applied to this question. A comment is enough to point out that the practice might have consequences. However one of the key sayings about Perl is "there is more than one way to do it" (TIMTOWTDI). To savage each other and say there is only one true way to do it is damaging to the Perl community. The author of this reply did so in good faith and perhaps instead of downvoting this post people could have upvoted the good posts instead. Downvoting is best for off-topic answers or deliberately inflammatory of which this post was neither.
Could you please explain me why you are downvoting my answer above? Is it because of my comment on "this way of programming"?
The main issue I see with this (which no-one has chosen to mention, simply feeling that downvoting the answer or adding a cryptic getout with no justification will suffice) is that it uses implicit global variables. This is frowned upon (any global name can be used regardless of if it's declared => no compile-time detection of variable spelling errors, no scoping of variables => subtler bugs) and is one of the reasons why use strict/warnings is such a good idea.
I didn't downvote it, but I'd consider it based on the fact that it advocates bad programming practices without sufficient warning or explanation of why it's bad. If someone asks how to check the temperature of a casserole they're baking, a good answer would be to use a food thermometer. Telling the person to jam their finger into the middle of the casserole to see if it feels hot is a bad answer. Yes, it might tell you if it's hot, but you risk burning yourself, you're putting your finger in the food, making a mess, etc. Even if it works, it's a bad solution.
2

You have a few approaches.

The simplest is not to declare the variable with my. But this requires you to avoid use strict; and not recommended as a result.

You could declare your variable outside the functions at the top of your script. This variable would then be available to all functions below. This is a consequence of scope: variables declares outside a set of curly braces are generally available inside any subsequent curly braces.

You could declare your variable using the use vars qw/$myvar/; pragma. This inherently makes your variable available throughout the following code.

Comments

2

The following code may demonstrate a solution to what you describe:

#!/usr/bin/perl
use strict;
my $var = "hello";
sub foo {
    local *var;
    print "$var world\n";
    $var = "hi";
}

sub bar {
    local *var;
    print "$var world\n";
    $var = "hey";
}

foo();
bar();
print "$var world\n";

The result should be:

hello world
hi world
hey world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.