in reply to Re: Eval doesn't see lexicals
in thread Eval doesn't see lexicals

Perhaps the main problem is trying to use inside-out objects. I would use a hash otherwise. I have previously used fields to enforce a set of keys, however I understand they are deprecated so I was seeking an alternative. I like the concept of inside-out objects, however there's no easy way to walk the keys. I also wanted to keep the keys in order for outputting a record so a straight hash wouldn't be enough anyway.

Design considerations aside, I have been banging my head against this code for a little while. It would be nice to know why eval is choosing to ignore a lexical until it is named, unfortunately when I try to boil it down to a simple test case, it works, so I am obviously removing the problem as well, and I am not at liberty to post the code I am working on.

Thanks for the reply, anyway

Replies are listed 'Best First'.
Re^3: Eval doesn't see lexicals
by chromatic (Archbishop) on Nov 02, 2005 at 02:15 UTC
    It would be nice to know why eval is choosing to ignore a lexical until it is named...

    That's because Perl doesn't know (at compile time) whether the eval will run nor what it will contain, so it really can't bind to the appropriate lexical. One way to solve this may be to have every scope containing a string eval to bind every lexical in every parent lexical pad, but wow, what a cost.

    It would surprise me if dave_the_m weren't along here shortly to give more details, especially where I started waving my hands over the details.

Re^3: Eval doesn't see lexicals
by Anonymous Monk on Nov 02, 2005 at 02:20 UTC
    package EvilEval; use Class::Std; sub _serial() { '1234567890' }; my %serial_of :ATTR; sub BUILD { my ( $this, $ident, $arg_ref ) = @_; $serial_of{$ident} = _serial(); } sub evil { #use Data::Dumper; print Dumper \%serial_of; my $string = 'use Data::Dumper; print Dumper \%serial_of'; eval $string; } 1;
    Meanwhile, in a nearby perl script ...
    use EvilEval; my $evil = EvilEval->new(); $evil->evil();
    This demonstrates the problem, at least on my setup. If all the code is in the same file there is no problem - even if it is declared in different packages. When the caller is in a different file, the problem occurs.
      This is a version of the problem that I described in Re: Eval doesn't see lexicals. (End of file is a block exit and Perl does cleanup.) The simplest solution is to insert the following in EvilEval.pm.
      # Fool Perl's reference counting into keeping %serial_of alive sub do_not_cleanup_serial_of { %serial_of; }