Core Function isVarClass
isVarClass( $variable, <type>, <strict> )
Contents |
Description
Check if a variable's object type is a class.
Parameters
variable
The variable to check.
type
Optional; A class name to check for
Default: this is ignored entirely and as long as the variable contains a class of any type it will return true.
strict
Optional; If true the the class type will be required as absolute and will not allow for inherited classes by that name.
Default: false
Return Value
Success: Returns true.
Failure: Returns false.
Remarks
Can be useful to validate array/non-array parameters to user-defined functions.
Example
A simple example to see if a variable is a class variable :
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( isVarClass($cat1) ? "TRUE" : "FALSE" ); println( isVarClass($moo) ? "TRUE" : "FALSE" );
Same example but this time making sure the class is a vec3 and nothing else:
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( isVarClass($cat1, "vec3") ? "TRUE" : "FALSE" ); println( isVarClass($moo, "vec3") ? "TRUE" : "FALSE" );
Alternative way to see if a variable contains a class object by name
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( $cat1 ~~ Vec3 ? "TRUE" : "FALSE" ); println( $moo ~~ Vec3 ? "TRUE" : "FALSE" );
Yet another alternative way to see if a variable contains a class object by name
Class Vec3 { my $x = 0; my $y = 0; my $z = 0; Function Vec3($x1 = 0, $y1 = 0, $z1 = 0) { $this->$x = (double)$x1; $this->$y = (double)$y1; $this->$z = (double)$z1; } }; $cat1 = new Vec3(10, 20, 30); println( $cat1 is Vec3 ? "TRUE" : "FALSE" ); println( $moo is Vec3 ? "TRUE" : "FALSE" );