TN wrote:
Help me to understand your explanation of "assigning a hash reference to
a hash."

Considering

my %params = {
    protocol => 2,
    interactive => 1,
    identity_files => [EMAIL PROTECTED],
};

It appears to have an even number of elements like a hash should (since
"=>" works essentially like ",") but the right hand side should be
delimited by () instead of {} for it to be properly assigned as the
value of a hash.  With {} the right hand side is really a hash reference
and has its location as its value which accounts for the error message
"Reference found where even-sized list expected"???


This sounds correct so I am not sure what the question is. Essentially you have to think of the assignment of,


%params =

Such that the right side is taken in list context, rather than any specific type of value. So if you have a single value on the right then it gets set as a key without a corresponding value because that is how it is seen in list context, so when that value is a set of braces,

%params = {};

Then it is a single value in list context (and the reference is stringified to boot), where that value is seen as a hash reference by definition. Where as the parenthesis just reinforce list context on the right side and allow multiple values in a key/value, key/value, key/value... fashion to be assigned to the hash elements.

%params = ( 'key' => 'value', 'key' => 'value' );

So you have two ways to write your code, either the more common:

%params = ( 'protocol' => 2, 'interactive' => 1, 'identity_files' => [EMAIL PROTECTED], );

Or using a hash reference (note assignment is to a scalar):

$params = { 'protocol' => 2, 'interactive' => 1, 'identity_files' => [EMAIL PROTECTED], };

And then when using the variable you would do so such as:

Net::SSH::Perl->new($host, %params);

or,

Net::SSH::Perl->new($host, %$params);

perldoc perlreftut
perldoc perlref

For more on references....

Does this clear things up or muddy the water further?

http://danconia.org


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



Reply via email to