Re: Storing hashes in Apache::ASP

2000-01-06 Thread David Mitchell

>   I've been trying to store a hash in a session variable, using
> code like that appended below.  Call me a doofus, but I can't
> seem to get it to work.  Can anybody tell me what I'm doing wrong?

You have to store a reference to the hash, ie

$Session->{Stuff} = \%stuff;
not
$Session->{Stuff} = %stuff;
> 
>   Thanks,
>   Bryan
> Example follows:
> --
> 
> 
> 
> <%
> %stuff = ( a => '1', b => '2', c => '3' );
> 
> $Session->{Stuff} = %stuff;
> 
> $Response->Write ( "Results --\n" );
> for $i (keys %{$Session->{Stuff}} ) {
> $Response->Write ( "$i: $Session->{Stuff}{$i}\n" );
> }
> 
> %>
> 
> 
> 
> 
> -- 
> 
===
> Bryan Wright|"If you take cranberries and stew them like 
> Physics Department  | applesauce, they taste much more like prunes 
> University of Virginia  | than rhubarb does."  --  Groucho 
> Charlottesville, VA  22901  | 
> (804) 924-7218  | [EMAIL PROTECTED]
> 
===
> 
> 



Re: Storing hashes in Apache::ASP

2000-01-06 Thread Eric L. Brine

> You have to store a reference to the hash, ie
> 
> $Session->{Stuff} = \%stuff;
> not
> $Session->{Stuff} = %stuff;


\%stuff is not a reference to a hash, it's a reference to each key and
value in the hash. I don't think you'll ever have to use \ on arrays or
hashes. The only way to get a hash ref is by using the {} operator. e.g.:
{%stuff}  Of course, you know how two copies of the data, so be careful.

ELB

--
Eric L. Brine  |  Chicken: The egg's way of making more eggs.
[EMAIL PROTECTED]  |  Do you always hit the nail on the thumb?
ICQ# 4629314   |  An optimist thinks thorn bushes have roses.



Re: Storing hashes in Apache::ASP

2000-01-06 Thread darren chamberlain

Brian,

How about using a hashref

$stuff = { a => '1', b => '2', c => '3' };

instead of a hash?

darren

Bryan K. Wright ([EMAIL PROTECTED]) wrote:
> Hi folks,
> 
>   I've been trying to store a hash in a session variable, using
> code like that appended below.  Call me a doofus, but I can't
> seem to get it to work.  Can anybody tell me what I'm doing wrong?
> 
>   Thanks,
>   Bryan
> Example follows:
> --
> 
> 
> 
> <%
> %stuff = ( a => '1', b => '2', c => '3' );
> 
> $Session->{Stuff} = %stuff;
> 
> $Response->Write ( "Results --\n" );
> for $i (keys %{$Session->{Stuff}} ) {
> $Response->Write ( "$i: $Session->{Stuff}{$i}\n" );
> }
> 
> %>
> 
> 
> 
> 
> -- 
> ===
> Bryan Wright|"If you take cranberries and stew them like 
> Physics Department  | applesauce, they taste much more like prunes 
> University of Virginia  | than rhubarb does."  --  Groucho 
> Charlottesville, VA  22901  | 
> (804) 924-7218  | [EMAIL PROTECTED]
> ===
> 

-- 
vi, vi, vi - the Editor of the Beast



Re: Storing hashes in Apache::ASP

2000-01-06 Thread David Mitchell

> > You have to store a reference to the hash, ie
> > 
> > $Session->{Stuff} = \%stuff;
> > not
> > $Session->{Stuff} = %stuff;
> 
> 
> \%stuff is not a reference to a hash, it's a reference to each key and
> value in the hash. I don't think you'll ever have to use \ on arrays or
> hashes. The only way to get a hash ref is by using the {} operator. e.g.:
> {%stuff}  Of course, you know how two copies of the data, so be careful.

Er, I beg to differ. \%hash returns a reference to that hash -
see the top of p.246 of the camel book, 2nd ed.
The {} operators are for creating *anonymous* hashes.



Re: Storing hashes in Apache::ASP

2000-01-06 Thread Eric L. Brine



> How about using a hashref
> $stuff = { a => '1', b => '2', c => '3' };
> $Session->{Stuff} = $stuff;
> instead of a hash?

According to the other person, that would fix your problem.
Personally, I know too litte of EmbPerl to be of more help.

btw,
  $Session->{Stuff}{$i}
is the same as
  $Session->{Stuff}->{$i}
so you were already expecting a hashref. I think perllol is the relevant
man page, and you might also want to review perlref.

ELB


