Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Referencing a hash of hashes

by imp (Priest)
on Aug 18, 2006 at 13:03 UTC ( #568133=note: print w/replies, xml ) Need Help??

in reply to Referencing a hash of hashes

This is because you are storing the name of the subroutine in your hash, when you should be storing a reference to the subroutine.
my %scripts = ( "010" => { sub => \&ACC_GET_STEP, step => '010', }, "020" => { sub => \&ACC_PUT_STEP, step => '020', }, "030" => { sub => \&ACC_GET_STEP, step => '030', }, );
You can then call the sub like this:
$sub = $scripts{$script}{sub} ; $sub->($file, $step); # Or &$sub($file, $step);
The first way of doing it looks cleaner and and running subs with a & prefix is a bad habit as it has magic when no arguments are passed.

Replies are listed 'Best First'.
Re^2: Referencing a hash of hashes
by Ronnie (Scribe) on Aug 18, 2006 at 15:07 UTC
    Thanks I've implemented your suggestion and am using the tidy -> version. Can I be a pest and ask one further question? (Okay it's really 2 if you count the request!) For audit trail purposes the line -
    print "\n\tRunning Step :: $step Sub :: $sub\n" ;

    is to be written to a log file for each subroutine run. The $sub value is a reference to a subroutine and I've tried various ways to dereference this into plain text without success - I could be even more tedious and add an entry to the hash that is just text holding the subroutines name. I've been reading pages 251 onwards in Programming Perl and I've had plenty of messages :-
    <***** xxrc_test_step_subs S T A R T S *****> Not a SCALAR reference at line 206. rcruickshank@heather$ <***** xxrc_test_step_subs S T A R T S *****> Not a HASH reference at line 206.

    but no success! I thought it was something to do with a coderef but I've not yet found an example that works. Sorry to have be a pest.
      You can get this information from B::Deparse.
      sub a { print "foo\n"; } use B::Deparse; my $deparser = B::Deparse->new('-p'); print $deparser->coderef2text(\&a);
      Update - You can get the source of the subroutine with B::Deparse I should have said. Perhaps you meant the name of the subroutine.
        Please note that the code in this post is not the recommended method. It makes uses of B, which is the perl compiler backend. As such it is a little too magical to be used in production code.

        That said, it is interesting to know how to do this, and can be useful at times. This is one way to get the name of a subroutine.

        sub a { print "foo\n"; } use B; # Fetch the internal object for this coderef my $obj = B::svref_2object(\&a); # Fetch the glob for that object my $gv = B::CV::GV($obj); print $gv->NAME;
        My update WAS misleading - it's only the name of the subsoutine that I need.
        Cheers, Ronnie

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://568133]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-10-28 10:07 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (260 votes). Check out past polls.