Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Referencing a hash of hashes

by imp (Priest)
on Aug 18, 2006 at 13:03 UTC ( [id://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 xxrc_test_putstep.pl line 206. rcruickshank@heather$ xxrc_test_putstep.pl <***** xxrc_test_step_subs S T A R T S *****> Not a HASH reference at xxrc_test_putstep.pl 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.
    Cheers
    Ronnie
      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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found