0

Not sure how clear my title is, but im new to perl and had no other way to really describe it. What I'm trying to do is something like this:

if(condition){
     my $VAR = " ";
}

Then later use $VAR somewhere else...

if(! $USR){
     my $USR = "$VAR";
}

Is there a way to call a scalar that is referenced elsewhere that has been bracketed off? {}

Thanks in advance...

Nick Hatfield
  • 315
  • 2
  • 11

2 Answers2

3

A variable that is declared inside of scope {} is destroyed outside of scope. You need to declare variable before. Always use strict and warnings it will save you a lot of time.

use strict;
use warnings; 

my $VAR;
my $USR;
if(condition){
     $VAR = " ";
}

if(! $USR){
    $USR = "$VAR";
}
Andrey
  • 1,808
  • 1
  • 16
  • 28
0

In perl, the my variables are lexically scoped, so they can be accessed in that scope:

{ 
# start of lexical scope

my $VAR;

# end of lexical scope
}

In the above code $VAR can only be seen between the opening and closing braces, and after the ending brace $VAR no longer exists.

Miguel Prz
  • 13,718
  • 29
  • 42