in reply to Re^2: use strict refs unless you can't figure out the syntax
in thread use strict refs unless you can't figure out the syntax

thank you for the response. How would you do this same thing if you didn't know what the function name was to create a hard reference, the subs exist in a separate file, and you didn't know if the function even existed? (so you have to handle the error, if it hasn't been added yet or it doesn't work correctly) Unfortunately due to my inexperience, I am forced append the file name to the beginning to make a call like.

$function_name = "request_logic::".$request_hash{Logic};

Is there better way is really my question? I am uncertain how to create the reference to this sub and keep the program from dying if it throws some strange error. The $returned_hash_ref is because the sub that gets called alters the original request and I want to make sure the alterations get compared to the original. It might be an obtuse way to do it though, let me think about it...

thank you very much for you responses.

Replies are listed 'Best First'.
Re^4: use strict refs unless you can't figure out the syntax
by GrandFather (Saint) on Jan 08, 2010 at 08:28 UTC

    For some definition of 'better':

    use strict; use warnings; package Something; sub Logic { my ($param) = @_; print "Heh, Logic works$param\n"; } package main; my @names = qw(Logic Illogic); my %dispatch = map {$_ => Something->can ($_)} @names; for my $subName (@names, 'missing') { if (! exists $dispatch{$subName}){ print "Don't know about '$subName'\n"; } elsif ($dispatch{$subName}) { $dispatch{$subName}->('!'); } else { print "Can't call '$subName'\n"; } }

    Prints:

    Heh, Logic works! Can't call 'Illogic' Don't know about 'missing'

    True laziness is hard work