> 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. 

This is a matter of personal taste, but they should not be too far 
"up front" ...

> 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 ;

This is not what you think it is!
The code above only declares one lexical variable, $foo. The "my" has 
lower precedence than the comma!

You have to write it like this:

        my ( $foo, $bar, $baz) ;

>   my ($foo, $bar, $baz) = "" ;

This is not what you seem to expect either. This one does declare 
three lexical variables, but only assigns the empty string to the 
first. The other two stay undefined.

If you want to set all to empty strings you need 
        my ($foo, $bar, $baz) = ("", "", "");
or
        my ($foo, $bar, $baz) = ("") x 3;
        # the braces around the empty string ARE important!

> work nicely.  I haven't yet figured out, though, all of the rules for
> arrays and hashes.  In my current script, I have
> 
>   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
>     ) = "" ;

Again, here you only set $m3u to empty string, all other variables 
are undefined.

>   $m3u = $source = $host = "" ;                       # what can't be undef
> 
> 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.
> 
> 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?

You can have several arrays and hashes in a my() declaration. You 
just can't initialize any variables after the first one ... because 
it'll slurp the whole rest of the list on the right hand side :

        my ($x, @a, @b) = (1,2,3,4);
        print "\$x = $x\n" ;
        print "\@a = @a\n" ;
        print "\@b = @b\n" ;

In either case it's not good to write things like
        my ($x, $y) = "";
it's misleading.

Jenda
=========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain
I can't find it.
                                        --- me


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to