How does a scalar know what it is?

T

Tom

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?
 
K

Klaus Eichner

Tom said:
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?

it's not the complete answer, but the Devel::peek module could give some
hints. Try
http://search.cpan.org/~nwclark/perl-5.8.7/ext/Devel/Peek/Peek.pm
 
T

Tad McClellan

Tom said:
In C, I can just read the K&R book


The docs that ship with the perl distribution are to Perl what
K&R is to C.

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?


The first sentence of:

perldoc -f print

says "Prints a string or a list of strings."

So all arguments to print() are strings.

Perl must then use the string value of the variable.
 
S

Sisyphus

Tom said:
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
 
T

Tom

Sisyphus said:
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.
 
T

Tassilo v. Parseval

Also sprach Tom:
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
 
S

Sisyphus

Tom said:
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::peek;

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::peek;

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
 
A

Ala Qumsieh

Tom said:
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
 
T

Tom

Ala Qumsieh said:
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.
 
T

Tom

Sisyphus said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top