rupertInAust
Programmer
I tried to create a function that would run an output
based on the ref() function. it works in the main part,
but when I place the routine in a sub, the referencing
stuffs up. any clues? i've included all the code so you
can run it in your own IDE's if it helps. thanks heaps.
the output is;
defined:1
it's a hash ref:HASH(0x14d0e4)
the plan was that they print the same
(the #1 "defined:1" is not what I want,
#2 for both is what i'm aiming for )
and they don't. anybody know what i'm doing wrong?
[tt]
use warnings;
use strict;
my %coins = ( "Quarter" , '25', "Dime" , '10' , "Nickel", '05');
my $ref = \%coins;
detAndPnt($ref);
if (ref($ref) eq 'HASH'){
print "it's a hash ref:";
print $ref."\n";
}
elsif (ref($ref) eq 'ARRAY'){
print "it's a array ref:";
print $ref."\n";
}
else{
if(defined($ref)){
print "defined:";
print $ref."\n";
}
else{
print "it's undef\n";
}
}
#-sub routine--------------------------------
sub detAndPnt{
if (ref(@_) eq 'HASH'){
print "it's a hash ref:";
print @_."\n";
}
elsif (ref(@_) eq 'ARRAY'){
print "it's a array ref:";
print @_."\n";
}
else{
if(defined(@_)){
print "defined:";
print @_."\n";
}
else{
print "it's undef\n";
}
}
}
[/tt]
based on the ref() function. it works in the main part,
but when I place the routine in a sub, the referencing
stuffs up. any clues? i've included all the code so you
can run it in your own IDE's if it helps. thanks heaps.
the output is;
defined:1
it's a hash ref:HASH(0x14d0e4)
the plan was that they print the same
(the #1 "defined:1" is not what I want,
#2 for both is what i'm aiming for )
and they don't. anybody know what i'm doing wrong?
[tt]
use warnings;
use strict;
my %coins = ( "Quarter" , '25', "Dime" , '10' , "Nickel", '05');
my $ref = \%coins;
detAndPnt($ref);
if (ref($ref) eq 'HASH'){
print "it's a hash ref:";
print $ref."\n";
}
elsif (ref($ref) eq 'ARRAY'){
print "it's a array ref:";
print $ref."\n";
}
else{
if(defined($ref)){
print "defined:";
print $ref."\n";
}
else{
print "it's undef\n";
}
}
#-sub routine--------------------------------
sub detAndPnt{
if (ref(@_) eq 'HASH'){
print "it's a hash ref:";
print @_."\n";
}
elsif (ref(@_) eq 'ARRAY'){
print "it's a array ref:";
print @_."\n";
}
else{
if(defined(@_)){
print "defined:";
print @_."\n";
}
else{
print "it's undef\n";
}
}
}
[/tt]