FAQ 7.8 How do I declare/create a structure?

FAQ 7.8 How do I declare/create a structure?

am 22.10.2007 03:03:01 von PerlFAQ Server

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

7.8: How do I declare/create a structure?

In general, you don't "declare" a structure. Just use a (probably
anonymous) hash reference. See perlref and perldsc for details. Here's
an example:

$person = {}; # new anonymous hash
$person->{AGE} = 24; # set field AGE to 24
$person->{NAME} = "Nat"; # set field NAME to "Nat"

If you're looking for something a bit more rigorous, try perltoot.



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

Re: FAQ 7.8 How do I declare/create a structure?

am 22.10.2007 20:30:04 von it_says_BALLS_on_your forehead

On Oct 21, 9:03 pm, PerlFAQ Server wrote:
> This is an excerpt from the latest version perlfaq7.pod, which
> comes with the standard Perl distribution. These postings aim to
> reduce the number of repeated questions as well as allow the community
> to review and update the answers. The latest version of the complete
> perlfaq is athttp://faq.perl.org.
>
> ------------------------------------------------------------ --------
>
> 7.8: How do I declare/create a structure?
>
> In general, you don't "declare" a structure. Just use a (probably
> anonymous) hash reference. See perlref and perldsc for details. Here's
> an example:
>
> $person = {}; # new anonymous hash
> $person->{AGE} = 24; # set field AGE to 24
> $person->{NAME} = "Nat"; # set field NAME to "Nat"
>
> If you're looking for something a bit more rigorous, try perltoot.
>

Is there any particular reason why this example uses a hash reference
as opposed to a hash?

Re: FAQ 7.8 How do I declare/create a structure?

am 22.10.2007 21:21:33 von jl_post

> On Oct 21, 9:03 pm, PerlFAQ Server wrote:
>
> > $person = {}; # new anonymous hash
> > $person->{AGE} = 24; # set field AGE to 24
> > $person->{NAME} = "Nat"; # set field NAME to "Nat"


On Oct 22, 12:30 pm, nolo contendere replied:
>
> Is there any particular reason why this example uses a hash reference
> as opposed to a hash?


My guess is so that the structure can be used as a scalar, which
affords the programmer to easily place it into lists or arrays like
this:

push @employees, $person;

It's possible of course, to pass the reference of a %person hash
into the push() function like this:

push @employees, \%person;

but then unwanted behavior would result if the programmer forgot the
'\' (which would be an error that neither "use strict;" nor "use
warnings;" would catch).

Not only that, but if a hash reference is used for a structure, the
programmer can then use the spiffy '->' operator on the structure,
making the code more similar to what is found in certain other
languages (like C and C++).

-- Jean-Luc