slicks1
slicks1

Reputation: 349

perl not saving variable value outside of if clause

I am a newbie to perl. Heres my issue:

my $monopentime;my $monopenper;my $monclosetime;my $moncloseper;my $monclosed;

if($monday ne "closed"){
 my @monfields = split /-/, $monday;
 my $monopen = $monfields[0];
 my @monopenparts = split / /,$monopen;
 my $monopentime = $monopenparts[0];
 my $monopenper = $monopenparts[1];
 my $monclose = $monfields[1];
 my @moncloseparts = split / /,$monclose;
 my $monclosetime = $moncloseparts[0];
 my $moncloseper = $moncloseparts[1];
 my $monclosed=0;
 print $monopentime;
} else {
 my $monopentime="1";
 my $monopenper="AM";
 my $monclosetime="1";
 my $moncloseper="AM";
 my $monclosed=1;
}
print $monopentime;

If you notice I have two print statements, one inside the ne clause and one outside the if else. when i print the variable inside the clause it prints the data but when I print outside the if else clause i get nothing.

Like I said im fairly new to perl what is the issue? I set my variables outside the clause too

Upvotes: 1

Views: 1300

Answers (3)

ikegami
ikegami

Reputation: 385809

my creates a new variable (scoped to the inner-most block in which it is located). Inside the if, you want to assign to the existing variable, so drop the my.

my ( $monclosed, $monopentime, $monopenper, $monclosetime, $moncloseper ); 
if ($monday eq "closed") {
    $monclosed    = 1;
    $monopentime  = "1";
    $monopenper   = "AM";
    $monclosetime = "1";
    $moncloseper  = "AM";
} else {
    $monclosed = 0;
    my ($monopen, $monclose) = split /-/, $monday;
    ( $monopentime,  $monopenper  ) = split ' ', $monopen;
    ( $monclosetime, $moncloseper ) = split ' ', $monclose;
}

print "$monopentime\n";

I cleaned up the code at the same time.

  • Avoided some needless temporary vars.
  • Used the list form of my to improve readability.
  • Avoided a needless negation in the condition (by swapping the "then" and "else" blocks).
  • Used split ' ' instead of split / / for increased robustness. (These are not equivalent.)
  • Improved indenting and spacing.
  • Moved $monclosed to the front/top. It seemed more natural.

Upvotes: 3

Hunter McMillen
Hunter McMillen

Reputation: 61512

Variables that are declared with my are lexically scoped, meaning they only exist in the innermost enclosing block. In this case that means your if and else blocks. If you need to access their values outside of these blocks, they should be declared outside of your if-statement.

use warnings; and use strict; will also reveal that you are shadowing the definition of $monopentime here as well.

Upvotes: 1

Sobrique
Sobrique

Reputation: 53478

my lexically scopes a new variable to the current block.

If you run this with strict and warnings enabled you will get:

"my" variable $monopentime masks earlier declaration in same scope.

Which means exactly what it says - you're declaring it twice, writing a value into the one inside the block - and then it vanishes at the end of the block.

So basically - remove the 'my' statements around the variables you have already declared at the beginning, and it will work.

And turn on use strict; use warnings; because that would have told you exactly what the problem is.

Upvotes: 3

Related Questions