Is Perl weak or strongly typed?

I am currently learning Perl 5 from learn.perl.org, and I'm curious about its typing system. I see mixed information about Perl that is weakly or strongly typed, and that it is also dynamically typed. Wikipedia states that it also supports duck printing.

Can anyone confirm their weakly or strongly typed nature?

+6
source share
2 answers

I see mixed information about Perl that is weakly or strongly typed

No wonder. There is also conflicting information about whether C is a strongly typed or weakly typed language.

  • An example of a static but weak typed language is C. [ source ]

  • C is heavily printed ... [ source ]

  • This works great in weakly typed languages ​​like C. [ source ]

    Engines
  • LISP ... are usually written in highly typed languages ​​such as C ... [ source ]

  • In a weakly typed language such as C, ... [ source ]

  • TYPE: highly printed: (C, Algol, Fortran) [ source ]

Mark J Dominus is compiled in the above list, and there are eight incompatible definitions of what strict typing means.

  • A language is strongly typed if type type annotations are associated with variable names and not with values. If types are bound to values, they are weakly typed.

  • A language is strongly typed if it contains compile-time checks for violations of type restrictions . If a check is delayed until runtime, it is weakly typed.

  • A language is strongly typed if it contains compilation or execution checking for constraints such as constraints. If the check fails, it is weakly typed.

  • The language is strongly typed if conversions between different types are prohibited . If such conversions are allowed, it is weakly typed.

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

  • A language is strongly typed if it exists without a language level for disconnecting or evading the type system . If there are throws or other deviating mechanisms, they are weakly typed.

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

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

This shows that “strongly typed” and “weakly typed” meaningless phrases . So the rest of this answer and all the other answers are really just guessing what you are asking.


Is Perl weak or strongly typed?

It has both strong types (scalar, array, hash, etc.) and weak types (string, floating-point number, signed integer, unsigned integer, links, etc.).

This is based on definitions # 1, # 2, # 4, # 5 and # 6 from the list above.


Wikipedia states that it also supports duck printing.

Method calls use dynamic binding (duck printing).

This is unambiguous.

+12
source

If you use the strongly typed definition, it means that you specify what type of data is stored in the variable - perl is not.

Basically, it has one type - a scalar - which can contain many different types of data. Perl determines the correct operation based on how it looks. (It also has a hash and an array - which are groups of scalars)

Because you can:

#!/usr/bin/env perl use strict; use warnings; my $value = 42; #concat 0 onto the end. $value .= 0; print $value,"\n"; #numeric add $value += 1; print $value,"\n"; #division - to create a float. $value = $value / 4; print $value,"\n"; #string replacement on a float. $value =~ s/\.25//g; print $value,"\n"; 

Which implicitly passes $value back and forth between the string and the number, and it just works - I call it weakly typed.

These are operations that ... will not work (as you expect) in a strongly typed language due to type mismatch.

One thing worth mentioning is related to the concept of context . Perl is a context-sensitive language, as it indicates what you mean by context.

So, if you just open and read from a file descriptor:

 open ( my $input, '<', "some_file.txt" ) or die $!; my $line = <$input>; 

This will read a single line from $input , because it reads in a scalar context.

And if you do this:

 my @lines = <$input>; 

Since you are working in a list context, the entire line of the file is read into the @list array.

You can explicitly test the context using wantarray() (which is wrong - it really should be wantlist ):

 #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; sub context_test { if ( wantarray() ) { return ( "some", "results", "here", "(list context)" ); } else { if ( defined wantarray() ) { return "some results (scalar context)"; } else { print "called in a void context\n"; } } } my $first = context_test; print Dumper \$first; my @second = context_test; print Dumper \@second; context_test; 

I would also think that this is an example of weak typing, as perl is trying to figure out the right thing based on context. And sometimes it goes wrong.

So you can:

 print "Elements are:", @second, "\n"; # prints elements from the array. print "Count is", scalar @second,"\n"; #prints number of elements in array. if ( @second > 2 ) { print "Array \@second has more than 2 elements\n"; } 

The reason I mention is that (besides the comment) the context can also be caused by the type of operator.

 $first .= @second; #forces scalar context due to concat operation. 
+5
source

Source: https://habr.com/ru/post/1013038/


All Articles