in reply to Re: Is this a severe error?
in thread Is this a severe error?

Dear Monks!
Thanks A LOT for all these posts... I looked in the code and, before all these, I have written:
my ($initial_label, $barrel_region, $start_of_barrel_region, $length_o +f_barrel_region);

Wouldn't this "secure" that the $start_of_barrel_region is defined? Should I write something else?

Replies are listed 'Best First'.
Re^3: Is this a severe error?
by choroba (Cardinal) on Jun 04, 2015 at 17:37 UTC
    Wouldn't this "secure" that the $start_of_barrel_region is defined?
    No, as you can easily verify:
    #! /usr/bin/perl use warnings; use strict; my $x; print defined($x) ? "Defined\n" : "Undefined\n"; print $x;

    my declares a variable, but doesn't make it defined. You have to assign a value to it (other than undef) to make it defined.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re^3: Is this a severe error?
by Laurent_R (Canon) on Jun 04, 2015 at 17:42 UTC
    No, defined is not the same thing as declared.

    With your my (...); statement, you have properly declared these variables, but they are still not defined because you haven't assigned any value to them.

    Update: Oops, choroba was faster than me.

      Ok, I did as told and got:
      $hash_plp_lbl_barrel_region_only{195} is undefined

      So there is no value for the hash if $s is equal to 195 I presume, right?
        Yes. This is most probably right.

        Note that you can check that with two possibly syntax:

        print "value undefined" unless defined $hash_plp_lbl_barrel_region_onl +y{$s};
        or
        print "key does not exist in hash" unless exists $hash_plp_lbl_barrel_ +region_only{$s};
        The difference between the two syntaxes is a bit subtle, but not so complicated.

        The variable $hash{foo} may have several status.

        If the element for key "foo" does not exist at all in the hash, both defined and exists will report a false value.

        If the element for key "foo" exists, but the value for that element is not defined, then exists will return a true value, and defined will report false.

        I hope this is clear, as I said this is somewhat subtle, but it is important in some cases to understand the difference.

        Having said that, it is often not so important, the difference is in many case inexistent, it really depends on how you hash was built in the first place (and how you maintain it).

        Well, I hope my explanations are clear, if they are not, please ask, may be an example might be clearer.