Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > calling subroutine via hash

Reply
Thread Tools

calling subroutine via hash

 
 
John W. Burns
Guest
Posts: n/a
 
      01-03-2006
The following code calls only the first subroutine not the second. It seems
to
recoginize both subroutines but executes only the first one. Can
anyone help me figure out how to get the code to call the second subroutine
as
well? Both subroutines when run as standard routines and not part of
a hash work.Thanks
JWB

#!/usr/local/bin/perl
use warnings;
# following example reflects Perl Cookbook 11.4
my $name;
my $key;
my $dispatch;
my $var;

my %dispatch = (
"foo" => sub {
my $x = 3;
my $ans = ($x**2);

print "answer is: $ans \n"; #prints "answer is: 9"

},
"moo" => sub {
my @data = (3,7,15,2;
my $radius;
my $radius_ref = @data;
foreach $radius(@$radius_ref) {

print "my radius is: $radius \n";

my $area = 3.14159 * ( $radius ** 2);
print "and circle area is $area \n";
}

}
);

#Perl PCB 5.2 reference
my $input;
foreach $input("foo", "moo"){ #noteCB uses chomp($input = <STDIN>) in
lieu of "foreach"
if ( exists ${dispatch}{ $input } ) {
${dispatch} {$input}( );
print "$input is the answer.\n";
}
else {
die "Cannot find the subroutine $input\n";
}

}






 
Reply With Quote
 
 
 
 
MikeGee
Guest
Posts: n/a
 
      01-03-2006
> my $radius_ref = @data;

This does not create an arrayref. You want:
my $radius_ref = \@data;

 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      01-03-2006

"John W. Burns" <(E-Mail Removed)> wrote in message
news:wjBuf.5660$tJ1.2933@trndny01...
> The following code calls only the first subroutine not the second. It
> seems
> to
> recoginize both subroutines but executes only the first one. Can
> anyone help me figure out how to get the code to call the second
> subroutine
> as
> well? Both subroutines when run as standard routines and not part of
> a hash work.Thanks


They both run...

>
> #!/usr/local/bin/perl
> use warnings;
> # following example reflects Perl Cookbook 11.4
> my $name;
> my $key;
> my $dispatch;
> my $var;
>
> my %dispatch = (
> "foo" => sub {
> my $x = 3;
> my $ans = ($x**2);
>
> print "answer is: $ans \n"; #prints "answer is: 9"
>
> },
> "moo" => sub {
> my @data = (3,7,15,2;
> my $radius;
> my $radius_ref = @data;


Your problem is here, however. You're assigning the count of @data to the
scalar...

> foreach $radius(@$radius_ref) {


And then trying to call it as an array ref, which it is not Change that
assignment to:

my $radius_ref = \@data;

Or just get rid of this useless assignment altogether and write:

foreach my $radius (@data) {

Matt


 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      01-03-2006
John W. Burns wrote:
> The following code calls only the first subroutine not the second. It seems
> to recoginize both subroutines but executes only the first one. Can
> anyone help me figure out how to get the code to call the second subroutine
> as well?


Yes, you can do that by asking Perl for help: enable strictures!!

use strict;

Please see the posting guidelines for this group:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John W. Burns
Guest
Posts: n/a
 
      01-05-2006
Thanks for the help. Your suggestions work.
JWB


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
variation on 'calling subroutine via reference'? Alison Bowes Perl Misc 2 02-18-2005 07:32 PM
copying values from a hash into CGI.pm via tied hash reference ioneabu@yahoo.com Perl Misc 14 01-10-2005 01:22 PM



Advertisments