Mark wrote:
>
> I am trying to create a hash where each key is a referance to
> subs. The bellow code produces the expected result, with one
> unexpected side-effect. Why are the subs executed when I only
> declare them ??
>
> <- cut
> #!/usr/local/bin/perl -w
>
>
> %hash=( 1 => funca(),
> 2 => funcb(),
> );
>
>
>
>
> sub funca{
>
> print "a";
> }
>
> sub funcb{
>
> print "b";
> }
>
> <- paste
Hi Mark.
use strict; #always
use warnings; # usually
First of all be clear in your head: each hash /value/ is
supposed to be a reference to a subroutine. The keys that you
have used are '1' and '2', and if all keys are numeric and non-
sparse then you should be using an array.
Your %hash is being assigned with the /results of a call to/
funca() and funcb(). As James says you can supply references to
actual subroutines as values, or you may want to use anonymous
subroutines:
my %hash = (
1 => sub {print 'a'},
2 => sub {print 'b'},
)
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]