On Tue, Apr 15, 2008 at 9:54 AM, perl pra <[EMAIL PROTECTED]> wrote: > Hi Gurus, > > How can I bless a hash. > > below is my code: > > package load; > > use strict; > use warnings; > > sub new { > > my $class=shift; > my %samp=('1' => "xxx", '2' => "yyyy"); > > } > > sub print { > > here i need to print $samp{1} > > > } > > > > 1; > > hwo can i print $samp{1} in print subroutine > > Thanks in Advance > > Siva >
The following should work. The bless* function takes a reference to a Perl variable** and a class to bless it into. It returns the blessed object. Also, it is customary (but not required) to name the object $self inside the of the methods. You might want to read perldoc perltoot*. package load; use strict; use warnings; sub new { my $class = shift; my $self = { '1' => "xxx", '2' => "yyyy" }; return bless, $self, $class; } sub print { my $self = shift; print "$_ => $self->{$_}\n" for sort keys %$self; } 1; * http://perldoc.perl.org/functions/bless.html ** Actually all sorts of things can be blessed including functions and filehandles, but that is a trickier subject *** or http://perldoc.perl.org/perltoot.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/