• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 132
  • Last Modified:

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

0
Tolgar
Asked:
Tolgar
  • 7
  • 5
1 Solution
 
ozoCommented:
do you understand the description in
perldoc -f eval
0
 
TolgarAuthor Commented:
I think so. Why?

0
 
ozoCommented:
were you questioning the difference between eval "$1;"; and eval {$1;};?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
ozoCommented:
$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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now