calling subroutine via hash

Discussion in 'Perl Misc' started by John W. Burns, Jan 3, 2006.

  1. 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,28);
    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"){ #note:pCB 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";
    }

    }
     
    John W. Burns, Jan 3, 2006
    #1
    1. Advertising

  2. John W. Burns

    MikeGee Guest

    > my $radius_ref = @data;

    This does not create an arrayref. You want:
    my $radius_ref = \@data;
     
    MikeGee, Jan 3, 2006
    #2
    1. Advertising

  3. John W. Burns

    Matt Garrish Guest

    "John W. Burns" <> 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,28);
    > 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
     
    Matt Garrish, Jan 3, 2006
    #3
  4. 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/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 3, 2006
    #4
  5. Thanks for the help. Your suggestions work.
    JWB
     
    John W. Burns, Jan 5, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    534
    red floyd
    Nov 10, 2011
  2. Srijayanth Sridhar
    Replies:
    19
    Views:
    624
    David A. Black
    Jul 2, 2008
  3. Replies:
    14
    Views:
    249
    Tomi Häsä
    Jan 10, 2005
  4. Alison Bowes
    Replies:
    2
    Views:
    87
    Gunnar Hjalmarsson
    Feb 18, 2005
  5. king
    Replies:
    5
    Views:
    186
Loading...

Share This Page