mrityunjaynath has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone ....i have a written a code where i would take up the value form a config.txt file and use those value further. The code is working fine but every time it runs along with the correct output it is also giving some warning like


use of uninitialized value $key in hash element at newperlfile.pl line 85, <$CFILE> line 3


my config.txt file reads like this

chipid::1925
subversid::0001

the code portion is here with and line 85 being marked *****

my $cChipId; my $cSubVersId; my %data_ids; my $readline; open (my $CFILE,"config.txt" ) || die "couldnt open file: $!"; while ($readline = <$CFILE>) { chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value; ***** } close($CFILE); for my $key (keys %data_ids) { $cChipId = $data_ids{'chipid'}; $cSubVersId = $data_ids{'subversid'}; }
please put some light as i am not able to solve this

Replies are listed 'Best First'.
Re: use of uninitialized value in hash element problem
by 1nickt (Canon) on Jun 26, 2015 at 11:53 UTC

    You are telling the program that when you split your line on '::', you will get two variables. So the program gives you two variables, even if one or both of them are empty. Then when you try to use the variable $key, it complains because $key is "uninitialized."

    This is Perl being helpful since if it did not warn you, you wouldn't know that $key is not defined and that therefore the next line is not going to accomplish what you think.

    Just make the next statement conditional on $key and $value having been initialized and populated. How you check that depends on whether you are concerned that either or both of the variables are defined, or are non-empty, or meet some other requirement.

    UPDATE: removed a bunch of examples that could have led OP in the wrong direction, thx Alexander...

    If you only want to populate your hash when both $key and $value contain text, you could do something like:

    my %data_ids; my $count = 0; while ( # loop through lines from file ) { $count++; chomp $readline; my ($key , $value) = (split /::/, $readline); if (defined $key and $key ne '' and defined $value and $value ne ' +') { $data_ids{$key} = $value; } else { warn "No key-value pair from line $count: $readline\n"; } } # end loop

    Also, you don't say whether you have been given this config file or whether you are building it yourself. If you have control over the file, consider using https://metacpan.org/pod/Config::Tiny which will take a config file in standard .ini format ( key=value ) and return the config to you in a hashref - with no blank lines!

      chomp $readline; my ($key , $value) = (split /::/, $readline); defined $key and $data_ids{$key} = $value;

      Which is the same as saying:

      chomp $readline; my ($key , $value) = (split /::/, $readline); $data_ids{$key} = $value if $key;

      Which is the same as saying:

      chomp $readline; if ( my ($key , $value) = (split /::/, $readline) ) { $data_ids{$key} = $value; }

      Sorry, but that's wrong. The three code variants have similar, but not identical effects:

      The first one stores any defined key (and value) in %data_ids, including the empty string and 0 (zero), but excluding undef. The second one stores only those keys in %data_ids which are true in boolean context. This excludes the empty string, 0, and undef. The third variant stores any key as long as split returns at least one value. This includes the empty string, 0, and undef.

      Also, the scope of $key and $value is limited to the if block in the third variant, but not in the first and second variant.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Agreed. Should not have said "the same as." Tried to oversimplify.

      thanks lnickt....your suggestion helped and now its working fine. yes i have control over config file as i have made it and i will definetly go through the link you have givern me. thanks again

Re: use of uninitialized value in hash element problem
by hippo (Archbishop) on Jun 26, 2015 at 12:23 UTC
    use of uninitialized value $key in hash element at newperlfile.pl line 85, <$CFILE> line 3

    my config.txt file reads like this

    chipid::1925 subversid::0001

    So the warning comes at line 3 of config.txt but you think your file has only 2 lines. The conclusion is that your config.txt file has at least a third line, probably with no content in it. The warning alerts you to that fact and so is very helpful. If you remove the erroneous third line from config.txt the warning should disappear.

Re: use of uninitialized value in hash element problem
by stevieb (Canon) on Jun 26, 2015 at 13:47 UTC

    If you're to use Config::Tiny as 1nickt suggested (and I recommend), here's an example on its use.

    This is a simple example of a basic config file:

    [Database] enable_replication = 0 master_locked = 0 slave_servers = 0 master_source = DBI:mysql:name:master.example.com: master_user = username master_pass = password test_mode_source = DBI:SQLite:t/db_test.db [Logging] verbose = 99

    Code that uses the config:

    #!/usr/bin/perl use warnings; use strict; use Config::Tiny; my $file = 'config.conf'; my $config = Config::Tiny->read($file); # get the master_user for the Database my $db_user = $config->{Database}{master_user}; print "$db_user\n"; # get the logging level my $verbose = $config->{Logging}{verbose}; print "$verbose\n";

    With a config parser module, everything is consistent. You don't have to worry about the amount of whitespace on each side of the delimiter (=) or empty lines in the file, and everything is put into a nice clean hash for you.

    -stevieb