user1828300
user1828300

Reputation: 81

Getting an "Use of uninitialized value in string ne at..." even when checking that the value is defined

i have the following piece of code that throws an uninitialized warning in perl

WARNING: "Use of uninitialized value in string ne at ......."

    foreach my $sub (@{$decoded->{subscriptions}})
    {
        print STDERR Data::Dumper::Dumper $sub;

        if (defined $sub and defined $sub->{status} and $sub->{status} ne 'unsubscribed')
        {
            push @subscriptions, $sub;
        }
        else
        {
            my $nowDate = Lib::Time::getSQLTime();
            $nowDate =~ s/ /T/;
            my $expireDate = $sub->{next_charge_at};

            my $subtract = str2time($expireDate) - str2time($nowDate);

            if ($subtract > 0)
            {
                push @subscriptions, $sub;
            }
        }
    }

I'm checking that the value i need is defined but even then it's throwing this warning and I'm not exactly sure why it's doing it.

The value of $sub is:

{
      'source' => 'xxxxxx',
      'billing_system' => 'xxxxxxx',
      '_id' => 'xxxxxxxxxxxxxxxx',
      'status' => 'unsubscribed',
      'userid' => xxxxxx,
      'billing_key_type' => 'msisdn',
      'created' => '2013-06-20T23:02:13',
       'next_charge_at' => 2013-06-21T23:02:13',
      'product' => 'xxxxxx',
      'billing_key' => 'xxxxxxxxxxxxxxx'
};

When i changed the 'ne' to 'eq' it doesn't throw that warning even though for both tests i use the same data to compare the value against.

I also tried printing the value of of $sub->{status} before i do the check and it displays either 'unsubscribed' or 'subscribed' so that value is not empty for each time it does the check.

Can someone help me out with this please? Let me know if you require more info.

Thanks in advance :)

Upvotes: 0

Views: 752

Answers (2)

mpr4ul
mpr4ul

Reputation: 99

comment the Data Dumper and run , sometimes data dumper will derefer/unrefer the object.

Upvotes: 0

ysth
ysth

Reputation: 98398

Is it possible you have an elsif clause? Some warnings in elsif will be reported with the if's line number.

Upvotes: 1

Related Questions