in reply to compiling perl scripts aka why is perl not as fast as C

I often read arguments for and against strict typing vs. no typing as in Perl.

Let me clarify that Perl does have static typing - it is implied by sigils, and works at compile time. Any expression that begins with @ returns a list or an Array, for example.

However instead of type check failures, you get coercions in many places, which is why you often don't recognize them as types.

Other than possibly catching errors and thereby making for more robust programs, strict typing doesn't by itself contribute to any speed gains, does it?

Sure it does. The more a compiler knows about a program, the more it can optimize. Types are just one of those possible informations.

Also in Perl 6 types are very important for extensibility: you can write multi routines with the same name, where each of these single routines only accepts certain types. That means that you can "overload" those names for custom types. For example

class TurkishStr is Str { ... } multi sub uc(TurkishStr $x) { # handle upper-casing i to I with dot above somehow } # now you can write say uc $string; # and it will work both for ordinary stringsand for Turkish Strings.
It seems like just an excuse for macho programmers to snigger at sissy programmers such as me?

Never attribute to malice what may in fact just be your own state of not being well informed.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: compiling perl scripts aka why is perl not as fast as C
by ikegami (Patriarch) on Mar 21, 2010 at 21:59 UTC

    Let me clarify that Perl does have static typing - it is implied by sigils,>

    So you define Perl5's types as being scalar, array, hash and few others.

    However instead of type check failures, you get coercions in many places,

    No, no coercions occur between any of of those types. There's only coercion if you consider Perl to have dynamic typing (signed integer, unsigned integer, UTF8=0 string, UTF8=1 string, float, regex, IO, etc) in addition to static typing.

    The more a compiler knows about a program, the more it can optimize.

    Indeed, and Perl5 doesn't have that much information available at compile time. Yeah, you can consider scalar to be a static type, but considering all subs take a list of scalars for argument, it's a rather useless type from the perspective of optimising sub calls.

    Talking of static vs dynamic typing sounds a lot like talking about strong vs weak typing.

      So you define Perl5's types as being scalar, array, hash and few others

      Right, these are the user exposed types; strings and integers (PV/IV) are hidden from the user as much as possible, so I don't see them as types in Perl 5. (This attitude is influenced by me not writing any XS code, I think).

      No, no coercions occur between any of of those types.
      my $scalar = @array; # or my @array = $scalar;

      Is that no coercion?

      Talking of static vs dynamic typing sounds a lot like talking about strong vs weak typing.

      In my book there's a difference: For example the C programming language has static typing (determined at compile time), but weak (you can cast anything to anything else even if it doesn't really makes sense).

      But I'm guess I use these words not in the same sense as everybody else does, nor is there any shared consensus on what these things actually mean.

      Perl 6 - links to (nearly) everything that is Perl 6.

        Is that no coercion?

        • my @array = $scalar;

          Definitely not. List assignment takes a list of scalars, and that's exactly what you provided. It's no more a scalar being coerced into an array than my @array = foo(); is a function call being coerced into an array.

        • my $scalar = @array;

          Type coercion is the changing an entity from one type to another, but that's not what that statement does. The array isn't assigned to $scalar as a different type. It's count is assigned, and that's a completely different entity.

          Furthermore, no array is ever present to be coerced. @array acts like a function. It takes an arg and returns a value based on the input and context. Note that there does not exist a function anywhere in Perl's code to coerce an array into a scalar.

        But I'm guess I use these words not in the same sense as everybody else does,

        I think it's more a question of context than definition. Yes, there is static typing in Perl. But there is also dynamic typing. It depends on how zoomed in your view is. If you're concerned with optimising arithmetic, for example, you'll find yourself extremely limited by Perl's dynamic typing. Do you have a string? An integer? An object with overloads? Something magical?

        And then you try to optimise sub calls, and all you know is that you have a list of scalars. Are they the right type? If you think Perl is statically typed, then yes they are the right type since they are scalars. But if you take that view, you can't optimise anything. In reality, it's not good enough to know they are scalars.