Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

printing an array with references in it

by ozgurp (Beadle)
on Jun 29, 2003 at 07:35 UTC ( [id://269974]=perlquestion: print w/replies, xml ) Need Help??

ozgurp has asked for the wisdom of the Perl Monks concerning the following question:

The following code puts references to sub functions into an array and calls them.
use strict; use warnings; sub a1 { print("a1\n"); } sub a2 { print("a2\n"); } my @procs = (\&a1, \&a2); foreach (@procs) { $_->(); }
If I wanted to print @procs array;
foreach (@procs) { print("$_\n"; } I get: CODE(0x1d4f9ec) CODE(0x1dc40b4)
how can i print the references in the @procs array?

Replies are listed 'Best First'.
Re: printing an array with references in it
by Missing Words (Scribe) on Jun 29, 2003 at 17:56 UTC
    I took tilly's suggestion about looking through the symbol table and came up with the following code:
    my %test_procs; foreach my $sym (sort keys %main::){ if(defined &{$main::{$sym}}){ $test_procs{\&{$main::{$sym}}} = $sym; } } foreach my $sub_ref (keys %test_procs){ foreach(@procs){ print "$test_procs{$sub_ref}\n" if $sub_ref eq $_; } }

    From the small amount of testing I did, it seems to do what he asked for; however, as you can see, the code is fairly messy. I would be interested in seeing how other more experienced monks would improve upon/rewrite this.
    UPDATE: fixed a typo
    Cheers, Missing Words
      foreach my $sub_ref (keys %test_procs){ foreach(@procs){ print "$test_procs{$sub_ref}\n" if $sub_ref eq $_; } }

      This could be:

      foreach(@procs){ print "$test_procs{$_}\n" if $test_procs{$_}; #If it's defined. }
Re: printing an array with references in it
by davorg (Chancellor) on Jun 29, 2003 at 13:26 UTC
    how can i print the references in the @procs array?

    That's exactly what you are doing. CODE(0x1d4f9ec) is the reference to your first subroutine.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: printing an array with references in it
by sgifford (Prior) on Jun 29, 2003 at 08:06 UTC
    You need to be more specific with your question. What is it you want to print? The names of the subs? The code for the subs?
      Sorry for not being specific, I want to print the name of the subs.
        You can't, unless your data structure stores the names somewhere or you want to walk the symbol table, or unless you give it a name in some other way.

        A code reference doesn't know by what name it appears, and cannot since it might be anonymous, or it might have been assigned to multiple names throughout the filesystem. (A common example of that is when you import functions from a module - that function is now both in the module and in your namespace, which is its name?)

        Therefore unless you want to start walking through the symbol table to see if any named function matches the function reference that you have, it doesn't have a name.

        BUT if you really want, what you can do is use bless to give references a name (well it does more, but we can ignore the more), and then with no change in your dumping code, function references will now keep track of the names given. I wouldn't recommend this for serious use, but it could be handy while debugging.

Re: printing an array with references in it
by bart (Canon) on Jun 29, 2003 at 12:38 UTC
      I believe Data::Dumper cannot yet handle code references. It will just display "DUMMY" in place of the code reference.

        then xmaths Data::XDumper might be of help

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://269974]
Approved by allolex
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-03-19 04:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found