Search code examples
linuxperlscalarcurly-brackets

Scalars in Perl - Can they be exported throughout the script


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...


Solution

  • 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";
    }