Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

what type of dsc is this

by navalned (Acolyte)
on Oct 07, 2018 at 21:09 UTC ( #1223645=perlquestion: print w/replies, xml ) Need Help??
navalned has asked for the wisdom of the Perl Monks concerning the following question:

I'm playing around with Gtk2 and I have the following output from a Data::Dumper.
I'm not 100% sure what its telling me, so its hard to move forward. My best guess is
an array of blessed hashes, but again not sure.
Thanks in advance.

$VAR1 = [bless( {}, 'Gtk2::Window' ),bless( {}, 'Gtk2::Window' ),bless( {}, 'Gtk2::Window' )];

Replies are listed 'Best First'.
Re: what type of datastructure is this
by LanX (Archbishop) on Oct 07, 2018 at 21:29 UTC
    > My best guess is an array of blessed hashes

    Well yes, an array of 3 different widget objects of class Gtk2::Window.

    Data::Dumper is trying to represent the input as a string, which can be reversed by evaling it again. (quote: "stringified perl data structures, suitable for both printing and eval" )

    Since the name of an object constructor is not fixed in Perl("->new" is only a convention) using bless is the most accurate way to do it.

    edit

    I just checked there is indeed a new constructor for Gtk2::Window :

    http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/Window.html#widget_Gtk2_Window_n

    So it might be possible to represent the same information with

    $VAR1 = [ Gtk2::Window->new(), Gtk2::Window->new(), Gtk2::Window->new() ]

    but to be sure me as a human would need to look up the implementation. Data::Dumper as a piece of code can't do this.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    PS: Not sure what you meant with DSC, in perldsc this stands for Data Structure Cookbook..

      Further to LanX's comments, Data::Dumper knows about the data but not the code that generated it, hence the bless calls instead of method calls to new. This is the simplest way of recreating blessed objects after serialisation.

      I assume that the reason there is nothing inside the hashes is that the Gtk2 objects are external data that cannot be accessed by Data::Dumper (or the debugger). Such empty hashes are safer for serialisation and later reconstruction. I've hit issues in the distant past with Gtk objects reconstructed from Storable files. These caused segmentation faults when accessed, presumably because the contents no longer exist at the stored address. Needless to say I don't store references to the Gtk objects in the structures that are to be serialised.

Re: what type of dsc is this
by Anonymous Monk on Oct 08, 2018 at 09:47 UTC

    What you see is one of the ways to hide a pointer to an opaque C structure in a Perl object returned to the library user. (Use Dump on an Image::Magick object to see a different one.) As others have already pointed out, recreating these objects from such dumps is probably a bad idea because the pointer that's stored somewhere is no longer valid (or, if it is, destroying the recreated object will also free the memory belonging to the original).

    See http://gtk2-perl.sourceforge.net/doc/binding_howto.pod.html for more information on how it is achieved and perlxstut for more information about the language used to extend Perl with code written in C.

      All true, but I don't think the OP is attempting to serialize XS objects, he's trying to inspect structures while "playing around"

      No need to dive into Perl 's XS guts then.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: what type of dsc is this
by dbuckhal (Friar) on Oct 09, 2018 at 00:12 UTC
    My best guess is an array of blessed hashes...

    Agreed... and a little snippet that might be helpful?

    perl -Mstrict -MData::Dumper -we ' package Test; sub new { my $class = shift; my $stuff = {}; bless $stuff, $class; return $stuff; } package Main; my $t = [ Test->new("foo"), Test->new("bar"), Test->new("baz"), ]; print "Ref: " . ref($t) . "\n"; print Data::Dumper->Dump([$t]) . "\n"; ' __output__ Ref: ARRAY $VAR1 = [ bless( {}, 'Test' ), bless( {}, 'Test' ), bless( {}, 'Test' ) ];
      Suggestion, it's even more helpful if you put the passed arguments into $stuff. :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        I thought of that, but did not in order to leave my references empty, similar to the OP's. But, if you insist. :)

        perl -Mstrict -MData::Dumper -we ' package Test; sub new { my $class = shift; my $stuff = { shift() => 1}; bless $stuff, $class; return $stuff; } package Main; my $t = [ Test->new("foo"), Test->new("bar"), Test->new("baz"), ]; print "Ref: " . ref($t) . "\n"; print Data::Dumper->Dump([$t], ["for_LanX"]) . "\n"; ' __output__ Ref: ARRAY $for_LanX = [ bless( { 'foo' => 1 }, 'Test' ), bless( { 'bar' => 1 }, 'Test' ), bless( { 'baz' => 1 }, 'Test' ) ];
Re: what type of dsc is this
by Anonymous Monk on Oct 11, 2018 at 03:27 UTC

    GTK2 is a C library and Perl can't really introspect C objects so, yeah, it's a C object that you can use from Perl. As far as what the output from the Dumper means, bless says it's an object and Gtk2::Window tells you the type but, because it's a C object, you can't really drill any deeper here.

Re: what type of dsc is this
by navalned (Acolyte) on Oct 19, 2018 at 01:10 UTC

    Just wanted to thank everyone for their insight. I haven't had much time to play around with this code. I am trying to use Gtk2::WindowGroup to keep track of a bunch of toplevel windows. There is a list_windows() method that isn't documented well so I was trying to see what I could do with it.

    my @windows = $group->list_windows();

    I need to get at the data and hopefully it will do what I need.

    Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-12-15 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many stories does it take before you've heard them all?







    Results (69 votes). Check out past polls.

    Notices?