rakheek has asked for the wisdom of the Perl Monks concerning the following question:

Wise Monks, I am using use strict; and I had to modify my code to add my to all variables. The following code snippet is not assigning the value to $rec_type. I don't understand. It is a simple if -elsif condition. The $rec_type is still "" after the loop. Please help. Regards, Rakhee
my $which_radio_button = param('P1P2_bugs'); # #Decide the rec_type based on the Radio button selection if ( $which_radio_button eq 'P1' ) { my $rec_type = '3'; my $y_label = 'Open P1 Bugs'; } elsif ( $which_radio_button eq 'P2' ) { my $rec_type = '4'; my $y_label = 'Open P2 Bugs'; } elsif ( $which_radio_button eq 'P1-P2' ) { my $rec_type = '2'; my $y_label = 'Open P1-P2 Bugs'; } elsif ( $which_radio_button eq 'Daily-Snapshot' ) { my $rec_type = '1'; my $y_label = 'Daily-Snapshot'; }

Replies are listed 'Best First'.
Re: if else condition not assigning value when using my
by LanX (Saint) on Apr 08, 2010 at 23:20 UTC
    just do ONE declaration in the surrounding scope!

    my $which_radio_button = param('P1P2_bugs'); my $rec_type; my $y_label; # #Decide the rec_type based on the Radio button selection if ( $which_radio_button eq 'P1' ) { $rec_type = '3'; $y_label = 'Open P1 Bugs'; } elsif ( $which_radio_button eq 'P2' ) { $rec_type = '4'; $y_label = 'Open P2 Bugs'; } elsif ( $which_radio_button eq 'P1-P2' ) { $rec_type = '2'; $y_label = 'Open P1-P2 Bugs'; } elsif ( $which_radio_button eq 'Daily-Snapshot' ) { $rec_type = '1'; $y_label = 'Daily-Snapshot'; }

    should do!

    Cheers Rolf

      Thank you Monk. It worked. Thank you all who replied. Regards, Rakhee
Re: if else condition not assigning value when using my
by jethro (Monsignor) on Apr 08, 2010 at 23:19 UTC

    'my' variables are local to any block they are in. In your case I count 4 blocks with as many DIFFERENT variables $rec_type that vanish as soon as the 'if' or 'elsif' block (the code lines inside {}) is finished executing

    put a line "my $rec_type; my $y_label;" before the "if" and remove any 'my's inside the if and elsif cases.

Re: if else condition not assigning value when using my
by GrandFather (Saint) on Apr 08, 2010 at 23:33 UTC

    The immediate problem is that $rec_type and $y_label need to be global to the if/elsif code. If you have access to Perl 5.10.x however you can use given/when to clean up the code somewhat. Consider:

    use strict; use warnings; use 5.010; my $rec_type; my $y_label; given (param('P1P2_bugs')) { when ('P1') { $rec_type = '3'; $y_label = 'Open P1 Bugs'; } when ('P2') { $rec_type = '4'; $y_label = 'Open P2 Bugs'; } when ('P1-P2') { $rec_type = '2'; $y_label = 'Open P1-P2 Bugs'; } when ('Daily-Snapshot') { $rec_type = '1'; $y_label = 'Daily-Snapshot'; } default { die "Unrecognised parameter type: $_\n"; } }

    Note that the two variables that are being set are global to the given block and that there is a default handler for unexpected results from param.

    True laziness is hard work
      In this case of one-to-one mappings I'd rather prefer a hash.

      %radiobutton=( P1 => [ 3, 'Open P1 Bugs' ], P2 => ... ); ($rec_type,$y_label)= @{ $radiobutton{ param('P1P2_bugs') } };

      (untested)

      Cheers Rolf

Re: if else condition not assigning value when using my
by Anonymous Monk on Apr 08, 2010 at 23:15 UTC
    Each code block creates a new scope.
    #!/usr/bin/perl -- use strict; use warnings; { my $one = 1; my $two = 1; { my $one = 3; ## new $one my $two = 4; ## new $two print "$one $two\n"; # old $one $two } if( time ){ my $one = 5; ## new $one my $two = 6; ## new $two print "$one $two\n"; # old $one $two } print "$one $two\n"; # old $one $two } ### NO $one/$two exist here ## print "$one $two\n"; __END__ 3 4 5 6 1 1
    See Coping with Scoping
      Sorry, the first two comments "# old $one $two" are a copy/paste typo.