Zadeh has asked for the wisdom of the Perl Monks concerning the following question:
In Perl Best Practices (PBP) section 5.2, there is talk of initializion but only in the context of locals:my $retval = 0; my @list = (); my %hash = ();
"Even if you specifically did want that variable to be undefined, it's better to say so explicitly: ... That way any readers of the code can immediately see that the lack of definition is intentional, rather than wondering whether it's an oversight."What about the general case? Arguably, it's not of much significance in perl compared to other languages since your data will just get the value undef, but I'd rather not have to deal with undef or all the defined() checks on variables. I'd rather just assume, in my own code at least, that something undef indicates a problem somewhere. During a recent code review this issue came up, and a fellow co-worker argued against this practice because:
Outside subroutines, there's a big difference. As package variables, the assignments happen at certain times. For example, you have a package variable that gets assigned in a subroutine. You have no idea when that subroutine is called; it can be called from a BEGIN {} block. Then later, if you have that package variable assigned during a regular block, the values get overriden by my @var = ();. The = () happens during the normal execution time but the my @var exists before the BEGIN block so you end up wiping out everything you put in your package variables this is really bad if you're doing inside-out classes.Putting aside the issue of whether one ought to be using inside-out classes, what do you perl monks find to be the best practice?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Use of uninitialized variables?
by ikegami (Patriarch) on Jun 11, 2008 at 18:30 UTC | |
|
Re: Use of uninitialized variables?
by kyle (Abbot) on Jun 11, 2008 at 16:47 UTC | |
by Anonymous Monk on Jun 11, 2008 at 18:41 UTC | |
by Anonymous Monk on Jun 11, 2008 at 18:51 UTC | |
|
Re: Use of uninitialized variables?
by Herkum (Parson) on Jun 11, 2008 at 17:57 UTC | |
by Zadeh (Beadle) on Jun 11, 2008 at 21:30 UTC | |
by Herkum (Parson) on Jun 12, 2008 at 14:27 UTC | |
|
Re: Use of uninitialized variables?
by GrandFather (Saint) on Jun 11, 2008 at 21:42 UTC | |
by Zadeh (Beadle) on Jun 11, 2008 at 22:40 UTC | |
by GrandFather (Saint) on Jun 11, 2008 at 23:10 UTC | |
by chromatic (Archbishop) on Jun 12, 2008 at 06:39 UTC | |
|
Re: Use of uninitialized variables?
by Glav (Novice) on Jun 11, 2008 at 23:01 UTC | |
by ikegami (Patriarch) on Jun 12, 2008 at 09:07 UTC | |
by Glav (Novice) on Jun 12, 2008 at 16:42 UTC | |
by kyle (Abbot) on Jun 12, 2008 at 17:29 UTC | |
by ikegami (Patriarch) on Jun 12, 2008 at 18:00 UTC | |
by Glav (Novice) on Jun 12, 2008 at 19:31 UTC | |
| |
by j1n3l0 (Friar) on Jun 13, 2008 at 09:24 UTC |