Front page | perl.beginners |
Postings from April 2008
Re: blessing a hash
Thread Previous
|
Thread Next
From:
perl pra
Date:
April 15, 2008 07:30
Subject:
Re: blessing a hash
Hi Owen,
yeah that true ,but theres little change in the way i build the hash,I build
the hash from variables in the text file.
The below are the datails:
I have a following text file app.config
---------------------------------------------------------------------
app_path=/home/ap/buuild
host=test.per.com
port=3330
-----------------------------------------------------------------------------
I call the new subroutine of the load package with the app.config file
now my package is
package Load;
use strict;
use warnings;
sub new
{
my $class=shift;
my $file=shift;
my %samp;
open my $FH, '<', $file or die "$!";
while(<$FH>)
{
next unless /(.*)=(.*)/
my($key,$val)=split(/=/,$_);
$samp{$key}=$val;
}
}
sub print {
here i need to print $samp{app_path}
}
1;
Thanks In Advance
Siva
On Tue, Apr 15, 2008 at 7:33 PM, Chas. Owens <chas.owens@gmail.com> wrote:
> On Tue, Apr 15, 2008 at 9:54 AM, perl pra <perlpra@gmail.com> 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.
>
Thread Previous
|
Thread Next