Perl tends to use a descriptive character to denote what something is in a typeglob:
$ => SCALAR @ => ARRAY % => HASH
so wouldn't the language be a trip if subroutines were written just like the glob assigments they are:
&add = ( shift + shift ) ;
instead of
sub add { shift + shift }

note for some reason, I couldn't write &add within a code tag.. here's what happens:

&add

Edit: chipmunk 2001-09-09

Replies are listed 'Best First'.
Re: A subroutine is a reference to a list of statements
by japhy (Canon) on Sep 09, 2001 at 21:52 UTC
    Note: in <code> tags, you don't need to do any &-escaping.

    If the &foo syntax goes the way of the dodo (for calling functions) in Perl 6, then I do think a viable method of assigning code to a function would be something like:

    &add = sub { ... };
    I'm not sure I agree with the syntax you've proposed. A list is a very specific thing in Perl, and I wouldn't call a subroutine a list of statements. A block is a connected series of statements, and a subroutine is a block with benefits.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      But what if you wanted to manipulate a subroutine like a list? And push and pop statements from it? And splice things, etc and so forth.

      The sub word is un-necessary. Just think, do we have to do:

      $x = scalar 5;
      No, the onl time we needscalar is when we are forcing something else into scalar context.
        If you want to treat a subroutine as a list, that's fundamentally different from how most languages treat them. You'd have to create an array of statements and modify that array, and evaluate the entire array.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        I think that a better way to do this might be the way that (AFAIK) has been proposed for Perl 6: runtime syntax tree modification. So...
        &hello = sub { print "Hello, " }; &hello.syntree[0][1] .= "world!\n"; hello(); # prints "Hello, world!"
        ... or some such thing.
        Unfortunately, Perl's syntactic texture is more than just a list of words. To make meaningful code changes, you need to work on the parse tree.

        You could take a look at HP's RPL ("Reverse Polish Lisp"), which does things this way (it mimics Forth, where subs are (almost) nothing more than lists of words. Available on any of their programmable calculators, at least since the HP-41 (I think), and including the HP-48 and HP-49 series.

Re: A subroutine is a reference to a list of statements
by Maclir (Curate) on Sep 10, 2001 at 00:57 UTC
    While your proposition may have some intellectual "completeness" to it, there is enough line noise in Perl without taking what is a perfectly understood concept of labelling subroutines / functions / whatever with a descriptive tag. Now any reasonable programmer seeing a code fragment like:
    sub foo { # # some collection of statements that do foo # return $status }
    will be pretty clear what it is doing. Why complicate things by replacing a perfectly good word (sub) with some cryptic symbol? This is Perl, not APL. How does it advance the language, or provide needed features to the language?
Re: A subroutine is a reference to a list of statements
by t'mo (Pilgrim) on Sep 10, 2001 at 17:40 UTC

    I would have to agree with Maclir's comments: the syntax as it stands is readable, and that's a good thing. But, if you really want to be clever about it...

    As I relearned* last week with this question, you can directly manipulate the symbol table to assign a sub to the CODE portion of a typeglob:

    *desiredSubroutineName = sub { ... };

    Here's a working example that surprised me:

    perl -e '*s1=sub{&s2};*s2=sub{print"Hello,Daniel\n"};&s1'

    Unfortunately, that pesky sub-word keeps coming up! :-)

    I think the statement about "cleverness" in tilly's reply to my earlier question would be relevant here... I'm guessing that a good portion of the time, most Perl programmers don't (and shouldn't) muck around with the symbol table.


    * I said "relearned" in the sense that I had read about this in the Camel, but had never used it.

Re: A subroutine is a reference to a list of statements
by chipmunk (Parson) on Sep 10, 2001 at 19:02 UTC
    As t'mo said, you do need the sub keyword to do something like this currently. However, you can in fact assign a subroutine, with the help of AUTOLOAD and the new lvalue subroutine feature in 5.6.
    use 5.006_001; sub AUTOLOAD : lvalue { *{ $AUTOLOAD } } &add = sub { shift() + shift() }; print add(17, 25), "\n";
    It doesn't allow you to manipulate the subroutine as a list of statements, and you can only assign the subroutine once, but maybe it could be useful anyway. :D