How does this code work? Need comments

Hi,
the following is my sub function in PERL. (Original question ID: 36711912)

1- Can you please explain me how this work?

2- What does these two lines do?

                local $_;
                eval "$1;";

Open in new window


3- Perlcritic complains about eval line above. (http://cpan.uwinnipeg.ca/htdocs/Perl-Critic/Perl/Critic/Policy/BuiltinFunctions/ProhibitStringyEval.pm.html) When I change double quotes to curly braces then perlcritic does not complain but the code does not work

3- Why does it assign to "undef" in some lines?

    my $arg_flag = 0;
    my $parsedData = {};
    my $current_option = '';
    while (my $line = shift @_) {
        if ($arg_flag) {
            if ($line =~ /^Currently (\$\_=.*)/) {
                local $_;
                eval "$1;";
                $parsedData->{dollar_} = $_;
                $arg_flag = 0;
            } elsif ($line =~ /^\s+\-(.*)/) {
                $current_option = $1;
                $parsedData->{$current_option} = undef;
                next;
            } elsif ($current_option && $line =~ /^\s+(.*)/) {
                $parsedData->{$current_option} = $1;
                $current_option = undef;
            }
        } else {
            if ($line =~ /^Original arguments:/) {
                $arg_flag = 1;
                next;
            }
        }
    }

Open in new window

TolgarAsked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
$current_option = undef; prevents one $current_option being used to set $parsedData->{$current_option} = $1; with a second $1 value

And if any code following the section you showed looks at $current_option, it could allow it to distinguish a defined value of '' from an undefined value.

$parsedData->{$current_option} = undef; allows you to unset an option that had previously been set, while also allowing subsequent code to distinguish options that are given with no value from options which are never mentioned at all.
0
 
ozoCommented:
do you understand the description in
perldoc -f eval
0
 
TolgarAuthor Commented:
I think so. Why?

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ozoCommented:
were you questioning the difference between eval "$1;"; and eval {$1;};?
0
 
TolgarAuthor Commented:
Actually yes. And how can I fix it for perlcritic not to get this complain?

0
 
ozoCommented:
what kinds of lines would you use that match Currently (\$\_=.*)
0
 
TolgarAuthor Commented:
@ozo: This is the pattern I parse:

Original arguments:
        -t
        Atk
        -F
        20110914.submit
        -KEYWORD1
Currently $_='154551'

Open in new window


Open in new window

0
 
ozoCommented:
In that case, you could use
if ($line =~ /^Currently \$_='(.*)'/) {
  $parsedData->{dollar_} = $1;
0
 
TolgarAuthor Commented:
@ozo: so what kind of a usage is this one? Can you please explain this to me?
                local $_;
                eval "$1;";
                $parsedData->{dollar_} = $_;

Open in new window


And, why do you think, we use undef in this code? Why do we need that?

Thanks,
0
 
ozoCommented:
eval of a string is a very powerful, and potentially dangerous operation.

if some malicious user were to call the function with a line like:
Currently $_=`delete *.*`
the results could be unfavourable.
0
 
ozoCommented:
and in the case of lines like
Currently $_='154551'
eval of a string is a gross overkill for what is needed
0
 
TolgarAuthor Commented:
ok. I got that part. But why do we use undef in the rest of the code? what does it provide us?

Thanks,
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.