Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / Perl / Getting Started / July 2008



Tip: Looking for answers? Try searching our database.

Integer data type

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roy M - 03 Jul 2008 16:03 GMT
Conside the following codes:

use POSIX;

print LONG_MAX, "\n";

my $num = 99999999994;
print $num;

Why $num is bigger than LONG_MAX ?

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

yitzle - 04 Jul 2008 02:47 GMT
> Conside the following codes:
>
[quoted text clipped - 6 lines]
>
> Why $num is bigger than LONG_MAX ?

It is a string maybe?

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

yitzle - 04 Jul 2008 02:53 GMT
>> Conside the following codes:
>>
[quoted text clipped - 8 lines]
>
> It is a string maybe?

OK. That wasn't clear. Perl doesn't have an int and string and char
datatypes per say. Perl has a scalar and an array and a hash.
Perl does this dynamic casting to int or string as needed. Consider
the following code:
   perl -e 'use POSIX; print LONG_MAX . "\n"; my $x = LONG_MAX + 1;
print "$x\n"; my $y = LONG_MAX * 2; print "$y\n";'
I get the same number 3 times. But then I did this:
   perl -e 'use POSIX; my $x = 99999999999; print "$x\n"; $x = ($x -
1) / 2; print "$x\n";'
It got no problems dealing with bigger numbers and does this
correctly... Not what I would have expected to be honest...

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Roy M - 04 Jul 2008 03:56 GMT
Hi,

On Jul 4, 9:53 am, yit...@users.sourceforge.net (Yitzle) wrote:
> OK. That wasn't clear. Perl doesn't have an int and string and char
> datatypes per say. Perl has a scalar and an array and a hash.
> Perl does this dynamic casting to int or string as needed.

Yes, but even for dynamic casting

During addition, you still need to have an internal integer variable
for calucation (that must fit into the CPU register size)

E.g.

use POSIX;

print LONG_MAX, "\n";

my $num = 99999999994;
print $num + 3;  # No overflow here

I am running on 32 bit CPU.

Thanks.

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

John W. Krahn - 04 Jul 2008 04:19 GMT
> Conside the following codes:
>
[quoted text clipped - 6 lines]
>
> Why $num is bigger than LONG_MAX ?

Because the value in $num is a floating point number.

perldoc perlnumber

John
Signature

Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Roy M - 04 Jul 2008 08:29 GMT
Hi

> perldoc perlnumber

Thanks,

btw, are there any typeof command so I can check the data type of a
scalar?

Thanks

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Yitzchok Good - 04 Jul 2008 20:31 GMT
> Hi
>
[quoted text clipped - 6 lines]
>
> Thanks

Scalar /is/ the variable type. Any casting is done dynamically.
See http://perldoc.perl.org/perldata.html for more info.

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Rob Dixon - 04 Jul 2008 21:56 GMT
> Hi
>
[quoted text clipped - 4 lines]
> btw, are there any typeof command so I can check the data type of a
> scalar?

A Perl scalar value (including array and hash elements) can have more than one
type simultaneously. Why do you think you need to know what type of value is
being held?

Rob

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

howa - 05 Jul 2008 05:16 GMT
> A Perl scalar value (including array and hash elements) can have more than one
> type simultaneously. Why do you think you need to know what type of value is
> being held?
>
> Rob

hello,

can you show an example of  "more than one type simultaneously" ?

this is interesting...

Thanks.

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

John W. Krahn - 05 Jul 2008 19:22 GMT
>> A Perl scalar value (including array and hash elements) can have more than one
>> type simultaneously. Why do you think you need to know what type of value is
[quoted text clipped - 3 lines]
>
> this is interesting...

I assume that Rob is talking about the $! variable:

$ perl -le' $! = $_ and print "number:", $! + 0, "  string:", $! . ""
for 5 .. 9 '
number:5  string:Input/output error
number:6  string:No such device or address
number:7  string:Argument list too long
number:8  string:Exec format error
number:9  string:Bad file descriptor

John
Signature

Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Rob Dixon - 05 Jul 2008 23:31 GMT
>> A Perl scalar value (including array and hash elements) can have more than one
>> type simultaneously. Why do you think you need to know what type of value is
[quoted text clipped - 7 lines]
>
> this is interesting...

Perl will convert a scalar value between integer, floating point and string
representations internally depending on what your program does with the value,
and more than one representation can be valid at the same time, for instance if
I write

 my $val = '0099';
 print $val+1;

then $val will have both a string value of '0099' and an integer value of 99.

The Scalar::Util module has a function dualvar() that lets you assign both the
string and numeric value of a scalar simultaneously. Try the program below.

HTH,

Rob

use strict;
use warnings;

use Scalar::Util qw/dualvar/;
use Devel::Peek;

my $twovals = dualvar 42, 'string';

print $twovals, "\n";
print $twovals+0, "\n";

Signature

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

John W. Krahn - 04 Jul 2008 22:02 GMT
> Hi

Hello,

>> perldoc perlnumber
>
> btw, are there any typeof command so I can check the data type of a
> scalar?

You can use ref(): <QUOTE> You can think of "ref" as a "typeof"
operator. </QUOTE>

perldoc -f ref

Also Scalar::Util has the looks_like_number and reftype subroutines.

perldoc Scalar::Util

John
Signature

Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.