Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variable, subroutine variable Question in Perl

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.

like image 481
Nano HE Avatar asked Jun 04 '10 07:06

Nano HE


People also ask

Can global variables be used in subroutines?

The scope of a variable can either be local to a particular subroutine, or part of a subroutine, or global to the whole program.

How do you pass a variable in Perl subroutine?

Passing Lists or Arrays to a Subroutine: An array or list can be passed to the subroutine as a parameter and an array variable @_ is used to accept the list value inside of the subroutine or function. Example 1: Here a single list is passed to the subroutine and their elements are displayed.

What is subroutine in Perl explain with suitable example?

A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.


4 Answers

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)

like image 163
mirod Avatar answered Oct 04 '22 15:10

mirod


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.

like image 33
Snake Plissken Avatar answered Oct 04 '22 16:10

Snake Plissken


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.

like image 20
MarcoS Avatar answered Oct 04 '22 16:10

MarcoS


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.

like image 43
PP. Avatar answered Oct 04 '22 15:10

PP.