For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > July 2005 > How does a scalar know what it is?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How does a scalar know what it is?
Tom

2005-07-24, 8:46 pm

In C, I can just read the K&R book and know
how everything is pretty much coded at the
machine level. At least to some extent.

But how does Perl know when you type

print $ref

for example that it is not an integer, string or what have
you?

And how does

print $num; 2 or "Two"

again at the machine level, how does perl know
what it has?




Tom

2005-07-24, 10:08 pm


"Dave" <daveandniki@ntlworld.com> wrote in message
news:LxLEe.15418$Ag3.7534@newsfe4-gui.ntli.net...
> I think this is what you want to read:
>
> From Klaus' link:
> http://search.cpan.org/~nwclark/per...s.pod#Datatypes
>
> or type:
>
> perldoc perlguts
>
> at the CLI.
>
> Dave
>
> "Tom" <lfs.1@spam.net> wrote in message
> news:toqdnVRhz5Wr3X7fRVn-og@comcast.com...
>
>


It looks to me that these scalars are carrying around
alot of baggage. Does it need to carry this baggage
on the function calls?


Sisyphus

2005-07-25, 4:22 am


"Tom" <lfs.1@spam.net> wrote

>
> It looks to me that these scalars are carrying around
> alot of baggage. Does it need to carry this baggage
> on the function calls?
>


Think of it as the scalars carrying around a pointer to the baggage (rather
than carrying around the actual baggage) - which is not such a big load to
be carrying.

Cheers,
Rob


Tom

2005-07-27, 4:02 am


"Sisyphus" <sisyphus1@nomail.afraid.org> wrote in message
news:42e463b2$0$27469$afc38c87@news.optusnet.com.au...
>
> "Tom" <lfs.1@spam.net> wrote
>
>
> Think of it as the scalars carrying around a pointer to the baggage

(rather
> than carrying around the actual baggage) - which is not such a big load to
> be carrying.
>
> Cheers,
> Rob
>
>


But that isnt true.


Tassilo v. Parseval

2005-07-27, 4:02 am

Also sprach Tom:

> "Sisyphus" <sisyphus1@nomail.afraid.org> wrote in message
> news:42e463b2$0$27469$afc38c87@news.optusnet.com.au...
[color=darkred]
> But that isnt true.


Oh, it isn't? Then maybe you explain us how the innards of perl work. I
am sure you know much better than those people who have worked with perl
on the C-level.

Tassilo
--
use bigint;
$n=7142335034377028016139702633033737113
9054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
Sisyphus

2005-07-27, 9:03 am


"Tom" <lfs.1@spam.net> wrote in message
news:bpednXYY1fzsmXrfRVn-vQ@comcast.com...
>
> "Sisyphus" <sisyphus1@nomail.afraid.org> wrote in message
> news:42e463b2$0$27469$afc38c87@news.optusnet.com.au...
> (rather
to[color=darkred]
>
> But that isnt true.
>
>


I'm not the best person to be giving an explanation at this level but I
think I understand where your confusion arises. I often see written
something like "a scalar (such as $x) is represented in Perl as a C
structure of type SV". (This statement equates a perl scalar to an SV.) I
would prefer to see it written as "a scalar (such as $x) is represented in
Perl as a pointer to a C structure of type SV". (This statement equates a
perl scalar to an SV*.)

When you pass a scalar to a function at the perl level, what's actually
happening at the C level ? You're passing a pointer to an SV structure to
the function.

Do you have a C compiler ? If you do, then here's a little Inline::C demo
you could run:

use warnings;
use Devel::P;

use Inline C => <<'EOC';

void foo(SV * x) {
printf("%x\n", x);
}

EOC

# Start perl code
$z = 17;
Dump($z);
print"#########\n";
foo($z);
__END__

For me that produces:

SV = IV(0x89dab4) at 0x3f5d3c
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 17
#########
3f5d3c

Note that at the C level, foo() receives an argument of type SV* - ie a
pointer to an SV structure.
And at the perl level foo() takes a simple scalar as its argument - ie $z.
Note also that the value output by foo() matches the address of the SV
structure. That's not a coincidence :-)

So ... when I'm calling foo($z), the baggage is staying put. All I'm really
passing is a pointer to all of that baggage.

In a similar fashion, we can create a perl scalar at the C level.

use warnings;
use Devel::P;

use Inline C => <<'EOC';

SV * foo() {
SV * x;
x = newSViv(17);
return x;
}

EOC

$z = foo();

Dump($z);

__END__

That produces output of:

SV = IV(0xb492e8) at 0x3f5d3c
REFCNT = 1
FLAGS = (IOK,pIOK)
IV = 17

At the C level, an SV structure has been created, and a pointer to that
structure (an SV*) has been returned. Yet as far as perl is concerned, the
foo() function has returned a scalar variable.

Hth. (More importantly, I hope it's accurate. If not, I'm sure that someone
will correct it as needed.)

Cheers,
Rob



Ala Qumsieh

2005-07-27, 10:01 pm

Tom wrote:
> again at the machine level, how does perl know
> what it has?


I'm curious to know why you ask this question. If you want a language
that is closer to the hardware, then you know where to find C, or even
assembly. The whole point of a higher level language like Perl is to
unburden (is that a real word?) the user from such information. As a
Perl user, you don't need to keep track of whether a scalar is an
integer, string, reference or whatever. You can rest assured that:

print $x;

will do the Right Thing (tm) irrespective of what $x contains. This
comes at the expense of more memory usage. If your goal is to optimize
memory/performance, then you should be looking at a lower level language
like C.

--Ala

Tom

2005-07-30, 5:00 pm


"Ala Qumsieh" <notvalid@email.com> wrote in message
news:odYFe.1262$iM7.1056@newssvr21.news.prodigy.com...
> Tom wrote:
>
> I'm curious to know why you ask this question. If you want a language
> that is closer to the hardware, then you know where to find C, or even
> assembly. The whole point of a higher level language like Perl is to
> unburden (is that a real word?) the user from such information. As a
> Perl user, you don't need to keep track of whether a scalar is an
> integer, string, reference or whatever. You can rest assured that:
>
> print $x;
>
> will do the Right Thing (tm) irrespective of what $x contains. This
> comes at the expense of more memory usage. If your goal is to optimize
> memory/performance, then you should be looking at a lower level language
> like C.
>
> --Ala
>


If you really want to understand how to use any
computer language whatever, it is very helpful to
understand how it works at the machine level.



Tom

2005-07-30, 5:00 pm


"Sisyphus" <sisyphus1@nomail.afraid.org> wrote in message
news:42e463b2$0$27469$afc38c87@news.optusnet.com.au...
>
> "Tom" <lfs.1@spam.net> wrote
>
>
> Think of it as the scalars carrying around a pointer to the baggage

(rather
> than carrying around the actual baggage) - which is not such a big load to
> be carrying.
>
> Cheers,
> Rob
>
>


But this is not true.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com