On Apr 15, 2008, at 10:30, perl pra wrote:
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.
snip
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;
}

}
snip

You have two choices: use a hash reference from the start or take a reference of the hash at the time you call bless:

#reference from the start
sub new {
    my $class = shift;
    my $file  = shift;
    my $self  = {};

    open my $FH, '<', $file
        or die "could not open $file: $!";

    while(<$FH>) {
        next unless my ($key, $val) = /^(.\S+)\s*=\s*(.*)$/;
        $self->{$key} = $val;
    }
    return bless $self, $class;
}

#take a reference at bless time
sub new {
    my $class = shift;
    my $file  = shift;
    my %self;

    open my $FH, '<', $file
        or die "could not open $file: $!";

    while(<$FH>) {
        next unless my ($key, $val) = /^(.\S+)\s*=\s*(.*)$/;
        $self{$key} = $val;
    }
    return bless \%self, $class;
}

--
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/


Reply via email to