in reply to use strict refs unless you can't figure out the syntax

Looks like you may be trying to dispatch to a selected sub by storing the name of the sub in a hash. If you store a reference to the sub instead the intent becomes clearer, to need for the string ref is removed, maintenance becomes easier and the code is better checked for typos. Consider (sample code only - unchecked):

my %request_hash = ( my $returned_hash_ref = Logic => \&request_logic::nnnnn, ); #... if ($request_hash{Logic}) { $request_hash{Logic}->($request_hash_ref, $log_file); } else { #...

True laziness is hard work

Replies are listed 'Best First'.
Re^2: use strict refs unless you can't figure out the syntax
by AnomalousMonk (Archbishop) on Jan 07, 2010 at 17:01 UTC

    I must admit I'm confused by the intent of theleftsock (who seems to have chosen a very complicated way to do a fairly simple thing), but I'm also confused by the intent of the extra lexical scalar  $returned_hash_ref present in your reply. What does this give you (other than another maintenance worry) that you do not get from the hash and key you already have in hand?

    >perl -wMstrict -le "sub S { print 'hi ', $_[0] } sub T { print 'lo ', $_[0] } sub U { print '-- ', $_[0] } my %hash = ( my $x = L => \&S, my $y = M => \&T, my $z = N => \&U, ); print $x; $hash{$x}->($x); $hash{L} ->('L'); $hash{$_}->($_) for qw(L M N); " L hi L hi L hi L lo M -- N

      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.

        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

      A misguided "copy/paste/misguided edit" sequence (covered by the parenthesised escape clause maybe?) :-(.


      True laziness is hard work