Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell what type of value is in a Perl variable?

Tags:

perl

How do I tell what type of value is in a Perl variable?

$x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).

like image 618
pm100 Avatar asked Nov 13 '09 19:11

pm100


People also ask

How do I know the value type?

To check the type of a variable, you can use the type() function, which takes the variable as an input. Inside this function, you have to pass either the variable name or the value itself. And it will return the variable data type.

What are the three categories of Perl variable?

Perl has three main variable types: scalars, arrays, and hashes. A scalar represents a single value: my $animal = "camel"; my $answer = 42; Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required.

What is $_ in Perl?

The most commonly used special variable is $_, which contains the default input and pattern-searching string. For example, in the following lines − #!/usr/bin/perl foreach ('hickory','dickory','doc') { print $_; print "\n"; }

What does %variable mean in Perl?

Finaly, the Hash variable will precede by sign % and will be used to store sets of key/value pairs. Perl maintains every variable type in a separate namespace. So you can, without fear of conflict, use the same name for a scalar variable, an array, or a hash. This means that $foo and @foo are two different variables.


2 Answers

$x is always a scalar. The hint is the sigil $: any variable (or dereferencing of some other type) starting with $ is a scalar. (See perldoc perldata for more about data types.)

A reference is just a particular type of scalar. The built-in function ref will tell you what kind of reference it is. On the other hand, if you have a blessed reference, ref will only tell you the package name the reference was blessed into, not the actual core type of the data (blessed references can be hashrefs, arrayrefs or other things). You can use Scalar::Util 's reftype will tell you what type of reference it is:

use Scalar::Util qw(reftype);  my $x = bless {}, 'My::Foo'; my $y = { };  print "type of x: " . ref($x) . "\n"; print "type of y: " . ref($y) . "\n"; print "base type of x: " . reftype($x) . "\n"; print "base type of y: " . reftype($y) . "\n"; 

...produces the output:

type of x: My::Foo type of y: HASH base type of x: HASH base type of y: HASH 

For more information about the other types of references (e.g. coderef, arrayref etc), see this question: How can I get Perl's ref() function to return REF, IO, and LVALUE? and perldoc perlref.

Note: You should not use ref to implement code branches with a blessed object (e.g. $ref($a) eq "My::Foo" ? say "is a Foo object" : say "foo not defined";) -- if you need to make any decisions based on the type of a variable, use isa (i.e if ($a->isa("My::Foo") { ... or if ($a->can("foo") { ...). Also see polymorphism.

like image 30
Ether Avatar answered Oct 16 '22 20:10

Ether


ref():

Perl provides the ref() function so that you can check the reference type before dereferencing a reference...

By using the ref() function you can protect program code that dereferences variables from producing errors when the wrong type of reference is used...

like image 151
ctd Avatar answered Oct 16 '22 21:10

ctd