in reply to read a hash from a file

You seem to be using some modules to do your work. Could you please specify which they are. The code below is just rephrasing your snippet and loads the data into a hash.

use strict; use warnings; use Data::Dumper; my %data = map { s/#.*//; # remove comments s/^\s+//; # remove leading white space s/\s+$//; # remove trailing white space m/(.*?)\s*=\s*(.*)/; # split on =, ignoring surrounding spaces } <DATA>; print Dumper(%data); __DATA__ NAME=John Smith AGE=15 RANK=Tier 1 URL=http://somesite.com

What is missing is probably something like my $record = REC->new( %data ); but I cannot test this hypothesis as I don't where REC comes from. Hope this helps...

Replies are listed 'Best First'.
Re^2: read a hash from a file
by Anonymous Monk on Apr 16, 2013 at 06:46 UTC
    I'm using File::Slurp
    #!/usr/bin/perl use strict; use warnings; use File::Slurp; use Data::Dumper; $file="config.txt"; my %record =map{ s/#.*//; s/^\s+//; s/\s+$//; m/(.*?)\s*=\s*(.*)/; } read_file($file); print Dumper (\%record);
    The dump from my code is
    $VAR1={ 'Name' =>John Smith', 'URL' => '15', 'RANK' => 'Tier 1', 'URL' => 'http://somesite.com' };
    but when the data is in the code, the dump is
    $VAR1= bless( { 'Name' =>John Smith', 'URL' => '15', 'RANK' => 'Tier 1', 'URL' => 'http://somesite.com' }, 'REC' );

      You are also having REC->new which must be provided by some module. Which one is that?

        opps. Thanks for pointing that out. The code pulls is a few custom libs written by my predecessor that I did't think were needed for this issue. Looks like they were.
        sub new { my ($self, %args) = @_; my @required = qw(NAME AGE RANK URL); my %cfg = map {uc $_ => $args{$_}} keys %args; for my $req (qw(NAME AGE RANK URL)) { croak "Required argument $req not found" unless exists $cfg{$req}; } return bless \%cfg, $self; }