"David Holmes" <> wrote:
> I have a few questions regarding perl composite data types:
I take it you are coming from a Pascal/ML-ish background? From what
little I know of such languages, Perl works quite differently. You do
not need to worry about types in the same way.
> firstly product types, as I see it the only way of doing this is to use
> Class::Struct. This will allow the use of multiple variables to be
> identified as a single value.
>
> secondly, does this make a hash a product type, because the keys are
> referenced by scalars, and these point to variables, which i guess could be
> anything but allows a simple structure.
I take it a 'product type' is something like a C struct, where you
define that a Foo, foo, consists of a Bar, foo.bar, and a baz,
foo.baz?
Class::Struct is indeed one way of doing this, although for simple
situations it is overkill. The standard Perl way is, as you say, with
a hash:
my %foo = (bar => "something", baz => "something else");
or with an anonymous hash:
my $foo = { bar => "something", baz => "something else");
my $x = { y => $foo, z => "Z" };
You can build up complex data structures like this on the fly, without
needing to declare the types beforehand.
> thirdly, does perl support sum types, as far as I can tell, since perl is
> not a typed language as such it is imposible to tell the differnce between
> the types, for example a case statement is impossible. The could obviously
> be overcome using the a regex to an extent to tell whether a scalar is a
> number or a string, but apart from that it is not really possible.
Again, I take it a 'product type' is like a C union, where a Foo is
either a Bar or a Baz but not both, and you can switch on the runtime
type? This is not really something you need in Perl: as the data
structures are all flexible, you just switch on the values instead.
Having never got my head round one of the 'type-oriented' languages, I
don't really know what you'd use them for to tell you how to do that
in Perl. If you can give me an example of a situation where you'd want
to use one, I'll think how I'd code it.
Ben
--
I've seen things you people wouldn't believe: attack ships on fire off the
shoulder of Orion; I've watched C-beams glitter in the darkness near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-|