Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How does a scalar know what it is?

Reply
Thread Tools

How does a scalar know what it is?

 
 
Tom
Guest
Posts: n/a
 
      07-24-2005
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?




 
Reply With Quote
 
 
 
 
Klaus Eichner
Guest
Posts: n/a
 
      07-24-2005
"Tom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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:eek module could give some
hints. Try
http://search.cpan.org/~nwclark/perl...l/Peek/Peek.pm

--
Klaus


 
Reply With Quote
 
 
 
 
Dave
Guest
Posts: n/a
 
      07-24-2005
I think this is what you want to read:

From Klaus' link:
http://search.cpan.org/~nwclark/perl....pod#Datatypes

or type:

perldoc perlguts

at the CLI.

Dave

"Tom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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?
>
>
>
>



 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      07-24-2005
Tom <(E-Mail Removed)> wrote:

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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      07-24-2005

"Dave" <(E-Mail Removed)> wrote in message
news:LxLEe.15418$(E-Mail Removed)...
> I think this is what you want to read:
>
> From Klaus' link:
> http://search.cpan.org/~nwclark/perl....pod#Datatypes
>
> or type:
>
> perldoc perlguts
>
> at the CLI.
>
> Dave
>
> "Tom" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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 looks to me that these scalars are carrying around
alot of baggage. Does it need to carry this baggage
on the function calls?


 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      07-25-2005

"Tom" <(E-Mail Removed)> 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


 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      07-27-2005

"Sisyphus" <(E-Mail Removed)> wrote in message
news:42e463b2$0$27469$(E-Mail Removed) u...
>
> "Tom" <(E-Mail Removed)> 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
>
>


But that isnt true.


 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      07-27-2005
Also sprach Tom:

> "Sisyphus" <(E-Mail Removed)> wrote in message
> news:42e463b2$0$27469$(E-Mail Removed) u...
>>
>> "Tom" <(E-Mail Removed)> 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.


> 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=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
Sisyphus
Guest
Posts: n/a
 
      07-27-2005

"Tom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Sisyphus" <(E-Mail Removed)> wrote in message
> news:42e463b2$0$27469$(E-Mail Removed) u...
> >
> > "Tom" <(E-Mail Removed)> 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
> >
> >

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

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

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(0xb492e 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



 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      07-28-2005
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

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Why does sort return undef in scalar context ? Willem Perl Misc 62 09-09-2011 03:37 PM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
I know, I know, I don't know Andries Perl Misc 3 04-23-2004 02:17 AM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM



Advertisments