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

Would $op='tag00'; eval "sub{dofunc($op);}" have the same result as sub{dofunc('tag00');}"

--
$Stalag99{"URL"}="http://stalag99.keenspace.com";

Replies are listed 'Best First'.
Re: eval with sub
by merlyn (Sage) on Mar 10, 2001 at 02:00 UTC
    On further reflection, why the eval at all?
    my $coderef = do { my $op = 'tag00'; sub { dofunc($op) } };
    That works just fine. Makes it a closure. 'tag00' can be a literal or a calculation.

    -- Randal L. Schwartz, Perl hacker

      Here's part of the code it does in. Works fairly well:
      if($l=~/^\[([^\]]+)\]/) { $j=$1; $k=$1; $j=$1, $k=$2 if($j=~/^([^\|]+)\|(.+)$/); $url[$uc]=$j; $l=$k; $op="url$uc"; $uc++; print "$op --> $j | $k\n"; $main_index_list->insert("end",$l,$op); $main_index_list->tagConfigure($op,-foreground=>"blue", -data=>$j); $main_index_list->tagBind($op,"<3>",eval "sub{do_url('$op');}"); }
      Update: Fixed with Merlyn's code.

      --
      $Stalag99{"URL"}="http://stalag99.keenspace.com";

        No. Stop with that eval. You are re-compiling that code every time you call that. There's no need.
        $main_index_list->tagBind($op, "<3>", do { my $thing = $op; sub { do_u +rl($thing) } });
        That'll be faster, safer, won't leak memory like a sieve, and if you had made $op a lexical variable earlier, could have been just written as :
        $main_index_list->tagBind($op, "<3>", sub { do_url($op) });
        But since I don't know the scope of $op, I wrote it safely.

        Please eliminate the runtime string-eval there! Please. It's so very unneeded.

        -- Randal L. Schwartz, Perl hacker

Re: eval with sub
by merlyn (Sage) on Mar 10, 2001 at 01:53 UTC
      ahhh.... I'm asking now so I don't have too many bugs in when I'm actually putting it into the chatterbox client.

      --
      $Stalag99{"URL"}="http://stalag99.keenspace.com";

(ar0n) Re: eval with sub
by ar0n (Priest) on Mar 10, 2001 at 01:56 UTC
    No. You'd have to put single quotes in:
    $op= q('tag00'); eval "sub{dofunc($op);}"

    Update: D'oh! Damn frickin' slow mozilla.

    [ar0n]