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

I'm trying to do the following (with all variables appropriately set)
for (@attribute_list) { my $sub = "package $package; sub get$_ { \$_[0]->get($_) }; "; eval $sub; }
It's not only not compiling, but it's not telling me why nor is it allowing me to go into the debugger (a truly last resort!). This is being executed in a section of code that is called as a class method. The method is defined in the parent class and is called like
Foo->define_attributes(qw(attr1 attr2 attr3));
If I comment out the eval and just define the methods myself, I'm all good. But, that sorta doesn't finish the job. *sighs*

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Weird eval behavior...
by converter (Priest) on Oct 09, 2001 at 22:22 UTC

    Fletch gave you the best advice but didn't spell it out. Check the value of $@ after eval(). If $@ is true, the eval() failed and printing $@ will give you the exception message.

    You should always check $@ after eval'ing code, unless you just don't care about the outcome, in which case you might as well just delete the eval. (:

    conv

    Update: for what it's worth, when you print $@, it will probably contain a complaint about a bareword, because after interpolation, you're passing a bareword to your sub. Using qq// and placing quotes around $_ in the sub argument probably fixes your code.

Re: Weird eval behavior...
by Fletch (Bishop) on Oct 09, 2001 at 21:43 UTC

    This works fine for me:

    @attribute_list = qw( foo bar baz ); $package="Zoikes"; for (@attribute_list) { my $sub = qq{package $package; sub get$_ { \$_[0]->get("$_") } }; eval $sub; warn "Problem with $_: $@\n" if $@; } { package Zoikes; sub get { print "Zoikes::get( $_[1] )\n" } } $z = bless { }, "Zoikes"; $z->getfoo; $z->getbar; $z->getbaz; exit 0;

    Also consider checking out Class::MethodMaker to avoid reinventing too many wheels.

      That works. I have no idea what's different, other than the double-quoting of the $_, which shouldn't make a different ... should it??

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        Well, that (""'ing around $_) makes it pass a string rather than a bareword.

Re: Weird eval behavior...
by chromatic (Archbishop) on Oct 10, 2001 at 00:02 UTC
    Mmm, tasty closures:
    no strict 'refs'; for (@attribute_list) { *{ $package . "::get$_" } = sub { $_[0]->get($_); }; }
    Yeah, that's the heart of several modules on CPAN. I want to say Class::Accessors, but it may not be exactly what you're looking for.
Re: Weird eval behavior...
by myocom (Deacon) on Oct 09, 2001 at 21:37 UTC

    Are you sure it's trying to eval what you think it's trying to eval? Replace 'eval' with 'print' (or better yet, since you're looping, do print "$sub\n"; so you can actually read it). Perhaps that will shed some light on it...

    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison
Re: Weird eval behavior...
by perrin (Chancellor) on Oct 09, 2001 at 21:40 UTC
    I don't see anything obviously wrong with your code. There must be some kind of error message from the compile. You may be missing it because of the configuration of your environment.

    (FYI, the debugger can only debug code that compiles correctly. You probably knew that though.)

    Incidentally, there are about 50 modules on CPAN that do this.