Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl data types

Reply
Thread Tools

Perl data types

 
 
David Holmes
Guest
Posts: n/a
 
      02-04-2004
I have a few questions regarding perl composite data types:

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.

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.

Regards

Dave


 
Reply With Quote
 
 
 
 
Eric Schwartz
Guest
Posts: n/a
 
      02-04-2004
"David Holmes" <(E-Mail Removed)> writes:
> 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.


What is a "product type"? I've not heard this terminology before.

> 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.


Not knowing what a product type is, I can't help.

> thirdly, does perl support sum types


What is a 'sum type'?

> 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.


Case statements are possible, they're just not built into the
language. See perlfaq7, "How do I create a switch or case statement?".

And perl is typed, albeit loosely.

> 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.


You don't (generally) care if it's a number or a string. If you use
it like a string, it's a string. If you use it like a number, it's a
number.

$foo = '4';

$bar = $foo + 3; # $bar is now 7

print "bar is [$bar]" if $bar eq '7';

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
 
 
 
AlV
Guest
Posts: n/a
 
      02-04-2004
Eric Schwartz wrote:
> "David Holmes" <(E-Mail Removed)> writes:


[snip]

>>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.

>
>
> Case statements are possible, they're just not built into the
> language. See perlfaq7, "How do I create a switch or case statement?".
>
> And perl is typed, albeit loosely.


Just to add my two cents, Damian Conway has made a module named
Attribute::Types which adds strong typing (at run time, not compile
time) to Perl 5:

http://search.cpan.org/~dconway/Attr...ibute/Types.pm
(this module requires Attribute::Handlers)

Have a nice day,
 
Reply With Quote
 
David Holmes
Guest
Posts: n/a
 
      02-04-2004
Hi,

The definition of a product type is a type that collects together a number
of data items in a single value. This is why I think that it is a struct. I
know that a product type would be a strinct in c, and in java it would just
be a case of specifying multiple variables, to a class.

Basically I dont know what languages you know but take java for example,
which of course would be similar for c++. If you have an object called Test
that has two string variables, you can deal with the Test object as an
individual item, but can get access to the individual elements.

Similar in a way to a perl hash, you can deal with the hash as a whole
thing, whilst being able to get access to the keys and values, as if they
are two variables of the hash object.

As for sum types, they are subtly different. Values of a sum type are
tagged. This can take several forms but again in the example of a java.
Incase you cant tell this is the language I use most. If you have a base
class that extends a superclass1 and a superclass2. You can initialise a
variable as the base class, and then say:

if (instanceOf(superclass1)) { do something }
else if (instanceOf(superclass2)) { do something else }

As I said the values of a sum type are tagged, so I guess in perl since
there are few primative types, it would be like taking a variable,
initialising it to some value and asking:

if this is a scalar { do something }
else if it is a hash { foreach hash { do something }}
else if it is an array.....


Hope that at least makes some sense.

Regards

Dave

"Eric Schwartz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "David Holmes" <(E-Mail Removed)> writes:
> > 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.

>
> What is a "product type"? I've not heard this terminology before.
>
> > 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.

>
> Not knowing what a product type is, I can't help.
>
> > thirdly, does perl support sum types

>
> What is a 'sum type'?
>
> > 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.

>
> Case statements are possible, they're just not built into the
> language. See perlfaq7, "How do I create a switch or case statement?".
>
> And perl is typed, albeit loosely.
>
> > 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.

>
> You don't (generally) care if it's a number or a string. If you use
> it like a string, it's a string. If you use it like a number, it's a
> number.
>
> $foo = '4';
>
> $bar = $foo + 3; # $bar is now 7
>
> print "bar is [$bar]" if $bar eq '7';
>
> -=Eric
> --
> Come to think of it, there are already a million monkeys on a million
> typewriters, and Usenet is NOTHING like Shakespeare.
> -- Blair Houghton.



 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-04-2004

