DEV Community

Discussion on: Switching up my switches in Perl

Collapse
 
x1madni profile image
x1mandi • Edited

Hi Mark,

I tried the suggested for loop with topicalizer in one of my scripts. I am developing a reporting tool in Perl (fetch data from DB and generate excel or html reports, then send them via mail).
I am still new to Perl, so I am writing baby Perl - I think that's the term for that :), and I must apologize for the sometimes cumbersome solutions.

Here is how my envrionment looks like:
There is a Collect.pm package,which initializes the "global" scalar $USECASE:

package My::Data::Collect;
use strict;
use warnings;
#Exports
use base 'Exporter';
our @EXPORT_OK = qw($USECASE get_pss_ael);
...
our $USECASE; 
Enter fullscreen mode Exit fullscreen mode

There is report.pl which import My::Data::Collect and assings the value to $USECASE using GetOpt::Long like follows:

GetOptions('type=s' => \$USECASE,
'mode=s' => \$MODE);

and then I start the scirpt: report.pl --type uc1.

Later in the same script I call get_pss_ael() subroutine from My::Data::Collect within a for loop, with $USECASE as topicalizer:

for ($USECASE) {
    /(uc1|uc2|uc3)/ and do {
        get_pss_ael($USECASE);
    }
}
Enter fullscreen mode Exit fullscreen mode

Then out of sudden the $USECASE scalar is undef inside the subroutine call. When I check it with say outside the for loop in the main namespace (report.pl) it is there with the value assigned by GetOpt::Long, in My::Data::Collect, outside any code block and subroutine it is there, although when I pass it to a subroutine it is already undef.
Substituting that for construct with if-else solves that problem.

What's going on there?
Could You please provide further explanation?
I hope I described my issue with enough examples, and good code snippets. If not please let me know and I edit.

Thank You!
Kind Regards,
Csaba