Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using references within a function

Status
Not open for further replies.

rupertInAust

Programmer
Apr 21, 2006
4
AU
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]
 
Don't you need to be checking @_[0] rather than just @_ in your sub?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
You are spot on. replaced
[tt]@_[/tt] with [tt]$_[0][/tt]
and worked perfectly. thanks so much.
 
this
Code:
my $ref = \%coins;
detAndPnt($ref);
means that you are passing to the subroutine a refernce to a hash or a reference to a whatever.
So in your subroutine you must check if what you take in is a reference to something .
so notice the red spots.
Code:
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{
      [red]my $foo = shift;[/red]
      if (ref([red]$foo[/red]) eq 'HASH'){
         print "it's a hash ref:";
         print [red]$foo[/red]."\n";
      }
      elsif (ref([red]$foo[/red]) eq 'ARRAY'){
         print "it's a array ref:";
         print [red]$foo[/red]."\n";
      }
      else{
         if(defined([red]$foo[/red])){
            print "defined:";
            print [red]$foo[/red]."\n";
         }
         else{
            print "it's undef\n";
         }
      }
   }


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
I haven't used shift much but that's a good application for it. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top