Re: Function to print Object properties
Hi Parag, On Friday 07 January 2011 07:55:38 Parag Kalra wrote: > Anyways I want to know is there any function or a way that would > reveal all the properties of the object. Introspect the symbol table hash (stash): use strict; use warnings; use Data::Dumper; use Foo::Bar; my $fob = Foo::Bar->new(); my $package = ref $fob; { no strict 'refs'; print Dumper \%{"${package}::"}; } Regards, Alan Haggai Alavi. -- The difference makes the difference. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Function to print Object properties
On Thu, 06 Jan 2011 21:31:11 -0500, Shawn H Corey wrote: > On 11-01-06 09:25 PM, Parag Kalra wrote: >> For example if I have a package say Foo::Bar. Now assume that there is >> a object called $fob and pretend that I don't know that its an instance >> of Foo::Bar > > print ref( $fob ), "\n"; > > See `perldoc -f ref`. The officially blessed :-) way to do this is with Scalar::Util: use Scalar::Util qw(blessed); say blessed( $fob ); since this can discriminate between a reference and a blessed reference. Consider the cases of $fob = {}; $fob = bless {}, "HASH"; for instance. -- Peter Scott http://www.perlmedic.com/ http://www.perldebugged.com/ http://www.informit.com/store/product.aspx?isbn=0137001274 http://www.oreillyschool.com/courses/perl3/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Function to print Object properties
Awesome. Thanks Alan and Shawn Cheers, Parag On Thu, Jan 6, 2011 at 6:52 PM, Alan Haggai Alavi wrote: > Hi Parag, > > On Friday 07 January 2011 07:55:38 Parag Kalra wrote: >> Anyways I want to know is there any function or a way that would >> reveal all the properties of the object. > > Introspect the symbol table hash (stash): > > use strict; > use warnings; > > use Data::Dumper; > use Foo::Bar; > > my $fob = Foo::Bar->new(); > my $package = ref $fob; > > { > no strict 'refs'; > print Dumper \%{"${package}::"}; > } > > Regards, > Alan Haggai Alavi. > -- > The difference makes the difference. > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Function to print Object properties
On 11-01-06 09:46 PM, Parag Kalra wrote: Thanks it worked.:) Cheers, Parag You can also print the whole thing out with Data::Dumper use Data::Dumper; if( ref( $fob ) ){ print '$fob ', Dumper $fob; } -- Just my 0.0002 million dollars worth, Shawn Confusion is the first step of understanding. Programming is as much about organization and communication as it is about coding. The secret to great software: Fail early & often. Eliminate software piracy: use only FLOSS. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Function to print Object properties
Thanks it worked. :) Cheers, Parag On Thu, Jan 6, 2011 at 6:31 PM, Shawn H Corey wrote: > On 11-01-06 09:25 PM, Parag Kalra wrote: >> >> For example if I have a package say Foo::Bar. Now assume that there is >> a object called $fob and pretend that I don't know that its an >> instance of Foo::Bar > > print ref( $fob ), "\n"; > > See `perldoc -f ref`. > > > -- > Just my 0.0002 million dollars worth, > Shawn > > Confusion is the first step of understanding. > > Programming is as much about organization and communication > as it is about coding. > > The secret to great software: Fail early & often. > > Eliminate software piracy: use only FLOSS. > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Function to print Object properties
On 11-01-06 09:25 PM, Parag Kalra wrote: For example if I have a package say Foo::Bar. Now assume that there is a object called $fob and pretend that I don't know that its an instance of Foo::Bar print ref( $fob ), "\n"; See `perldoc -f ref`. -- Just my 0.0002 million dollars worth, Shawn Confusion is the first step of understanding. Programming is as much about organization and communication as it is about coding. The secret to great software: Fail early & often. Eliminate software piracy: use only FLOSS. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Function to print Object properties
Hi, This question is related to OOPs in Perl and I am learning and enjoying it gradually. Anyways I want to know is there any function or a way that would reveal all the properties of the object. For example if I have a package say Foo::Bar. Now assume that there is a object called $fob and pretend that I don't know that its an instance of Foo::Bar There are many occasions while reviewing someone else's code that I want to know the object properties like (Package to which it belongs, sub routines I can execute using it etc) So I am looking for some function that can print that. Following is a snippet of my hypothetical idea. EG: use strict; use warnings; use Data::Dumper; use Foo::Bar; my $fob = Foo::Bar->new(); #pretend that I don't know this my %obj_prop = some_function($fob) print Dumper(%obj_prop); # This should print all the details of the function Cheers, Parag -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/