How does a scalar know what it is?

Discussion in 'Perl Misc' started by Tom, Jul 24, 2005.

  1. Tom

    Tom Guest

    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, Jul 24, 2005
    #1
    1. Advertising

  2. "Tom" <> wrote in message
    news:...
    > 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

    --
    Klaus
    Klaus Eichner, Jul 24, 2005
    #2
    1. Advertising

  3. Tom

    Dave Guest

    I think this is what you want to read:

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

    or type:

    perldoc perlguts

    at the CLI.

    Dave

    "Tom" <> wrote in message
    news:...
    > 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?
    >
    >
    >
    >
    Dave, Jul 24, 2005
    #3
  4. Tom <> 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
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jul 24, 2005
    #4
  5. Tom

    Tom Guest

    "Dave" <> wrote in message
    news:LxLEe.15418$...
    > I think this is what you want to read:
    >
    > From Klaus' link:
    > http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlguts.pod#Datatypes
    >
    > or type:
    >
    > perldoc perlguts
    >
    > at the CLI.
    >
    > Dave
    >
    > "Tom" <> wrote in message
    > news:...
    > > 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?
    Tom, Jul 24, 2005
    #5
  6. Tom

    Sisyphus Guest

    "Tom" <> 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
    Sisyphus, Jul 25, 2005
    #6
  7. Tom

    Tom Guest

    "Sisyphus" <> wrote in message
    news:42e463b2$0$27469$...
    >
    > "Tom" <> 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.
    Tom, Jul 27, 2005
    #7
  8. Also sprach Tom:

    > "Sisyphus" <> wrote in message
    > news:42e463b2$0$27469$...
    >>
    >> "Tom" <> 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=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
    Tassilo v. Parseval, Jul 27, 2005
    #8
  9. Tom

    Sisyphus Guest

    "Tom" <> wrote in message
    news:...
    >
    > "Sisyphus" <> wrote in message
    > news:42e463b2$0$27469$...
    > >
    > > "Tom" <> 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::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
    Sisyphus, Jul 27, 2005
    #9
  10. Tom

    Ala Qumsieh Guest

    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
    Ala Qumsieh, Jul 28, 2005
    #10
  11. Tom

    Tom Guest

    "Ala Qumsieh" <> wrote in message
    news:eek:dYFe.1262$...
    > 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
    >


    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, Jul 30, 2005
    #11
  12. Tom

    Tom Guest

    "Sisyphus" <> wrote in message
    news:42e463b2$0$27469$...
    >
    > "Tom" <> 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 this is not true.
    Tom, Jul 30, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Egginton

    LOOK! i just want to know does anyone know...

    Simon Egginton, Jul 26, 2004, in forum: Javascript
    Replies:
    3
    Views:
    166
    Dr John Stockton
    Jul 26, 2004
  2. Clint Olsen
    Replies:
    6
    Views:
    338
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  3. Andries

    I know, I know, I don't know

    Andries, Apr 23, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    216
    Gregory Toomey
    Apr 23, 2004
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    152
    Arndt Jonasson
    Jan 27, 2005
  5. Willem
    Replies:
    62
    Views:
    624
    Ilya Zakharevich
    Sep 9, 2011
Loading...

Share This Page