Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Perl *is* strongly typed (was Re: Perl description)

537 views
Skip to first unread message

Mark Jason Dominus

unread,
Jun 22, 2001, 5:14:31 PM6/22/01
to
In article <m1y9qk9x...@halfdome.holdit.com>,
Randal L. Schwartz <mer...@stonehenge.com> wrote:
>No, Perl has strong compile-time typing for primitive data types
>(scalars, arrays, hashes, filehandles),

I cannot imagine what you might mean by this, since each of these is
converted to the others automatically upon request, and hardly any
checking is done. If this qualifies as 'strong typing' then I must
conclude that 'strong typing' no longer has any meaning at all.

>The compiler will abort if you try to put an array into a hash
>variable: in fact, it can't even be specified because of the prefix
>characters and the context they provide.

Again, I think this makes a mockery of the concept of strong typing.
It seems to me that the only way to accept this and still have a
logical, consistent meaning for 'strongly typed' is to accept a
trivial definition, so that *every* language is strongly typed. If
you do this, the concept of 'strongly typed' uses any usefulness it
might have had.

In Perl,

@a = %h
and
%h = @a

are perfectly legal, and the compiler will not raise an error or even
a warning for them; it will instead apply an automatic conversion.
This behavior would appear to be the exact opposite of 'strongly
typed'.

awk is normally considered to be a not-strongly-typed language and is
frequently cited as a counterexample. But awk shows better diagnosis
of type errors than Perl in examples similar to the one you cited:

awk 'BEGIN { x["y"] = 1;
z = x }'

