Strong Typing
Written by Nikos Vaggalis   
Thursday, 18 November 2010
Article Index
Strong Typing
Using conversion classes
Perl
Casting
Implicit conversion
Altering representation

 

 

 

Perl weakly typed?

Perl has three fundamental data types, the scalar, the array and the hash (an array and hash would be considered a data structure in another language, but in Perl they are fundamental types) and all of them can be used in a weakly typed fashion by being implicitly converted depending on the context used.

There are two main contexts scalar and list; the scalar is further subdivided into string, numeric and boolean.

For example , the + operator works in scalar and numeric context:

$a=@a+@b;

and the . operator works in scalar and string context :

$a=@a.@b;

Where a list context is required the operand(s) is implicitly converted to a list and where scalar context (in any of its three forms) is required the operand(s) is  implicitly converted to a scalar

For example the map's function signature is :

map EXPR,LIST

where LIST denotes that the function works in list context; hence the argument will be converted into a list literal:

%a=(1,2,3,4);
@b=map { $_} %a;

#@b contains 1,2,3,4

%a is a hash but it got converted (or flattened) into a list because that is required by the function.

Furthermore, the assignment operator works in both contexts, list and scalar, and makes the decision to switch into list mode because this is required by the left operand which is an array (@b). It subsequently gets the data returned from map as a list and returns them as list to the @b array.

Compare this with :

@a=(1,2,3,4,"hi");
$b=map { $_} @a;

#$b contains 5

which is the number of elements of the array

map still returns a list but the assignment operator will now switch into scalar mode and hence will return just a number; the number of elements of map's returned list

That is:

@b=map { $_}  1,2,3,4,5,6,7;

Consider another example :

open( my $FILEHANDLE, '<', 'myfile.txt' );
$a=<$FILEHANDLE>;
#<> returns just one line
open( my $FILEHANDLE, '<', 'myfile.txt' );
@a=<$FILEHANDLE>;
#<> returns the whole file contents

the diamond operator returns different values depending on the context.

 

But the most fundamental example of implicit conversion in Perl is that of passing non-scalar parameters into a subroutine.

Subroutines in Perl flatten/convert their arguments implicitly into a list. For the data structure to be passed in and not get converted it must be passed in by reference.

Perl strongly typed?

While any of the three fundamental data types can be used in either list or scalar context if required by a function, they cannot be used interchangeably, e.g. by using an array in place of a hash:

push %a,@b;

#Type of arg 1 to push must be array (not hash dereference) at 3.pl line 1,

push (1,2,3),@b;

#Type of arg 1 to push must be array (not constant item) at 3.pl line 1,

push $a,@b;

#Type of arg 1 to push must be array (not scalar dereference) at 3.pl line 1,

No implicit conversion here, certainly Perl displayed strong typing properties in this case.

The push function is prototyped to expect an array (push ARRAY,LIST) and will accept only an array; nothing else can be coerced into an array.

Banner

<ASIN:1430227931>

<ASIN:0596000278>

<ASIN:0321496949>



Last Updated ( Friday, 09 November 2012 )