"David Holmes" <(E-Mail Removed)> 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. |-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-| http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-04-2004
[don't top-post]

"David Holmes" <(E-Mail Removed)> wrote:
> As for sum types, they are subtly different. Values of a sum type are
> tagged. This can take several forms but again in the example of a java.
> Incase you cant tell this is the language I use most. If you have a base
> class that extends a superclass1 and a superclass2.


Surely you mean 'you have a base class that is extended by a subclass1
and a subclass2'? You're talking normal single inheritance, rather
than a class which has two base classes (or implements two interfaces,
or whatever mess Java's made of multiple inheritance)?

> You can initialise a variable as the base class, and then say:


> if (instanceOf(superclass1)) { do something }
> else if (instanceOf(superclass2)) { do something else }


Well, you can do that in Perl perfectly easily.

package Base;
sub new { return bless {}, shift } # a constructor

sub method {
my $self = shift;
if ($self->isa("Sub1")) {
# do summat
}
elsif ($self->isa("Sub2")) {
# do summat else
}
}

package Sub1;
push @ISA, "Base"; # set up inheritance
sub new {
my $class = shift;
my $self = $class->SUPER::new;
my $self->{sub1} = "data";
}

package Sub2;
push @ISA, "Base"; # set up inheritance
sub new {
my $class = shift;
my $self = $class->SUPER::new;
my $self->{sub2} = "data";
}

IMHO, Perl's OO is *really* well done. It supports all the concepts if
you need them, but doesn't force them down your throat the way Java
and C++ do. Of course, if you're a fan of Java you're likely to
disagree...

Ben

--
Like all men in Babylon I have been a proconsul; like all, a slave ... During
one lunar year, I have been declared invisible; I shrieked and was not heard,
I stole my bread and was not decapitated.
~ (E-Mail Removed) ~ Jorge Luis Borges, 'The Babylon Lottery'
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      02-04-2004
"David Holmes" <(E-Mail Removed)> writes:
> The definition of a product type is a type that collects together a number
> of data items in a single value. This is why I think that it is a struct.


As Ben Morrow pointed out, you'd use a hash for this in Perl, as a
rule.

> As for sum types, they are subtly different. Values of a sum type are
> tagged. This can take several forms but again in the example of a java.
> Incase you cant tell this is the language I use most. If you have a base
> class that extends a superclass1 and a superclass2. You can initialise a
> variable as the base class, and then say:
>
> if (instanceOf(superclass1)) { do something }
> else if (instanceOf(superclass2)) { do something else }


But that's crappy OO programming. You should do

object.method()

instead, and let inheritance take care of what gets done, no?

> As I said the values of a sum type are tagged,


How are they tagged? I don't see anything like that in your example.

> so I guess in perl since there are few primative types, it would be
> like taking a variable, initialising it to some value and asking:
>
> if this is a scalar { do something }
> else if it is a hash { foreach hash { do something }}
> else if it is an array.....


But in Perl, you already know what type it is, by the sigil in front
of the variable:

$var is a scalar
@var is an array
%var is a hash

So what would be the point?

> Hope that at least makes some sense.


Sorry, not really. I don't see what you'd want to use a sum type for.

And please don't top-post, it's rude, and it's against the Posting
Guidelines which are posted regularly to this group.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      02-05-2004
Eric Schwartz wrote:
> What is a "product type"? I've not heard this terminology before.


It's a type whose elements are the mathematical product (sometimes called
cross product) of two sets.
For example, the cross product of string and number could be used to create
data elements, which contain a name and the associated income.
A familar product type would be e.g. the "RECORD" type in Pascal or Modula.

>> secondly, does this make a hash a product type, because the keys are
>> referenced by scalars,


Corrrection: the keys are are actual scalars

>> and these point to variables, which i guess
>> could be anything but allows a simple structure.


Well, I wouldn't call it "point to", but essentially you are right.
And yes, hashes could be considered to be a form of a product type.

>> thirdly, does perl support sum types

>
> What is a 'sum type'?


A sum type is the mathematical sum of its constituents, e.g. the enumeration
of elements in a Pascal SET type.

>> 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,


You are mistaken, Perl is a strictly typed language. However, the types are
not what you would normally consider types. The Perl data types are scalar,
array, and hash. You just need to get used to that notion of type.

jue


 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      02-05-2004
Also sprach Eric Schwartz:

> "David Holmes" <(E-Mail Removed)> writes:


>> As for sum types, they are subtly different. Values of a sum type are
>> tagged. This can take several forms but again in the example of a java.
>> Incase you cant tell this is the language I use most. If you have a base
>> class that extends a superclass1 and a superclass2. You can initialise a
>> variable as the base class, and then say:
>>
>> if (instanceOf(superclass1)) { do something }
>> else if (instanceOf(superclass2)) { do something else }

>
> But that's crappy OO programming. You should do
>
> object.method()
>
> instead, and let inheritance take care of what gets done, no?


No, not in Java. Due to broken-by-design method dispatch, you will
constantly be checking the type and then do a typecast. If you have a
class "Foo" with a method "bar" and you stuff 10 Foo objects into

Object array[10];

you can't just say 'array[0].bar'. Instead you have to write

((Foo)array[0]).bar();

This gets worse if 'array' contains Foo and Bar objects and both have a
"bar" method. Then this becomes:

if (array[0] instanceof Foo) {
((Foo)array[0]).bar();
} else {
((Bar)array[0]).bar();
}

Thus, it is often required in Java to do the type checking by hand.
Therefore I never quite understood how SUN can claim that Java has
polymorphism at all.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Eric Schwartz
Guest
Posts: n/a
 
      02-05-2004
"Tassilo v. Parseval" <(E-Mail Removed)> writes:
> Also sprach Eric Schwartz:
>> "David Holmes" <(E-Mail Removed)> writes:
>>> if (instanceOf(superclass1)) { do something }
>>> else if (instanceOf(superclass2)) { do something else }

>>
>> But that's crappy OO programming. You should do
>>
>> object.method()
>>
>> instead, and let inheritance take care of what gets done, no?

>
> No, not in Java. Due to broken-by-design method dispatch, you will
> constantly be checking the type and then do a typecast.


<snip the horror... THE HORROR!>

Well, no wonder I dislike statically-typed OO languages. Your
explanation is cogent, sensible, and completely inane. I can't
believe Java programmers stand for that sort of thing.

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
Can XSD simple types be derived from complex types? Soren Kuula XML 2 12-01-2005 07:51 PM
Where are ref types that are members of value types stored? Sathyaish ASP .Net 2 05-22-2005 07:32 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM
STD types vs C++ intrinsic types Jeremy Cowles C++ 5 08-19-2003 05:33 PM



Advertisments