I have the following sub-routine.
sub Function{
my $ref = \($_[0]);
if(ref($ref) eq 'SCALAR'){
. . .
}
}
I am trying to get it to work such that passing a list, scalar or hash to it, it converts that into a reference and depending upon whether that reference is of type ARRAY, SCALAR or HASH, different actions are performed.
It could be called as
Function(%a)
Function($a)
Function(@a)
How can I make $ref be a reference to whatever is passed to the Function? My current approach isn’t working.
You can use a prototyped function. Although Perl 5 prototypes are a total mess, they can be used quite well in this case. As I understand them the prototype
should solve your problem, and the reference be in
$_[0].The big disadvantage is that the variable you pass has to start with the
$,@, or%character, so you can’t callFunctionwith a constant argument directly:fails, but
works.
Your approach failes, because
@_is a list, and all elements are scalars.