This is exactly why it is better to use references.  When you pass arguments to 
a subroutine or package, whatever you pass is "flattened out" into one list.  
The end result is that if you pass just one array or hash, it will work, 
because when you assign it on the other end, Perl will automatically convert 
that list into a hash for you.
 
The problems start when you try to pass more than just a hash or array.  If you 
try to pass a hash and a scalar, for example, the scalar will get sucked up 
into the list, and Perl will give you a warning about assigning an odd number 
of elements to the hash.  Consider this:
 
MySub(@array1,@array2);
 
sub MySub{
   my (@array1,@array2) = @_;
}
 
In the example above, @array1 will get everything, and @array2 will be empty.
 
Hopefully this gives you a little better understanding of why it is better to 
use references when passing arrays and hashes to subroutines and packages.
 
Oh, and one more thing that will end up saving you so much time and grief:  
 
use strict;
use warnings;
 
They may seem like a pain at first, but they're the best tools you will ever 
have in keeping you from making some of the most common mistakes.

        -----Original Message----- 
        From: Luinrandir [mailto:[EMAIL PROTECTED] 
        Sent: Sat 9/3/2005 12:01 AM 
        To: Perl Beginners List 
        Cc: 
        Subject: Re: Just need a yes or no answer on passing a hash to a package
        
        

        Yeah I was reading about references... AHHHH
        i don't get it...
        I fgured I could just breah the hash into two arrays, pass them and
        reconstruct the hash in the package.. of something like that...
        will read more tomorrow..maybe some sleep will make it all make 
sence!LOL
        thanks
        Lou
        
        
        
        ----- Original Message -----
        From: "Chris Devers" <[EMAIL PROTECTED]>
        To: "Luinrandir" <[EMAIL PROTECTED]>
        Cc: "Perl Beginners List" <beginners@perl.org>
        Sent: Friday, September 02, 2005 8:34 PM
        Subject: Re: Just need a yes or no answer on passing a hash to a package
        
        
        On Fri, 2 Sep 2005, Luinrandir wrote:
        
        > Can I pass a hash to a package?
        
        Yes.
        
        (Hint: passing any complex data around is generally best done with the
        use of references, so that's the area you need to be studying up on.)
        
        

        <snip>
        
        

Reply via email to