Original Problem:
> I've been trying to store a hash in a session variable, using
> code like that appended below.  Call me a doofus, but I can't
> seem to get it to work.  Can anybody tell me what I'm doing wrong?
>
> Example follows:
> 
> <%
> %stuff = ( a => '1', b => '2', c => '3' );
>
> $Session->{Stuff} = %stuff;
>
> $Response->Write ( "Results --\n" );
> for $i (keys %{$Session->{Stuff}} ) {
> $Response->Write ( "$i: $Session->{Stuff}{$i}\n" );
> }
>
> %>
> 

--
Eric L. Brine  |  Chicken: The egg's way of making more eggs.
[EMAIL PROTECTED]  |  Do you always hit the nail on the thumb?
ICQ# 4629314   |  An optimist thinks thorn bushes have roses.



Re: Storing hashes in Apache::ASP

2000-01-06 Thread Eric L. Brine

> Er, I beg to differ. \%hash returns a reference to that hash -
> see the top of p.246 of the camel book, 2nd ed.
> The {} operators are for creating *anonymous* hashes.

I stand corrected. I was thinking of \($a, $b, $c).  I always thought
\@array and \%hash worked the same way, so I never used them.

Sorry,
ELB

test.pl
===
%hash = (a => '1', b => '2', c => '3');
@array = qw(a b c);

@array_ref = \@array;  # returns ref to array
@hash_ref = \%hash;# returns ref to hash
@list_ref = \(@array); # returns ref for each element

{
  local $, = ', ';
  local $\ = "\n";
  print(@array_ref);   # prints: ARRAY(0x655b78)
  print(@hash_ref);# prints: HASH(0x655af4)
  print(@list_ref);# prints:
  # SCALAR(0x6559bc), SCALAR(0x6559d4), SCALAR(0x6559ec)
}


--
Eric L. Brine  |  Chicken: The egg's way of making more eggs.
[EMAIL PROTECTED]  |  Do you always hit the nail on the thumb?
ICQ# 4629314   |  An optimist thinks thorn bushes have roses.



Re: Storing hashes in Apache::ASP

2000-01-06 Thread Matt Sergeant

On Thu, 06 Jan 2000, Eric L. Brine wrote:
> > You have to store a reference to the hash, ie
> > 
> > $Session->{Stuff} = \%stuff;
> > not
> > $Session->{Stuff} = %stuff;
> 
> 
> \%stuff is not a reference to a hash, it's a reference to each key and
> value in the hash. I don't think you'll ever have to use \ on arrays or
> hashes. The only way to get a hash ref is by using the {} operator. e.g.:
> {%stuff}  Of course, you know how two copies of the data, so be careful.

{%stuff} is not a reference to %stuff. It's a reference to a newly created
hash. Be very careful using this syntax, it may not do what you expect it
to do:

%hash = (1 => 'perl');
$a = {%hash};
$hash{2} = 'apache';
print $a->{2};

If $a was a reference to %hash then it would print 'apache'.

-- 


Details: FastNet Software Ltd - XML, Perl, Databases.
Tagline: High Performance Web Solutions
Web Sites: http://come.to/fastnet http://sergeant.org
Available for Consultancy, Contracts and Training.



Re: Storing hashes in Apache::ASP

2000-01-06 Thread Joshua Chamas

David is right here... $Session->{Stuff} = \%stuff is your answer.

--Joshua
_
Joshua Chamas   Chamas Enterprises Inc.
NodeWorks >> free web link monitoring   Huntington Beach, CA  USA 
http://www.nodeworks.com1-714-625-4051

David Mitchell wrote:
> 
> >   I've been trying to store a hash in a session variable, using
> > code like that appended below.  Call me a doofus, but I can't
> > seem to get it to work.  Can anybody tell me what I'm doing wrong?
> 
> You have to store a reference to the hash, ie
> 
> $Session->{Stuff} = \%stuff;
> not
> $Session->{Stuff} = %stuff;
> >
> >   Thanks,
> >   Bryan
> > Example follows:
> > --
> > 
> > 
> > 
> > <%
> > %stuff = ( a => '1', b => '2', c => '3' );
> >
> > $Session->{Stuff} = %stuff;
> >
> > $Response->Write ( "Results --\n" );
> > for $i (keys %{$Session->{Stuff}} ) {
> > $Response->Write ( "$i: $Session->{Stuff}{$i}\n" );
> > }
> >
> > %>
> > 
> > 
> > 
> >
> > --
> >
> ===
> > Bryan Wright|"If you take cranberries and stew them like
> > Physics Department  | applesauce, they taste much more like prunes
> > University of Virginia  | than rhubarb does."  --  Groucho
> > Charlottesville, VA  22901  |
> > (804) 924-7218  | [EMAIL PROTECTED]
> >
> ===
> >
> >