awk: cmd. line:2: fatal: attempt to use array `x' in a scalar context

I conclude from this that you would also consider awk to be a strongly
typed language, perhaps more strongly typed than Perl. If so, then
what you mean by 'strongly typed' above has nothing to do with the
what the mainstream of computer science means by it.

If this isn't what you meant, I would like to see what you would
consider to be an example of a non-strongly-typed language of any
sort, and an example of an assignment or binding allowed by this
language that is disallowed in Perl.

>C++, by contrast, has strong compile-time typing, but weak run-time
>typing, because you can cast a pointer to another type and call a
>totally broken method on it. Crash!

I suppose this is your example, but I don't find it very illuminating,
because you can do an analogous thing in Perl:

my $randal = PerlExpert->new(NAME => "Randal Schwartz");
$randal->{number_of_wheels} = 18;
$randal->{temperature_of_fusion} = "1600 Kelvins";
bless $randal => 'Adorable_Doggie';
$randal->wag_tail(); # Totally broken method! Crash!

I don't see how you conclude from this anything about the relative
run-time typing strength of C++ and Perl.


--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print

Benjamin Goldberg

unread,
Jun 23, 2001, 2:43:44 AM6/23/01
to
Mark Jason Dominus wrote:
>
> In article <m1y9qk9x...@halfdome.holdit.com>,
> Randal L. Schwartz <mer...@stonehenge.com> wrote:
> >No, Perl has strong compile-time typing for primitive data types
> >(scalars, arrays, hashes, filehandles),
>
> I cannot imagine what you might mean by this, since each of these is
> converted to the others automatically upon request, and hardly any
> checking is done. If this qualifies as 'strong typing' then I must
> conclude that 'strong typing' no longer has any meaning at all.
>
> >The compiler will abort if you try to put an array into a hash
> >variable: in fact, it can't even be specified because of the prefix
> >characters and the context they provide.
>
> Again, I think this makes a mockery of the concept of strong typing.
> It seems to me that the only way to accept this and still have a
> logical, consistent meaning for 'strongly typed' is to accept a
> trivial definition, so that *every* language is strongly typed. If
> you do this, the concept of 'strongly typed' uses any usefulness it
> might have had.

Maybe he was thinking about how perl reacts to stuff like this:
my ($x,$y) = ( {}, [] );
print @$x, $x->[0]; # any of these four dereference attempts
print %$y, $y->{0}; # will cause perl to abort.

[snip]


> >C++, by contrast, has strong compile-time typing, but weak run-time
> >typing, because you can cast a pointer to another type and call a
> >totally broken method on it. Crash!
>
> I suppose this is your example, but I don't find it very illuminating,
> because you can do an analogous thing in Perl:
>
> my $randal = PerlExpert->new(NAME => "Randal Schwartz");
> $randal->{number_of_wheels} = 18;
> $randal->{temperature_of_fusion} = "1600 Kelvins";
> bless $randal => 'Adorable_Doggie';
> $randal->wag_tail(); # Totally broken method! Crash!
>
> I don't see how you conclude from this anything about the relative
> run-time typing strength of C++ and Perl.

Well, maybe not from *that* example. But how about from this one:
C code:
int main() { char x; printf("%d\n", *(int*)&x ); }
Perl code:
main: { my $x = [0]; printf("%d\n", $x->{0} ); }

Neither of these will produce any error at compile time, but at runtime,
the C code will try to read from memory it shouldn't be reading from
(and possibly crash), but the Perl code will detect the problem, print
an appropriate error message, and exit.

Another example:
perl -e "[]->{0}"
If Perl were loosly typed, perl would actually call the hash fetch
subroutines with the reference to an array. Since it's not that loosly
typed, it detects that the lhs is not a hash ref, and prints out an
error message and exits.

--
The longer a man is wrong, the surer he is that he's right.

Sweth Chandramouli

unread,
Jun 23, 2001, 3:25:28 AM6/23/01
to
In article <3b33b536.37c$3...@news.op.net>,

Mark Jason Dominus <m...@plover.com> wrote:
>In Perl,
>
> @a = %h
>and
> %h = @a
>
>are perfectly legal, and the compiler will not raise an error or even
>a warning for them; it will instead apply an automatic conversion.
>This behavior would appear to be the exact opposite of 'strongly
>typed'.
There's more to casting than assignment restrictions, though.
You can't do things like

my @a;
$a{'foo'};

, and have the second statement act on @a; because the {...} index
lookup syntax isn't defined for an array, the second statement can only
act on %a. If you're using strict 'vars', then, the above becomes
illegal, because Perl won't convert @a into a hash for this purpose, and
isn't allowed to use the undeclared %a. Using warnings can actually add
some minimal typing at the integer/string level as well; I think you
actually did a discussion of this that I read at some point. I'm not
denying your assertion that if Perl is strongly typed, then so is nearly
everything else; it's just that Perl isn't as weakly typed as it might
seem at first glance. (ksh93, for example, also has both associative and
indexed arrays, and in that language there is no real type distinction
between them, so you can do things like
$ set -A foo
$ foo[1]=1
$ foo[bar]=2
$ print ${foo[@]}
2 1
. I'd definitely say that that is weaker typing than Perl
provides.).

-- Sweth.

--
Sweth Chandramouli ; <sweth...@gwu.edu>

Mark-Jason Dominus

unread,
Jun 23, 2001, 11:33:05 AM6/23/01
to

Benjamin Goldberg <gol...@earthlink.net>:

> Another example:
> perl -e "[]->{0}"
> If Perl were loosly typed, perl would actually call the hash fetch
> subroutines with the reference to an array. Since it's not that loosly
> typed, it detects that the lhs is not a hash ref, and prints out an
> error message and exits.

OK, fair enough. But I would have said that a type system that only
has three or four types in it was a very weak type system indeed.
Fortran IV (1966) has more types than that, and I don't think many
people would claim that Fortran was a strongly typed language; my
previous message showed that AWK has similar checks, and I have never
known anyone to make the claim that AWK is a strongly typed language.

My argument wasn't that Perl isn't strongly typed, because I don't
pretend to know what that means. Let me say that again: I don't know
what it means for a language to be 'strongly typed'. I'm not arguing
with you because I think you're wrong and I think Perl is not a
strongly typed language. I do not know what 'strongly typed language'
means. (I did some research, and it didn't help; see below.)

All I said was that if Perl is considered strongly typed, then I don't
think the concept is useful or meaningful, because *every* language is
strongly typed. It appears from your argument above that you would
consider AWK and Fortran IV to be strongly typed languages also. That
may be a logcially consistent position to take, but it does not seem
to be a very useful one. My original message asked Randal to produce
an example of a language that is *not* strongly typed, and I don't
think you can do that---if you're going to rule out examples like AWK,
that doesn't leave you with very much to work with. (I guess you
could bring up assembly language. But that would be a silly evasion.)

>From a brief survey of web documents about strong typing, it appears
that the description 'strongly typed language' does *not* have any
useful or agreed-upon meaning. The confusion is not simply about
where to draw a line on a linear scale of type granularity or degree
of error checking or anything like that; it's not that some people
think a strongly-typed language needs fifty types and others think it
needs only thirty. There appears to be a fundamental confusion about
the basic meaning of the term 'strongly typed language'. In a couple
of hours, I found eight different and incompatible definitions of
'strongly typed language':

1. A language is strongly typed if type annotations are
associated with variable names, rather than with values.
If types are attached to values, it is weakly typed.

2. A language is strongly typed if it contains compile-time
checks for type constraint violations. If checking is
deferred to run time, it is weakly typed.

3. A language is strongly typed if it has compile or run-time
checks for type constraint violations. If no checking is
done, it is weakly typed.

4. A language is strongly typed if conversions between
different types are forbidden. If such conversions are
allowed, it is weakly typed.

5. A language is strongly typed if conversions between
different types must be indicated explicitly. If
implicit conversions are performed, it is weakly typed.

6. A language is strongly typed if there is no language-level
way to disable or evade the type system. If there are
casts or other type-evasive mechansisms, it is weakly typed.

7. A language is strongly typed if it has a complex,
fine-grained type system with compound types. If it has
only a few types, or only scalar types, it is weakly typed.

8. A language is strongly typed if the type of its data
objects is fixed and does not vary over the lifetime of the
object. If the type of a datum can change, the language is
weakly typed.

Some of these are contradictory; some are merely orthogonal. Many
came from class notes for undergraduate CS courses. Not all of these
instructors and experts can be mistaken. The only sensible conclusion
is that there is no agreement about what it means to be a strongly
typed language.

Examples are no help here:

I found several pages that asserted that C is a strongly-typed language.
I found several pages that asserted that C is a weakly-typed language.

I found several pages that asserted that C++ is a strongly-typed language.
I found several pages that asserted that C++ is a weakly-typed language.

I found several pages that asserted that Lisp is a strongly-typed language.
I found several pages that asserted that Lisp is a weakly-typed language.

I found several pages that asserted that Perl is a strongly-typed language.
I found several pages that asserted that Perl is a weakly-typed language.

It now appears to me that the topic under discussion, whether Perl is
a strongly or a weakly typed language, is as meaningless as the
related question of whether Perl sux or roolz, so I will withdraw from
the discussion.

Bruce Van Allen

unread,
Jun 23, 2001, 2:01:45 PM6/23/01
to
At 11:33 AM -0400 6/23/01, Mark-Jason Dominus wrote:
>From a brief survey of web documents about strong typing, it appears
>that the description 'strongly typed language' does *not* have any
>useful or agreed-upon meaning. The confusion is not simply about
>...

>the basic meaning of the term 'strongly typed language'. In a couple
>of hours, I found eight different and incompatible definitions of
>'strongly typed language':

[snip]

>Some of these are contradictory; some are merely orthogonal. Many
>came from class notes for undergraduate CS courses. Not all of these
>instructors and experts can be mistaken. The only sensible conclusion
>is that there is no agreement about what it means to be a strongly
>typed language.

>Examples are no help here [paraphrased by bva]:
>I found several pages that asserted that C|C++|Lisp| Perl is a
>strongly-typed language.
>I found several pages that asserted that C|C++|Lisp| Perl is a
>weakly-typed language.

>It now appears to me that the topic under discussion, whether Perl is
>a strongly or a weakly typed language, is as meaningless as the
>related question of whether Perl sux or roolz,

Like, ew.

>so I will withdraw from
>the discussion.

Mark-Jason --many thanks for turning your list-debate energy toward
the above research.

As a professional programmer, but not from a college-trained comp-sci
background, I am used to being confounded by discussions about
strong|weak typing, thinking that's one of those classes I didn't
take. I just jump to what the language expects, when I need to hint
or coerce, how variables deal with typing, etc. Thanks to you, I
don't feel so ignorant, both because I see that assertions like "XXX
is a strongly-typed language" are meaningless out of context, and
because your list of eight definitions enriches my understanding of
when and how typing can come into play.

Thanks for your service to the Perl community...

Cheers.

1;

Brian Raiter

unread,
Jun 23, 2001, 3:44:59 PM6/23/01
to
> From a brief survey of web documents about strong typing, it appears
> that the description 'strongly typed language' does *not* have any
> useful or agreed-upon meaning. The confusion is not simply about
> where to draw a line on a linear scale of type granularity or degree
> of error checking or anything like that; it's not that some people
> think a strongly-typed language needs fifty types and others think it
> needs only thirty. There appears to be a fundamental confusion about
> the basic meaning of the term 'strongly typed language'.

I think it's somewhat similar to the pornography/erotica distinction;
it depends on what the speaker likes.

FWIW, the only language I've used that has been clearly labelled as
strongly typed is Pascal. From this I unconsciously inferred that this
selection:

> 6. A language is strongly typed if there is no language-level
> way to disable or evade the type system. If there are
> casts or other type-evasive mechansisms, it is weakly typed.

was the closest to the meaning. But that's just another opinion.

Although, don't forget that Bliss and Ada are traditionally considered
strongly typed languages, due to the classic Real Programmers article:

"Real Programmers don't write in Pascal, or BLISS, or Ada, or any of
those pinko computer science languages. Strong typing is for people
with weak memories."

b

Keith Thompson

unread,
Jun 24, 2001, 5:04:05 AM6/24/01
to
Mark-Jason Dominus <m...@plover.com> writes:
[...]

> All I said was that if Perl is considered strongly typed, then I don't
> think the concept is useful or meaningful, because *every* language is
> strongly typed.

First, I agree that the phrase "strongly typed" is weakly defined.
But let me offer yet another possible definition that, in some
hypothetical universe, implies that both Perl and Ada are more
strongly typed than C (picking C and Ada as examples of weakly and
strongly typed languages).

A language is strongly typed if it avoids type errors. Ada avoids
type errors largely by detecting and rejecting them during
compilation. Perl avoids type errors largely by doing implicit type
conversions at run time (e.g., "$scalar = @array;" setting $scalar to
the length of @array). C, in many cases -- especially pre-ANSI C --
doesn't avoid type errors; it allows (some) type-inconsistent
assignments to take place without checking either at compile time or
at run time.

--
Keith Thompson (The_Other_Keith) k...@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.

Reini Urban

unread,
Jun 25, 2001, 4:07:14 AM6/25/01
to
>In article <m1y9qk9x...@halfdome.holdit.com>,
>Randal L. Schwartz <mer...@stonehenge.com> wrote:

nonsense.

if Perl would have been strongly typed it would be possible to write an
optimizing compiler which knows about types at compile time, and not as
now at run-time. Even Lisp-like languages with a similar weakly-typed
concept allow optionally strong type declarations to bind to special and
efficient methods at compile-time.
with perl this is - so far - impossible.

blessing as in the sample below is only for run-time security checks
(which is costly) but not for efficient compilation.

e.g. the lisp counterpart (the <type> <var>), allows checking (if some
compiler pragma enables this), but is mostly a compiler hint to bind it
directly to specialized methods (if enabled). both, for primitive and
non-primitive types.

perl is even weaker than Visual Basic in its native types.
imho one of the weakest languages around. not that bad, just not
properly compilable.

Mark Jason Dominus wrote:
>In article <m1y9qk9x...@halfdome.holdit.com>,
>Randal L. Schwartz <mer...@stonehenge.com> wrote:
>>No, Perl has strong compile-time typing for primitive data types
>>(scalars, arrays, hashes, filehandles),
>
>I cannot imagine what you might mean by this, since each of these is
>converted to the others automatically upon request, and hardly any
>checking is done. If this qualifies as 'strong typing' then I must
>conclude that 'strong typing' no longer has any meaning at all.

correct.

>>C++, by contrast, has strong compile-time typing, but weak run-time
>>typing, because you can cast a pointer to another type and call a
>>totally broken method on it. Crash!
>
>I suppose this is your example, but I don't find it very illuminating,
>because you can do an analogous thing in Perl:
>
> my $randal = PerlExpert->new(NAME => "Randal Schwartz");
> $randal->{number_of_wheels} = 18;
> $randal->{temperature_of_fusion} = "1600 Kelvins";
> bless $randal => 'Adorable_Doggie';
> $randal->wag_tail(); # Totally broken method! Crash!
>
>I don't see how you conclude from this anything about the relative
>run-time typing strength of C++ and Perl.


--
Reini Urban
http://xarch.tu-graz.ac.at/autocad/news/faq/autolisp.html

Rich Lafferty

unread,
Jun 26, 2001, 5:03:40 PM6/26/01
to
In comp.lang.perl.moderated,
Reini Urban <rur...@x-ray.at> wrote:
>
> if Perl would have been strongly typed [...]

> Even Lisp-like languages with a similar weakly-typed [...]

> perl is even weaker than Visual Basic in its native types. [...]

> Mark Jason Dominus wrote:
> >
> >I cannot imagine what you might mean by this, since each of these is
> >converted to the others automatically upon request, and hardly any
> >checking is done. If this qualifies as 'strong typing' then I must
> >conclude that 'strong typing' no longer has any meaning at all.
>
> correct.

Inasmuch as you quote, and appear to agree with, Dominus's remark that
"strong typing" has no meaning at all (for which, I should add, he
presents a strong case), could you clarify what you mean when you
speak of "strongly typed" and "weakly typed" above where you quoted
him?

-Rich

--
Rich Lafferty --------------+-----------------------------------------------
Montreal, Quebec, Canada | "Do not expose your LaserWriter to open
http://www.lafferty.ca/ | fire or flame" -- Apple LaserWriter manual
rich+...@lafferty.ca ----+-----------------------------------------------

John Porter

unread,
Jun 26, 2001, 5:33:10 PM6/26/01
to
Mark-Jason Dominus wrote:
> [an exceedingly incisive essay on "strong typing"]

M-J's essay is one of those rare pieces that I have to save
and stash someplace prominent on my web site.

However, I don't agree with the conclusion that the arguments
over whether Perl is strongly typed are therefore pointless.

All it takes is to specify which definition one has in mind
when declaring Perl strongly or weakly typed.


--
John Porter

Reini Urban

unread,
Jul 4, 2001, 10:28:34 AM7/4/01
to
Rich Lafferty wrote:

>In comp.lang.perl.moderated,
>Reini Urban <rur...@x-ray.at> wrote:
>>
>> if Perl would have been strongly typed [...]
>
>> Even Lisp-like languages with a similar weakly-typed [...]
>
>> perl is even weaker than Visual Basic in its native types. [...]
>
>> Mark Jason Dominus wrote:
>> >
>> >I cannot imagine what you might mean by this, since each of these is
>> >converted to the others automatically upon request, and hardly any
>> >checking is done. If this qualifies as 'strong typing' then I must
>> >conclude that 'strong typing' no longer has any meaning at all.
>>
>> correct.
>
>Inasmuch as you quote, and appear to agree with, Dominus's remark that
>"strong typing" has no meaning at all

no. we said that "perl is not strongly typed at all". not that is has
"no meaning".

it does have a very strong meaning and impact, esp. for object and
compiler optimization with perl6 (hopefully). I do work with lisp and
there we have both. strong typing has been added as optional declaration
syntax and library funcs.

>(for which, I should add, he presents a strong case), could you clarify
>what you mean when you speak of "strongly typed" and "weakly typed" above
>where you quoted him?

compile-time typing.

run-time casting will not go away, as long as the actual interpreter
handles types as now, but in certain advanced compilation modes it
should not be supported.

types are for safety (debugging) AND performance (release).
the user should be able to choose his compile-time and run-time
settings, between run-time safety checks and convenience ("VB mode") vs
disallowing dynamic type coercions, stack allocated lexvars and subs,
and esp. linked method calls.
--
When C++ is your hammer, everything looks like a thumb. Steven M. Haflich

Mark-Jason Dominus

unread,
Jul 5, 2001, 9:55:00 AM7/5/01
to

Rich Lafferty wrote:
> >Inasmuch as you quote, and appear to agree with, Dominus's remark that
> >"strong typing" has no meaning at all

rur...@x-ray.at (Reini Urban):


> no. we said that "perl is not strongly typed at all". not that is has
> "no meaning".

Since you've decided to speak for me by telling Rich what 'we' said,
please allow me to point out that you are completely wrong.
Not only did I not say the things you attributed to me, but I said the
exact opposite.

In spite of your quotation marks, I *did not say* that "perl is not


strongly typed at all". I said:

My argument wasn't that Perl isn't strongly typed, because I

don't pretend to know what that means. . . . I'm not arguing


with you because I think you're wrong and I think Perl is not
a strongly typed language.

And I *did say* that 'strongly typed language' has no agreed-upon meaning:

From a brief survey of web documents about strong typing, it
appears that the description 'strongly typed language' does

*not* have any useful or agreed-upon meaning. . . . There


appears to be a fundamental confusion about the basic meaning

of the term 'strongly typed language'. In a couple of hours,
I found eight different and incompatible definitions of

'strongly typed language' . . . The only sensible conclusion


is that there is no agreement about what it means to be a
strongly typed language.

So please, next time you want to quote me, don't.

> >could you clarify what you mean when you speak of "strongly typed"
> >and "weakly typed" above where you quoted him?
>
> compile-time typing.

If this is what you think 'strongly-typed language' means, then you
are an adherent of definition #2 of 8; most people disagree with you
about what 'strongly-typed language' means. Most people think it
means something else.

I suggest that in the future if you want to talk about compile-time
type checking you avoid the ambiguous phrase 'strongly typed' and
simply say 'compile-time typed'. It will cause less misunderstanding.

Chris Stith

unread,
Jul 5, 2001, 3:32:33 PM7/5/01
to
In comp.lang.perl.moderated Mark Jason Dominus <m...@plover.com> wrote:
> In article <m1y9qk9x...@halfdome.holdit.com>,
> Randal L. Schwartz <mer...@stonehenge.com> wrote:
>>No, Perl has strong compile-time typing for primitive data types
>>(scalars, arrays, hashes, filehandles),

> I cannot imagine what you might mean by this, since each of these is
> converted to the others automatically upon request, and hardly any
> checking is done. If this qualifies as 'strong typing' then I must
> conclude that 'strong typing' no longer has any meaning at all.

MJD doesn't even mention here that Perl, although it has type checks
for the type of aggregate, has no issue with casting numeric and string
data back and forth -- in fact, it welcomes and fully supports the
conversion. Many would say that 'strongly typed' has something to do
with placing data in a variable which only accepts data of one nature.
In this respect, even C is more strongly typed.

On the other hand, Perl does pretty good type checking on hashes v.
arrays v. scalars at some points. It does treat all of these as
flat lists at other times, though.

My personal opinion is that to call Perl weakly typed or strongly typed,
you must explain your point of view. As in many languages, Perl does
not ride one side of a line or the other. Both Perl and the idea of
type strength are complex topics, and it makes little sense to me to
classify Perl solidly on one side or the other of this classification.

Chris

--
Disclaimer: Actual product may not resemble picture in ad in any way.

0 new messages