on Wed, 12 Jun 2002 13:39:41 GMT, [EMAIL PROTECTED] (David T-G) wrote: > Hi, all -- > > I'm trying to be good and so I use "my $variable" rather than > making them global, and I prefer to not stick little > [potentially-confusing] "my" declarations around through the code > so I declare my vars up front.
But you do realize that in that case they are sort of 'global' anyway. In my opinion, variables should be declared in the smallest possible scope. > While some of them might usefully > be pre-filled, many of them can happily by left declared but > undefined, so things like > > my $foo, $bar, $baz ; > my ($foo, $bar, $baz) = "" ; > > work nicely. No in my opinion. The second line only assigns the empty string to $foo. Both $bar and $baz remain undefined (although declared). > my # vars we will use > ( > $m3u, # file name > $mp3, # disk label > $source,$host, # where we got it > $artist,$disk,$track, # the real data :-) > @allsources, @allhosts, # where we get 'em > $fullpath, # hash key > @working, # stripped copy of $fullpath > %threez, # hash that holds DB record > ) = "" ; > [...] > > with the second line since those vars cause problems later if they > are undefined (even though I set the vars to empty at declaration > time just above) and, more importantly, have to put the hash at > the end or I get an "odd number of elements added to hash" error. Again, only $m3u gets the empty string, the other scalars remain undefined, the array and the hash are empty. If you put your hash first in the list, Perl tries to assign a single empty string to it, and as you know a list assigned to a hash should always have an even number of elements, i.e. (key, value) pairs. > Since the code is meant to be clear and self-documenting, I don't > have to have all of those comments on the right, and would prefer > to just have a nice, polite "my" line listing everything and being > done with it. Is there any way to mix all of these together? For scalars, you can always do: my ($string, $number, $otherstring) = ('', 0, ''); but personally I prefer one variable per 'my', possibly accompanied by a #comment. For arrays and hashes I usually use (although this is not necessary): my @array = (); my %hash = (); to explicitly indicate that I want to start with empty ones. -- felix -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]