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

Alright, trying to wrap my head around something a little challenging for once . . .

All of this is part of a super secret project to implement a small language in Perl - and I have no idea what I am doing. I can't figure out how to make binary operators work, so I'm writing subroutines to implement them, and (hopefully) the parser will turn binops into appropriate calls to these subs.

In order to save my fingers, I decided to generate the subs with a closure, like so:

sub make_bin_ops { my $op = shift; return sub { my ($a, $b) = @_; return eval "$a $op $b"; } } my ($div_OP) = make_bin_ops('/');

This seems to work (but since I'm not always sure about closures, I wouldn't be surprised to learn otherwise).

I decided, however, that the eval in the return sub ref was probably a performance hit. So I decided to use eval instead to return a sub ref that used the operator directly, like so:

sub make_bin_ops { $op = shift; $func = "sub { my (\$a, \$b) = \@_ return \$a $op \$b; }"; return eval $func; } my ($div_OP, $mult_OP) = make_bin_ops('/'), make_bin_ops('*'); print &$div_OP(2,2); print &$mult_OP(2,2);

This however, does not seem to work. Perl tells me I'm calling an undefined subroutine - what gives?


ps - if someone can redirect me to a mortal comprhendable discussion of language implementation on the web, I'd be much appreciative.

Cheers,
Erik

Replies are listed 'Best First'.
Re: Closures! I pity da fool!
by japhy (Canon) on Apr 23, 2002 at 02:52 UTC
    Simple mistake -- you need parentheses:
    my ($div_OP, $mult_OP) = (make_bin_ops('/'), make_bin_ops('*'));

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Closures! I pity da fool!
by Chmrr (Vicar) on Apr 23, 2002 at 02:55 UTC

    Check for syntax errors in your eval -- looks to me like you're missing a semicolon after \@_ The following code works for me:

    sub make_bin_ops { my $op = shift; return eval "sub { \$_[0] $op \$_[1] }"; } my $div = make_bin_ops("/"); my $mult = make_bin_ops("*"); print $div->(2,2),"\n"; print $mult->(2,2),"\n";

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Re: Closures! I pity da fool!
by Molt (Chaplain) on Apr 23, 2002 at 10:05 UTC

    If you're looking at little languages written in Perl you may want to play about with Parse::RecDescent, a suitably wonderous module for generating recursive descent parsers by theDamian.

    On an even more nice-sounding note Perl.com had a tutorial on how to use this module, including a very basic little language with binary operators here.