How to know datatype of a variable in perl ?

G

Googy

Hi,

I am writing a program using AutoLoader and it has to simulate 10's of
sub routines, the problem I have is every simulated function has
different number of arguments and different number of argument types in
different orders.

Is there any method or sub routines to know the data type of the
argument ?

I know there are functions like is_array, is_hash, is_object, ... and
ref (), but I think these are only for reference types(please correct
me if I am wrong), simply put I need a routine which can tell the data
type of a variable like array, hash, vector.

Thanks.
 
B

Brian McCauley

Hi,

I am writing a program using AutoLoader and it has to simulate 10's of
sub routines, the problem I have is every simulated function has
different number of arguments and different number of argument types in
different orders.

Is there any method or sub routines to know the data type of the
argument ?

The arguments of a Perl subroutine (from the callee's perspective) are
always scalars.

Using a prototype you can make them something else from the _caller's_
perspective. But a prototype needs to be known at compile time. Even if
a function's body is constructed on the fly at run time by an AUTOLOAD
mechainsm the function's prototype needs to be declared ahead of time.

(The story may be a little different for XS.)
I know there are functions like is_array, is_hash, is_object, ... and
ref (), but I think these are only for reference types(please correct
me if I am wrong), simply put I need a routine which can tell the data
type of a variable like array, hash, vector.

The only way I know of that you could pass a variable of abiguous type
to the desired routine is by passing a refernence to the variable.
Given this fact, the problem you are asking us to solve reduces to the
one you already know how to solve.
 
G

Guest

Hi,

I am writing a program using AutoLoader and it has to simulate 10's of
sub routines, the problem I have is every simulated function has
different number of arguments and different number of argument types in
different orders.

Is there any method or sub routines to know the data type of the
argument ?

I know there are functions like is_array, is_hash, is_object, ... and
ref (), but I think these are only for reference types(please correct
me if I am wrong), simply put I need a routine which can tell the data
type of a variable like array, hash, vector.

Thanks.

You would probably need to pass an array or hash by reference, so ref()
should do the job. If by 'vector,' you mean a Bit::Vector object, ref()
will also do.

Arrays and hashes need to be passed to subroutines by reference since
Perl loses the structure of arrays and hashes that are passed directly.
 
A

anno4000

Googy said:
Hi,

I am writing a program using AutoLoader and it has to simulate 10's of
sub routines, the problem I have is every simulated function has
different number of arguments and different number of argument types in
different orders.

Is there any method or sub routines to know the data type of the
argument ?

Normally you don't have to do that when creating a sub through
autoload. Since you know the name of the missing sub, you know
the number and type of args the sub expects. Just create it
accordingly.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top