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;
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;