RE: copying structures

2005-03-17 Thread Peter Eisengrein
That helps a lot, thanks. I don't often use references, especially to
anonymous things. I'm used to it being a reference to a "real" hash, like

my $hashref = \%hash;

Thanks for the clarification!




> -Original Message-
> From: Thomas, Mark - BLS CTR [mailto:[EMAIL PROTECTED]
> Sent: Thursday, March 17, 2005 12:52 PM
> To: 'Peter Eisengrein'; perl-win32-users@listserv.ActiveState.com
> Subject: RE: copying structures
> 
> 
> Peter Eisengrein wrote:
> 
> > OK, I sort of get it. But what hash is it a reference to? If 
> > you wanted to
> > access or modify the hash directly where is it?
> 
> You can modify it through either reference. Maybe the 
> following code will
> help you. Other references are 'perldoc perldata', 'perldoc perldsc',
> Learning Perl, etc.
> 
> #---
> 
> my $hashref1 = { foo => 'bar' };
> 
> my $hashref2 = $hashref1;
> 
> # Only the reference is copied
> print $hashref1->{foo}; # prints "bar"
> print $hashref2->{foo}; # prints "bar"
> 
> # You can use either reference to modify the hash
> $hashref2->{foo} = 'baz';
> 
> print $hashref1->{foo}; # prints "baz"
> print $hashref2->{foo}; # prints "baz"
> 
> my %newhash = %$hashref1; #dereferenced, therefore values are copied
> 
> $hashref1->{foo} = 'qux';
> 
> print $hashref1->{foo}; # prints "qux"
> print $hashref2->{foo}; # prints "qux"
> print $newhash{foo}; # still prints "baz"
> 
> #---
> 
> 
> 
> 
> __
> This message was scanned by ATX
> 12:52:33 PM ET - 3/17/2005
> 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Chris Wagner
It's nowhere.  It's anonymous, u can only access it through the reference in
ur data structure.

At 12:33 PM 3/17/05 -0500, Peter Eisengrein wrote:
>OK, I sort of get it. But what hash is it a reference to? If you wanted to
>access or modify the hash directly where is it?







--
REMEMBER THE WORLD TRADE CENTER ---=< WTC 911 >=--
"...ne cede males"

0100

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Thomas, Mark - BLS CTR
Peter Eisengrein wrote:

> OK, I sort of get it. But what hash is it a reference to? If 
> you wanted to
> access or modify the hash directly where is it?

You can modify it through either reference. Maybe the following code will
help you. Other references are 'perldoc perldata', 'perldoc perldsc',
Learning Perl, etc.

#---

my $hashref1 = { foo => 'bar' };

my $hashref2 = $hashref1;

# Only the reference is copied
print $hashref1->{foo}; # prints "bar"
print $hashref2->{foo}; # prints "bar"

# You can use either reference to modify the hash
$hashref2->{foo} = 'baz';

print $hashref1->{foo}; # prints "baz"
print $hashref2->{foo}; # prints "baz"

my %newhash = %$hashref1; #dereferenced, therefore values are copied

$hashref1->{foo} = 'qux';

print $hashref1->{foo}; # prints "qux"
print $hashref2->{foo}; # prints "qux"
print $newhash{foo}; # still prints "baz"

#---

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Peter Eisengrein

> 
> > Its great that it's already done but the bigger question, to 
> > me, is why does it copy a reference in the second example. I 
> > don't get it.
> 
> In the first example, it's a list of scalars. The scalars are 
> copied. In the
> second example, it's a list of anonymous hash references. The 
> references are
> copied. And the scalars they refer to are of course one and the same.


OK, I sort of get it. But what hash is it a reference to? If you wanted to
access or modify the hash directly where is it?
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Thomas, Mark - BLS CTR

> Its great that it's already done but the bigger question, to 
> me, is why does it copy a reference in the second example. I 
> don't get it.

In the first example, it's a list of scalars. The scalars are copied. In the
second example, it's a list of anonymous hash references. The references are
copied. And the scalars they refer to are of course one and the same.

-- 
Mark Thomas 
Internet Systems Architect
___
BAE SYSTEMS Information Technology 
2525 Network Place
Herndon, VA  20171  USA 

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Peter Eisengrein

> Roll your own? This is Perl, remember? It's already done :)

Its great that it's already done but the bigger question, to me, is why does
it copy a reference in the second example. I don't get it.

Confused... 
 
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


Re: copying structures

2005-03-17 Thread Johan Lindstrom
At 15:22 2005-03-17, [EMAIL PROTECTED] wrote:
Is there a module that 'really' copies the values instead of the
references or do I have to do that on my own?
Roll your own? This is Perl, remember? It's already done :)
perldoc Storable
use Storable qw(dclone);
# Deep (recursive) cloning
$cloneref = dclone($ref);
/J
 --  --- -- --  --  - - --  -
Johan LindströmSourcerer @ Boss Casinos   johanl AT DarSerMan.com
Latest bookmark: "TCP Connection Passing"
http://tcpcp.sourceforge.net/
dmoz: /Computers/Programming/Languages/JavaScript/ 12
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: copying structures

2005-03-17 Thread Thomas, Mark - BLS CTR
One way:

  use Clone;
  $bref = clone ([EMAIL PROTECTED]);

or 

  @b = @{ clone ([EMAIL PROTECTED]) };

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


copying structures

2005-03-17 Thread gerhard . petrowitsch
Hi all,

this is more a general Perl question:
When I do

perl -e "@a=qw(a b c); @[EMAIL PROTECTED]; $b[1] = 'v'; print $a[1]"
I get
b
as output

bus when I do
perl -e "$a[1]{var} = 1; @[EMAIL PROTECTED]; $b[1]{var} = 2; print $a[1]{var}"
I get
2
as output

So obviously, which I copy a simple array, I get a real copy
of the values, but when I copy a structure (like an array of hashes
as in the second example), I get the hash references in the array
copied instead of all the key and value pairs, right?

Is there a module that 'really' copies the values instead of the
references or do I have to do that on my own?

Thanks and regards,
Gerhard

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs