If you use the definition of 'strongly typed' that means you specify what type of data a variable holds - perl isn't.
It basically has one type - scalar - which can hold a variety of different data types. Perl infers correctness of operation based on what it 'looks like'. (It also has hash and 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 casts $value
back and forth between string and number, and it 'just works' - I call it weakly typed.
These are operations that ... wouldn't work (the way you expect) in a strongly typed language because of the type mismatching.
One thing perhaps worth mentioning as related is the notion of context. Perl is a context sensitive language, in that it infers what you mean from the context.
So if you simply open and read from a file handle:
open ( my $input, '<', "some_file.txt" ) or die $!;
my $line = <$input>;
This will read a single line from $input
because it's doing a read in a scalar context.
And if you instead do:
my @lines = <$input>;
Because you're working in a list context, the entirelty of the file is read into the array @list
.
You can explicitly test context using wantarray()
(which is a misnomer - it should really 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 I think also call this an example of weak typing, in that perl tries to figure out the right thing to do, based on context. And sometimes it does get this 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 it is (aside from a comment) context may also be coerced by operator type.
$first .= @second; #forces scalar context due to concat operation.