in reply to eval with sub

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

Replies are listed 'Best First'.
Re: Re: eval with sub
by strredwolf (Chaplain) on Mar 10, 2001 at 02:31 UTC
    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

        Merlyn wrote "That'll be faster, safer, won't leak memory like a sieve"

        Is Merlyn saying that poorly coded Perl can leak memory?

        According to the Memory Advisor page "A leak is a data area that your program allocated but did not free, even though your program will not use the memory again. In many instances, the data area has no pointer..."

        I thought that Perl takes care of such matters automatically, and that memory leaks were not a problem in Perl programs. Now I think from the comments of Merlyn and others that memory leaks can indeed be a problem in Perl.

        For instance, Jamie Zawinski wrote in a 1998 article that

        just about all of the open source garbage collectors are junk (e.g., Perl, Python, and Emacs.) Perl's GC is an especially bad joke: if you have circular references, the objects won't get collected until the program exits! Give me a break!

        Do poorly-coded Perl programs really contain memory leaks?