Re: How to define a sub on the fly?

From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 05/01/04


Date: 1 May 2004 12:40:10 GMT

Mark Clements <mark.clements@kcl.ac.uk> wrote in comp.lang.perl.misc:
> Anno Siegel wrote:
>
> >>eval<<EOF;
> >> sub newsub(){
> >
> >
> > Why the prototype? They are the exception with sub definitions, not
> > the rule.
> I tend to use them because if used correctly they enable the compiler to
> catch attempts to call subs with the wrong number of arguments,

True, but it also makes your subs behave weirdly, supplying unexpected
context to arguments or even silently taking references. In my view,
prototypes can (sparingly) be used in interface-type functions to
help the user deal with them, or to allow suggestive syntax. The
non-standard behavior must be clearly documented. Few Perl programmers
use prototypes routinely with all subs.

> though
> obviously here they'll have no effect as the compiler can't see the
> definition of the new sub. It was therefore misleading of me to use it here.
>
>
> >
> > *newsub = sub { return "from newsub" };
> In this case, yes, but I don't see how this enables you to build
> subroutines on the fly. eval STRING is relatively inefficient but
> extremely powerful (and therefore dangerous).

If you actually need to compile a string of code for the new sub, there
is no way around "eval".

Often a closure gives enough flexibility. If the sub needs to read a
particular file, this would do (simplified, untested):

    sub create_sub {
        my $name = shift;
        my $sub = sub {
            open( my $f, $name) or die "Can't read file '$name': $!";
            # ...
        };
        no strict 'refs';
        *{ 'read_' . $name} = $sub;
    }

That creates one named closure per call, each reading the file it is
named after. No need for "eval" here.

Anno



Relevant Pages

  • Re: kanjidic parser in Perl?
    ... they give programmers a false sense of security, because they only catch argument type and number errors under some circumstances and not others. ... that he make a point of including prototyped sub declarations ... I myself have found prototypes to be extremely valuable, ... John Chew http://www.poslfit.com ...
    (sci.lang.japan)
  • Re: Sweetest Accessor?
    ... care for mixing shift and @_ in the same sub -- visually. ... That isn't tied to shifting. ... You can't throw prototypes into an OO design. ...
    (comp.lang.perl.misc)
  • Re: IO::Socket::INET on OSX or TCP stack problem
    ... just declaring vars outside a sub makes them static. ... SG> I am using perl -w - I dont usually, but while I am trying to figure ... SG> perl -w complains if you don't use prototypes. ... perl doesn't complain if you don't use prototypes. ...
    (comp.lang.perl.misc)
  • Re: why doesnt this argument list need a comma after the 1st argument?
    ... I'm reading this very interesting book on Perl (Effective Perl Programming ... that the anonymous subroutine does not require the "sub" keyword, ... Prototypes were introduced to allow you to write ... block (without a comma) like this, and treat it as an anon sub. ...
    (comp.lang.perl.misc)
  • Bit counting benchmarks Was: count of 1s in a binary number
    ... >> Anno Siegel ... > with a lookup table. ... ilya - the method Ilya presented ... sub decrement { ...
    (comp.lang.perl.misc)