in reply to Re: Perl Style: Is initializing variables considered taboo?
in thread Perl Style: Is initializing variables considered taboo?

I don't understand ... Why you are restricting this behavior to my...?

Every non-initialized variable is not defined.

DB<1> print $a // "undef" undef DB<2> my $a;print $a // "undef" undef DB<3> our $a;print $a // "undef" undef DB<4> $a=1;print $a // "undef" 1
UPDATE:

And in most cases accessing an undefined variable will be caught under use warning;.

DB<1> print $a DB<2> use warnings; print $a Use of uninitialized value $a in print at (eval 6)[/usr/share/perl/5.1 +0/perl5db.pl:638] line 2. .. DB<3> use warnings; $a=undef;print $a Use of uninitialized value $a in print at (eval 9)[/usr/share/perl/5.1 +0/perl5db.pl:638] line 2. .. DB<4> print $a++ 0

Notable exceptions of this warnings are some altering operators like ".=" and "++" and auto-vivication in hashes and arrays.¹

Cheers Rolf

1) anything else?

... of course boolean context including the defined operator won't throw a warning.

Replies are listed 'Best First'.
Re^3: Perl Style: Is initializing variables considered taboo?
by BrowserUk (Patriarch) on Aug 21, 2010 at 16:38 UTC
    Why you are restricting this behavior to my...? Every non-initialized variable is not defined.

    Unless is was used in module you called, or the previous iteration of the loop, or call to sub, or ...

    They may start out as undef, but only for the first use.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      > Unless is was used in module you called, or the previous iteration of the loop, or call to sub, or ...

      ...which describes "already initialized".

      UPDATE:

      IMHO your intention is to control the scope of a variable.

      my is only one mean to achieve this, our, state, local are others.

      Cheers Rolf

        .which describes "already initialized".

        In which case you need to reinitilise them.

        IMHO your intention is to control the scope of a variable.

        You're telling me what my [sic] intent was?

        My intent was to state that initialisation to undef is unnecessary when you use my. Nothing more and nothing less.

        As for you "scoping" missive:

        • our limits visibility, not scope.
        • state persists scope, not limits it.
        • local limits scope temporarially, but not reliably.

          If you localise a var, but then call some sub or method that uses it without localising it, you get screwed over.

        But quite why you feel the need to tell me (wrongly) about these things I don't understand, because I'm pretty sure that you're aware that I know they exist, and what they do. And they have no bearing on what I said.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.