CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [goli123] accessing subs from variables: Edit Log



Kenosis
User

Mar 15, 2013, 5:54 PM


Views: 380
Re: [goli123] accessing subs from variables

Did you mean:

Code
use strict; 
use warnings;

my $input;
my %h;
$h{show} = \&show;

while (1) {
chomp( $input = <STDIN> );
$h{$input}->();
}

sub show() {
printf "bla";
}


Remember that you need to chomp off the newline... Here's what's happening:

Code
Use of uninitialized value in subroutine entry at temp3.pl line 10, <STDIN> line 1. 
Can't use string ("") as a subroutine ref while "strict refs" in use at temp3.pl line 10, <STDIN> line 1.


Without the chomp when you enter "show" you're using $h{"show\n"} , but that's associated with an "uninitialized value." Next, Perl was expecting a sub reference, but the associated value was "", and so Perl complained.


(This post was edited by Kenosis on Mar 15, 2013, 6:08 PM)


Edit Log:
Post edited by Kenosis (User) on Mar 15, 2013, 5:58 PM
Post edited by Kenosis (User) on Mar 15, 2013, 5:59 PM
Post edited by Kenosis (User) on Mar 15, 2013, 6:01 PM
Post edited by Kenosis (User) on Mar 15, 2013, 6:08 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives