in reply to C-like Function Prototypes
in thread sub ($self,$var,@arr) {}

I don't think that C style dispatch on the type of parameters is being asked for. What is being asked for is done in a number of languages. For comparison here is a Ruby version of your function which differs only in the fact that Ruby was designed from the start using lexical scope and so does not have to support dynamic scope for backwards compatibility:
def foo (x, y=10, z=nil) z = x * y if z.nil? # etc end
which does not seem to me to be significantly uglier than Perl.

But mixing Perl and typing is IMNSHO utterly broken. It is often said that Perl does not differentiate between strings, numbers, etc. This is only somewhat true. Perl allows a variable to contain any of them, and then your code differentiates between them. Mixing this existing type system with anything else doesn't work. On a similar note, it is very hard in Perl to write generic code which will continue to work if people pass in strings, references to arrays, etc. It can be done, but you are fighting the language.

In Ruby this is trivial. In fact the above code is perfectly generic as long as x is some type that supports a * method. (Overloading in Perl is possible, but not so transparent.)

Now I am not saying that the way that Ruby does it is right or better. I am just saying that it is possible to support prototyping very similar to what raptor asked for without it being ugly and broken. I am also saying that Perl's claim of not forcing types on variables isn't completely true. But what Perl loses in the ability to write generic code, it gains in the fact that you can transparently extract numbers from strings, work with them as numbers, and then use them as strings again without needing to do explicit conversions. And 3/2 in Perl is not 1.

PS: Ruby does not have a concept like @_. The closest it has to that is the ability to declare a parameter with a * which will gather remaining arguments into an array. If Ruby did have @_ then the conflict between the two modes of passing arguments would become much worse. Not coincidentally, Ruby is also not list oriented...

PPS: I am not trying to advocate Ruby, merely show by comparison with another language that the suggestion can work. However I don't think it would work well in Perl for a number of reasons. First is the confusion with the existing idea of prototypes. Second we have the conflict with the existing notion of @_. Thirdly there would be questions about pass by value versus pass by reference.

Replies are listed 'Best First'.
Re: Re (tilly) 2: C-like Function Prototypes
by raptor (Sexton) on Feb 13, 2001 at 16:29 UTC
    ok... I wanted to write also this, but didn't done that ... but now will do :
    sub mysub (\$var1,$var2) { ..code.. $var1 = "Hello";#this changes $somvar later in the code }
    Later :
    mysub(my \$somvar,$echo) print $somevar
    instead of :
    sub mysub () { my (\$var1,$var2) = @_; } my $somvar = 12; my $refvar = \$somvar; mysub($refvar,$echo) print $somevar
    my 5c