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

Hello,
I am creating a module, for my own use, that will take an object an create an XML document based on the data inside that object, whether the object be a SCALAR, ARRAY, HASH, or an object defined by a user.

I have it set up, so that it starts by taking an object, and then either asking if it has a certain function that returns allow of the methods that the person wants to store data returned from, or checking if the object is a hash, array, or scalar. I have not debugged it yet, and I want to know ahead of time will the code:

my $object = whatever; my $function = whatever; my $returneddata = $object->$function; # with the other option of: my $returnedData = $object->&{ $function };

Does anybody here know which one I should attempt to do, to get the fruits of my wants?

DakeDesu teh Confused Werewolf.
In perl, only those who are bless'd are treated as objects
"Apparent confusion is a product of good Order" -- Sun Tzu (Webpages under construction)

Replies are listed 'Best First'.
(jeffa) Re: Module Data Dump.
by jeffa (Bishop) on Mar 25, 2002 at 22:38 UTC
    If you are wanting to do this to learn, good for you. But if you are wanting to do something right, check out Gisle Aas's Data::DumpXML:
    use strict; use Data::DumpXML qw(dump_xml); my $foo = Foo->new(); dump_xml($foo); package Foo; sub new { return bless { scalar => 'foobar', array => [('a'..'d')], hash => { bar => { baz => 'qux'}}, }, shift; }
    If you want to know what methods a class provids, you can use UNIVERSAL::can():
    package main; my $no = Bar->new(); print "yes\n" if $no->can('do'); package Bar; sub new { bless {},shift } sub do { print "cool\n" }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Module Data Dump.
by IlyaM (Parson) on Mar 25, 2002 at 22:40 UTC
    I am creating a module, for my own use, that will take an object an create an XML document based on the data inside that object, whether the object be a SCALAR, ARRAY, HASH, or an object defined by a user.

    Sounds like XML::Dumper.

    I have it set up, so that it starts by taking an object, and then either asking if it has a certain function that returns allow of the methods that the person wants to store data returned from, or checking if the object is a hash, array, or scalar. I have not debugged it yet, and I want to know ahead of time will the code:

    Not sure I understand the question. Are you asking how to find if object has some method? It is easy:

    my $object = whatever; my $method = whatever; if($object->can($method)) { print "Has method $method\n"; } else { print "Has no method $method\n"; }

    See perldoc UNIVERSAL.

    --
    Ilya Martynov (http://martynov.org/)

      Well, the thing I was asking was how would I call the method defined in the variable. I already have the code testing with UNIVERSAL::can. I think I have just about figured it out:

      my $data = &{$object->$method}; # do stuff with Data...

      I will check out those modules though, cause there are several high chances that they will do this better than me.

      Sorry about how short my last message was, I was running out of time on a public terminal.

      DakeDesu teh Confused Werewolf.
      In perl, only those who are bless'd are treated as objects
      (Webpages under construction)