
Kenosis
User
Mar 15, 2013, 5:54 PM
Post #2 of 3
(113 views)
|
|
Re: [goli123] accessing subs from variables
[In reply to]
|
Can't Post
|
|
Did you mean:
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:
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)
|