> -----Original Message-----
> From: David T-G [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, June 12, 2002 9:40 AM
> To: perl beginners
> Subject: "lazy" variable declaration
> 
> 
> 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.
> 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 does not declare $bar and $baz to be "my" vars. "use strict"
will barf on this. Here the comma is the sequence operator, not
the list separator. You need:

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

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

This only initializes $foo. $bar and $baz are undef. To initialize
all 3, use a list:

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

> 
> 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
>     ) = "" ;
> 
>   $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.

The single initializer value goes only to the first variable.
If the first variable is a hash, you get the warning because
a hash should have an even number of key/value pairs in an
assignment.

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

Try the following:

   my (@foo, @bar) = qw(a b c d);

   print "\@foo is [@foo]\n";
   print "\@bar is [@bar]\n";

This prints:

   @foo is [a b c d]
   @bar is []

Note that @foo "sucked up" all the values in the initializer list.
So there's really no way to initialize @foo to [a b] and @bar to [c d]
in a single statement like this.

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

Reply via email to