variables
am 26.06.2008 08:26:50 von basil.daoust
Help,
OK - a perl procedure returns a reference to a hash, or at least I think
that what it returns.
datadumper returns the following.
$VAR1 = {
'discrete' => 'text',
'composite' => 'plain',
'attributes' => {
'charset' => 'us-ascii'
}
};
now all I have is:
my $data = parse_content_type($ct);
How to I access $var1{attributes}{charset} as the above returns %data
requires explicit package ...
I know its because I don't fully understand references, help please.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 26.06.2008 08:58:24 von Bill Luebkert
Basil A. Daoust wrote:
> Help,
>
> OK - a perl procedure returns a reference to a hash, or at least I think
> that what it returns.
>
> datadumper returns the following.
> $VAR1 = {
> 'discrete' => 'text',
> 'composite' => 'plain',
> 'attributes' => {
> 'charset' => 'us-ascii'
> }
> };
>
> now all I have is:
> my $data = parse_content_type($ct);
>
> How to I access $var1{attributes}{charset} as the above returns %data
> requires explicit package ...
> I know its because I don't fully understand references, help please.
Example:
use strict;
use warnings;
use Data::Dumper; $Data::Dumper::Indent=1; $Data::Dumper::Sortkeys=1;
my $hashref = hashref_returner (); # get a hashref returned
print Data::Dumper->Dump([$hashref], [qw($hashref)]); # print it out
# just for some exercise using the hashref simulate dumping it:
foreach (sort keys %{$hashref}) { # loop through hash keys
if (ref $hashref->{$_} eq 'HASH') { # deeper loop for hashref
print "$_ => {\n";
my $ref = $hashref->{$_};
foreach (sort keys %{$ref}) { # loop through inner hash
print "\t$_ => $ref->{$_}\n";
}
print "}\n";
next;
}
print "$_ => $hashref->{$_}\n";
}
exit;
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub hashref_returner {
my %hash = ( # let's assume this is the hash
'discrete' => 'text',
'composite' => 'plain',
'attributes' => {
'charset' => 'us-ascii',
}
);
return \%hash; # return ref to hash to caller
}
__END__
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 26.06.2008 10:08:26 von basil.daoust
Thanks Bill, I'm not sure that answered my question, but it kind of did.
Why does $hashref->{attributes}{charset} and
$hashref->{attributes}->{charset} work
and this fails $hashref{attributes}{charset}?
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 26.06.2008 12:09:34 von Ingo Schwarze
Hello Basil,
Basil A. Daoust schrieb am Thu, Jun 26, 2008 at 03:08:26AM -0500:
> Why does $hashref->{attributes}{charset} and
> $hashref->{attributes}->{charset} work
> and this fails $hashref{attributes}{charset}?
The strictest way to write this is:
%{$hashref} is a hash (like %{hash})
${$hashref}{'attributes'} is an element (like ${hash}{'key'})
%{ ${$hashref}{'attributes'} } is a hash
${ ${$hashref}{'attributes'} }{'charset'} is an element
Now, you can abbreviate:
%{$hashref} as %$hashref
${$hashref}{'attributes'} as $$hashref{'attributes'}
as $$hashref{attributes}
or $hashref->{'attributes'}
as $hashref->{attributes}
%{ ${$hashref}{'attributes'} } as %{$$hashref{'attributes'}}
as %{$$hashref{attributes}}
or %{$hashref->{'attributes'}}
as %{$hashref->{attributes}}
${ ${$hashref}{'attributes'} }{'charset'}
as ${ ${$hashref}{attributes} }{charset}
as ${ $$hashref{attributes} }{charset}
or ${ $hashref->{attributes} }{charset}
as $hashref->{attributes}->{charset}
as $hashref->{attributes}{charset}
The last step is:
The arrow '->' is optional between braces, brackets and parantheses.
Note:
$hashref->{attributes} is an element of %{$hashref}
$hashref{attributes} is an element of %hashref
For details, see perlref(1).
This is perl.
There is more than one way to write it up.
There is more than one way to screw it up.
Yours,
Ingo
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: variables
am 26.06.2008 12:13:41 von Brian Raven
Basil A. Daoust <> wrote:
> Thanks Bill, I'm not sure that answered my question, but it kind of
> did. =
> =
> Why does $hashref->{attributes}{charset} and
> $hashref->{attributes}->{charset} work and this fails
> $hashref{attributes}{charset}?
The clue is in the error message that you get because you have 'use
strict;' at the top of all of your code. Something like:
Global symbol "%hashref" requires explicit package name at...
To permit $hashref{attributes} to be a shorthand for
$hashref->{attributes} would be ambiguous, as it already means look up a
key in hash %hashref. The expressions $hashref->{attributes}{charset}
and $hashref->{attributes}->{charset} are not ambiguous because the
result of $hashref->{attributes} (or indeed $hashref{attributes} if
%hashref exists) can only be a scalar, in this case a hash ref.
Personally I always use the '->' so that I don't have to remember where
it can be omitted.
HTH
-- =
Brian Raven =
==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================
The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =
Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.
L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.
Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 26.06.2008 16:12:00 von geoff
Perl references are really cool things, you just need to play with the
concept to 'get' it.
Declare a variable. Say it's $foo.
Set $foo = 'bar';
Now if you print "$foo"; you'll see "bar" printed out. I'm not going
to show you this; I assume you already know.
Now try this:
C:\>perl
$foo='bar';
$ref = \$foo;
print "$ref\n";
print "$$ref\n";
When you run the program, you'll see something like this:
SCALAR(0x325bc0)
bar
You see, $ref is set to the value of the space in memory where Perl is
storing the value of $foo. The \ sigil tells Perl to return the
address where the $foo variable is stored. So when you print out
$ref, you get the Perl-ish representation of "where $foo is".
The final line of the code is interesting: it says: give me the SCALAR
that is located where you are storing $foo. That's the double $ at
work there. The first $ sigil is telling Perl how to interpret the
value at SCALAR(0x325bc0).
Now let's talk about your particular issue.
Your subroutine is returning a reference, this time to a hash.
Imagine for a moment that internally, the parse_content_type() sub is
methodically doing whatever it does, and placing the results into
%hash.
At the end of the sub, there is a line that looks like this:
return(\%hash);
The \ sigil is telling Perl: give me the address where %hash is
located. (It looks something like this when represented as a scalar:
HASH(0x2735bc0) -- see the difference from the SCALAR in the example
above? Internally, Perl labels its data structures so it can tell how
to interpret them.)
Now, here you are with $data being set to \%hash.
What to do? You "dereference" $data to get at the info stored in
%hash. There are *at least* two ways to do that.
ONE way is to say $data->{somekey}. When Perl sees the $ sigil and
the ->, it knows that you're telling it: give me the 'somekey' value
stored in the hash that is located at $data. Here's an example:
C:\>perl
my %hash;
$hash{'key'} = 'value';
$hashref = \%hash;
print $hashref->{'key'}."\n";
When you run the program, you'll see this:
value
ANOTHER way is to say: %myhash = %$data, and then %myhash becomes the
equivalent of the original %hash. A short example will illustrate
this best:
C:\>perl
my %hash;
$hash{'key'} = 'value';
$hashref = \%hash;
%anotherhash = %$hashref;
print $anotherhash{'key'}."\n";
When you run the program, you'll see this:
value
Why?
The % sigil is the magic. When you assign %anotherhash to %$hashref,
you're essentially saying: assign %anotherhash to the values
represented in the hash stored at the memory location $hashref.
So you can either use the -> dereferencing scheme, or the %$
dereferencing scheme, according to taste. It is functionally the
same.
I do encourage you to PLAY WITH THIS. It only makes sense when you
do. After you've got the concept down, you'll wonder what was so hard
about it! :-))
Best,
--
--Geoff
Cell: (313)506-7295
Sterling Heights CERT
'04 Grand Cherokee | '05 Town & Country
http://www.moparmailinglist.com
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: variables
am 26.06.2008 17:21:41 von NBharucha
References are needed when programming properly in OO fashion, OO Perl
Modules. They are a great thing. It might take a few weeks before you
understand very well what is going on (for those new to perl). Once you
do you will embrace it.
Nick
-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Geoff
Sent: Thursday, June 26, 2008 10:12 AM
To: basil.daoust@gmail.com
Cc: activeperl@listserv.activestate.com
Subject: Re: variables
Perl references are really cool things, you just need to play with the
concept to 'get' it.
Declare a variable. Say it's $foo.
Set $foo = 'bar';
Now if you print "$foo"; you'll see "bar" printed out. I'm not going
to show you this; I assume you already know.
Now try this:
C:\>perl
$foo='bar';
$ref = \$foo;
print "$ref\n";
print "$$ref\n";
When you run the program, you'll see something like this:
SCALAR(0x325bc0)
bar
You see, $ref is set to the value of the space in memory where Perl is
storing the value of $foo. The \ sigil tells Perl to return the
address where the $foo variable is stored. So when you print out
$ref, you get the Perl-ish representation of "where $foo is".
The final line of the code is interesting: it says: give me the SCALAR
that is located where you are storing $foo. That's the double $ at
work there. The first $ sigil is telling Perl how to interpret the
value at SCALAR(0x325bc0).
Now let's talk about your particular issue.
Your subroutine is returning a reference, this time to a hash.
Imagine for a moment that internally, the parse_content_type() sub is
methodically doing whatever it does, and placing the results into
%hash.
At the end of the sub, there is a line that looks like this:
return(\%hash);
The \ sigil is telling Perl: give me the address where %hash is
located. (It looks something like this when represented as a scalar:
HASH(0x2735bc0) -- see the difference from the SCALAR in the example
above? Internally, Perl labels its data structures so it can tell how
to interpret them.)
Now, here you are with $data being set to \%hash.
What to do? You "dereference" $data to get at the info stored in
%hash. There are *at least* two ways to do that.
ONE way is to say $data->{somekey}. When Perl sees the $ sigil and
the ->, it knows that you're telling it: give me the 'somekey' value
stored in the hash that is located at $data. Here's an example:
C:\>perl
my %hash;
$hash{'key'} = 'value';
$hashref = \%hash;
print $hashref->{'key'}."\n";
When you run the program, you'll see this:
value
ANOTHER way is to say: %myhash = %$data, and then %myhash becomes the
equivalent of the original %hash. A short example will illustrate
this best:
C:\>perl
my %hash;
$hash{'key'} = 'value';
$hashref = \%hash;
%anotherhash = %$hashref;
print $anotherhash{'key'}."\n";
When you run the program, you'll see this:
value
Why?
The % sigil is the magic. When you assign %anotherhash to %$hashref,
you're essentially saying: assign %anotherhash to the values
represented in the hash stored at the memory location $hashref.
So you can either use the -> dereferencing scheme, or the %$
dereferencing scheme, according to taste. It is functionally the
same.
I do encourage you to PLAY WITH THIS. It only makes sense when you
do. After you've got the concept down, you'll wonder what was so hard
about it! :-))
Best,
--
--Geoff
Cell: (313)506-7295
Sterling Heights CERT
'04 Grand Cherokee | '05 Town & Country
http://www.moparmailinglist.com
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
This email is intended for the recipient only. If you are not the intended recipient please disregard, and do not use the information for any purpose.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 27.06.2008 19:20:35 von Ingo Schwarze
Hi Geoff,
i like your explanation of the fundamentals of references,
but near the end, you run the risk of spreading confusion:
Geoff wrote on Thu, Jun 26, 2008 at 10:12:00AM -0400:
> ANOTHER way is to say: %myhash = %$data, and then %myhash
> becomes the equivalent of the original %hash.
Better say: a shallow copy, not "the equivalent".
Why is this important?
1. A reference is not a copy.
After you change the original,
the reference points to the new value.
$ perl -e '
> use warnings;
> use strict;
> my %orig = ( key => "oldvalue" );
> my $ref = \%orig;
> print "at first, ref points to: $ref->{key}\n";
> $orig{key} = "newvalue";
> print "after changing orig, ref also points to: $ref->{key}\n";'
at first, ref points to: oldvalue
after changing orig, ref also points to: newvalue
2. Hash assignment does copy.
When you change the original,
the copy does not change.
$ perl -e '
> use warnings;
> use strict;
> my %orig = ( key => "oldvalue" );
> my %copy = %orig;
> $orig{key} = "newvalue";
> print "after changing orig, copy still contains: $copy{key}\n";'
after changing orig, copy still contains: oldvalue
3. The copy is shallow.
So, if references are contained,
they are copied as refernces:
$ perl -e '
> use warnings;
> use strict;
> my $scalar = "oldvalue";
> my $ref = \$scalar;
> my %orig = ( key => $ref );
> print "${$orig{key}}\n"; # caution: not $orig->{key}
> my %copy = %orig;
> $scalar = "newvalue";
> print "${$copy{key}}\n";'
oldvalue
newvalue
You need to be very careful what is refernced and what is copied;
in fact, that's the whole point of references...
Vague terms like "equivalent" are not helpful,
and even "assign" may sometimes be confusing.
[...l
> %anotherhash = %$hashref;
[...]
> When you assign %anotherhash to %$hashref,
Well, no. What you are doing here is assigning the contents
of %$hashref to %anotherhash. Or better, copying the contents
of %$hashref into %anotherhash.
Yours,
Ingo
--
Ingo Schwarze | Software Engineer | Framework Team
Astaro AG | www.astaro.com | 76227 Karlsruhe | Germany
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: variables
am 27.06.2008 20:44:46 von geoff
Thank you for adding precision to my post!
--Geoff
On Fri, Jun 27, 2008 at 1:20 PM, Ingo Schwarze wrote:
> Hi Geoff,
>
> i like your explanation of the fundamentals of references,
> but near the end, you run the risk of spreading confusion:
>
> Geoff wrote on Thu, Jun 26, 2008 at 10:12:00AM -0400:
>
>> ANOTHER way is to say: %myhash = %$data, and then %myhash
>> becomes the equivalent of the original %hash.
>
> Better say: a shallow copy, not "the equivalent".
>
> Why is this important?
>
> 1. A reference is not a copy.
> After you change the original,
> the reference points to the new value.
>
> $ perl -e '
>> use warnings;
>> use strict;
>> my %orig = ( key => "oldvalue" );
>> my $ref = \%orig;
>> print "at first, ref points to: $ref->{key}\n";
>> $orig{key} = "newvalue";
>> print "after changing orig, ref also points to: $ref->{key}\n";'
> at first, ref points to: oldvalue
> after changing orig, ref also points to: newvalue
>
> 2. Hash assignment does copy.
> When you change the original,
> the copy does not change.
>
> $ perl -e '
>> use warnings;
>> use strict;
>> my %orig = ( key => "oldvalue" );
>> my %copy = %orig;
>> $orig{key} = "newvalue";
>> print "after changing orig, copy still contains: $copy{key}\n";'
> after changing orig, copy still contains: oldvalue
>
> 3. The copy is shallow.
> So, if references are contained,
> they are copied as refernces:
>
> $ perl -e '
>> use warnings;
>> use strict;
>> my $scalar = "oldvalue";
>> my $ref = \$scalar;
>> my %orig = ( key => $ref );
>> print "${$orig{key}}\n"; # caution: not $orig->{key}
>> my %copy = %orig;
>> $scalar = "newvalue";
>> print "${$copy{key}}\n";'
> oldvalue
> newvalue
>
> You need to be very careful what is refernced and what is copied;
> in fact, that's the whole point of references...
> Vague terms like "equivalent" are not helpful,
> and even "assign" may sometimes be confusing.
>
> [...l
>> %anotherhash = %$hashref;
> [...]
>> When you assign %anotherhash to %$hashref,
>
> Well, no. What you are doing here is assigning the contents
> of %$hashref to %anotherhash. Or better, copying the contents
> of %$hashref into %anotherhash.
>
> Yours,
> Ingo
>
> --
> Ingo Schwarze | Software Engineer | Framework Team
> Astaro AG | www.astaro.com | 76227 Karlsruhe | Germany
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
--
--Geoff
Cell: (313)506-7295
Sterling Heights CERT
'04 Grand Cherokee | '05 Town & Country
http://www.moparmailinglist.com
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs