Perl scalars as numbers or character strings

S

sln

PES> Sorry for the noobish questions, but I am used to C and Delphi
PES> Pascal, where I am familiar with the syntax.

It's not the syntax you're having trouble with, it's the semantics.
Perl doesn't have strongly typed variables the way the languages you
insist on comparing it with do.

A scalar holds one data value, such as a string or a number. If you
treat a scalar as if it has a number in it, Perl will do its best to
interpret that scalar as a number. If you treat a scalar as if it has a
string in it, Perl will do its best to interpret that scalar as a
string.

So yes, you can say $x = "57"; and Perl will dutifully put the string
"57" into $x. Then, when you say $y = $x + 7; Perl will interpret $x as
if it contains a number and add 7 to it. And then, when you say print
$y; -- well, Perl will convert $y to a string to print it for you.

Charlton

Total confusion. A vaiable contains a value that is typed. How you
use it depends on its TYPE not visa versa. Don't go down that road
with a C person.

Do give C people credit for knowing what a Union is. Because, that
is what Perl uses.

You obviously, never, ever, programed in a structured language.
If you did, describe Perl's actual usage to somebody who has.

And don't sugar coat, and lay off beginner crap, superiority shit
you and others keep foaming about via insinuation.

And since PERL is actually written upon C, try to learn it.
I realize you don't know C/C++, or structured language, but
try to make it sound like you do, at least.

-sln
 
C

Charlton Wilbur

sln> Total confusion. A vaiable contains a value that is typed. How
sln> you use it depends on its TYPE not visa versa. Don't go down
sln> that road with a C person.

You appear to be the one who's confused; if your assertion here is to be
believed, then saying something like:

$x = "42";
print $x + 7;

would result in a type mismatch error of some sort.

The values in scalars do have types, but the value and the type are
implicitly converted whenever it's appropriate. It's a lot less
confusing if you don't worry about the implicit type and value
conversion until you get a grasp on the basics, just as it's a lot less
confusing if you don't worry about reference counts and garbage
collection until you get a grasp on the basics.

sln> Do give C people credit for knowing what a Union is. Because,
sln> that is what Perl uses.

Except that it's a good deal more complicated than that, and explaining
it at that level is not likely to make things any clearer.

Consider:

union foo
{
int i;
char *str;
};

struct foo x;

If I then say

x.i = 27;

I can't then say

printf ("%s", x.str);

and expect to see a nice neat 27.

Likewise, I can't say

x.str = malloc (20);
strcpy (x.str, "27");
printf ("%d", x.i + 3);

and expect to see a nice neat 30.

Explaining scalars to a C programmer as if they were unions is unlikely
to be productive because the resemblance is only superficial. If he
understands unions well, the differences are going to trip him up far
more than the similarities are going to help him, and if he does not
understand unions well, then it's just going to confuse him further.

sln> You obviously, never, ever, programed in a structured language.
sln> If you did, describe Perl's actual usage to somebody who has.

Perl's implementation details are irrelevant to someone working at the
level of the original poster, and they're unlikely to help him
understand the language itself.

And what you seem to think is obvious is, frankly, wrong. This should
be obvious to most of the people who have read your postings, but it
doesn't hurt to reiterate it.

sln> And since PERL is actually written upon C, try to learn it. I
sln> realize you don't know C/C++, or structured language, but try
sln> to make it sound like you do, at least.

What you "realize" is utter nonsense; among other things, if you had the
remotest clue about either C or C++ you'd realize that calling it
"C/C++" shows idiocy to the C aficionados that's roughly comparable to
the idiocy you demonstrate by calling Perl "PERL."

And given your confusion about how Perl implements its data structures
internally -- they're structs, not unions -- and your misconception that
Perl scalars are usefuly analogous to C unions, I daresay I most likely
have a better grasp of C than you do.

Charlton
 
P

Paul E. Schoen

I think I understand. But I don't like it. I am really more used to Delphi,
where it is easy to concatenate strings such as '5'+'7' := '57';

Total confusion. A vaiable contains a value that is typed. How you
use it depends on its TYPE not visa versa. Don't go down that road
with a C person.

Do give C people credit for knowing what a Union is. Because, that
is what Perl uses.

My understanding of a Union is allowing a certain block of memory to be
interpreted in more than one way. So an integer of 32 bits may also be
addressed as an array of bytes, or a multipart record may also be addressed
as a single string. It is essentially a predefined typecast. I really have
not used it often, and it's done differently in Delphi, which uses a Case
statement to refer to the same data in different ways.

But a string in C, such as "1234", will always be just four ASCII
characters, and a NULL terminator. You use a function like atol() or atoi()
or atof() to assign the result to a numerical variable in a different
memory space.

Now a Variant may hold various kinds of information, and is used when the
data type is known only at runtime. But once it is set to a specific type,
it remains as such until explicitly reassigned to another type with new
data. It is essentially a pointer to an object that is created at runtime.

In Perl I don't know enough about the internals, but since it is
essentially an interpreted language it may perform its interpretations of
the data type on the fly, by applying appropriate conversions, or perhaps
the data is stored in a structure which contains parts that are string,
integer, and floating point.

Paul
 
C

Charlton Wilbur

PES> I think I understand. But I don't like it. I am really more
PES> used to Delphi, where it is easy to concatenate strings such as
PES> '5'+'7' := '57';

You can concatenate strings in Perl too; it uses the . operator.

sln> Do give C people credit for knowing what a Union is. Because,
sln> that is what Perl uses.

PES> My understanding of a Union is allowing a certain block of
PES> memory to be interpreted in more than one way.

....which is *not* what a Perl scalar is. sln is tragically confused,
and probably best ignored.

PES> In Perl I don't know enough about the internals, but since it
PES> is essentially an interpreted language

Er, no. Perl is compiled to an intermediate representation, which is
then executed. It's no more "essentially an interpreted language" than
Java is -- the principal difference is that you can store the
intermediate representation in Java, and you can't in Perl.

You *really* need to get yourself a good basic book on Perl. Beginning
Perl is available online for free at
http://www.perl.org/books/beginning-perl/, and odds are good that it's
better than any other tutorial you're likely to find free online. Work
through it -- if you're an experienced programmer with a clue, it will
go quickly, and you'll spare yourself a lot of the fumbling around that
you're doing now.

Charlton
 
J

Jürgen Exner

Charlton Wilbur said:
sln> Total confusion. A vaiable contains a value that is typed.

Right.
And in Perl those types are scalar, array, hash, file handle, and
directory handle (did I forget any?).
sln>How
sln> you use it depends on its TYPE not visa versa.

Right. In general you cannot use e.g. an array instead of a file handle.

Now, what does that have to do with the numerical versus the string
versus the boolean value of a scalar?
sln> Do give C people credit for knowing what a Union is. Because,
sln> that is what Perl uses.

Except that it's a good deal more complicated than that, and explaining
it at that level is not likely to make things any clearer.

Actually it is WAAAAYYY easier than that. Perl has scalars.
And when used in numerical context then the numerical value of the
scalar will be used, when in string context then the string value will
be used, when in boolean context then the boolean value will be used.
That's it. Nothing compicated about it unless you insist on making it
complicated.

And it has nothing whatsoever to do with a union in C. And scalars
aren't implemented using C unions (set sum in mathematics), either.
Actually quite the opposite, they are more like a struct in C (cross
product in mathematics), where the system ensures that the different
representation of the value (numerical, text, ...) are automatically
kept in sync with each other whenever needed.

jue
 
J

Jürgen Exner

Well, I'd rather rephrase this as

Then, when you say $y = $x + 7; Perl will determine the numerical value
of $x and add 7 to it. And then, when you say print
$y; -- well, Perl will determine the string value of $y and print it for
you.
This "determine the xxx value of" doesn't change the value of the
scalar. It just creates (and actually even stores for later re-use) an
additional representation of that value in the scalar data item.
I think I understand. But I don't like it. I am really more used to Delphi,
where it is easy to concatenate strings such as '5'+'7' := '57';

What's the problem? In Perl you use + to add numbers and . to
concatenate strings:
my $foo = '5' . '7';
It's just a different operator (. instead of +).
In Perl I don't know enough about the internals, but since it is

And you don't have to. Just remember that in Perl a scalar always has a
numerical, a string, a float, a boolean value and Perl will
automatically choose the right representation for the task at hand.
essentially an interpreted language it may perform its interpretations of
the data type on the fly, by applying appropriate conversions, or perhaps
the data is stored in a structure which contains parts that are string,
integer, and floating point.

Both.

jue
 
S

sln

PES> Sorry for the noobish questions, but I am used to C and Delphi
PES> Pascal, where I am familiar with the syntax.

It's not the syntax you're having trouble with, it's the semantics.
Perl doesn't have strongly typed variables the way the languages you
insist on comparing it with do.

A scalar holds one data value, such as a string or a number. If you
treat a scalar as if it has a number in it, Perl will do its best to
interpret that scalar as a number. If you treat a scalar as if it has a
string in it, Perl will do its best to interpret that scalar as a
string.

So yes, you can say $x = "57"; and Perl will dutifully put the string
"57" into $x. Then, when you say $y = $x + 7; Perl will interpret $x as
if it contains a number and add 7 to it. And then, when you say print
$y; -- well, Perl will convert $y to a string to print it for you.

Charlton

A Perl variable is probably most closely related to a structure.
I achieved my intended purpose. C programmer, now u know and have
some respect.

Good luck!

-sln
 
J

Jon Du Kim

Whats your claim, apparently you know everything, sort of a jack-off
of all trades.
I am essentially a lurker but will uncloak briefly to
provide some information.
Here is the deal with U. Guttman:
(1) He is chronically unemployed(i.e. he "works
for himself" but has no clients)
(2) He is a loud boorish oaf. Picture the "comic
book guy" from The Simpsons. He is morbidly obese with a
constant stench of faeces.
(3) He was a smart kid (he went to harvard or
mit or someplace of that caliber) but has been a failure
as an adult.
Rumor mill at yapc this year held that he is losing his
house to foreclosure after many long years of
professional failure.
Bottom line, take his advice but with no more weight
given to it than that from any other enthusiastic
amateur or academic. He doesn't seem to have much
understanding of(or aptitude for) real life. I have seen
him irl at yapc the several times I have gone since he
always seems to be a presence there. I think he is from
Boston(Not that it matters at all). I don't think I have
ever spoken to him but people do make fun of him behind
his back quite a bit and he seems to have few real
friends in the community anymore. You should probably
should just feel sad for him and leave him alone. Just
another internet weirdo...
 
S

sln

I am essentially a lurker but will uncloak briefly to
provide some information.
Here is the deal with U. Guttman:
(1) He is chronically unemployed(i.e. he "works
for himself" but has no clients)
(2) He is a loud boorish oaf. Picture the "comic
book guy" from The Simpsons. He is morbidly obese with a
constant stench of faeces.
(3) He was a smart kid (he went to harvard or
mit or someplace of that caliber) but has been a failure
as an adult.
Rumor mill at yapc this year held that he is losing his
house to foreclosure after many long years of
professional failure.
Bottom line, take his advice but with no more weight
given to it than that from any other enthusiastic
amateur or academic. He doesn't seem to have much
understanding of(or aptitude for) real life. I have seen
him irl at yapc the several times I have gone since he
always seems to be a presence there. I think he is from
Boston(Not that it matters at all). I don't think I have
ever spoken to him but people do make fun of him behind
his back quite a bit and he seems to have few real
friends in the community anymore. You should probably
should just feel sad for him and leave him alone. Just
another internet weirdo...

De-cloaking Klingon eh.. Every assholes got em, nothing new,
"move on", "move on", "move on to offworld colony", "move on"...!

-sln
 
H

Heinrich Mislik

[email protected] says... said:
print ($date == $today) ? "Happy B.Day!\n" : "Happy Day!\n";

Really better?

# perl -cwe 'print ($date == $today) ? "Happy B.Day!\n" : "Happy Day!\n";'
print (...) interpreted as function at -e line 1.
Useless use of a constant in void context at -e line 1.
Useless use of a constant in void context at -e line 1.
Name "main::date" used only once: possible typo at -e line 1.
Name "main::today" used only once: possible typo at -e line 1.
-e syntax OK

Cheers

Heinrich
 
K

Keith Thompson

On Mon, 06 Jul 2009 18:16:11 -0400, Uri Guttman
then you have an aversion to most popular languages today. c is still
used and is useful but it isn't good for dynamic data which is what web
apps typically need.

WTF is a Web App, lol !!!!

<http://lmgtfy.com/?q=web+app>

[...]

You know i won't explore any links anybody types here.
Wanna know why? Cause some jack-off like you points to a page that
will load my machine with a virus.

Ok, fine. If you really want an answer to your question, go to
www.google.com and type "web app" into the search box, or do the
research in any other manner you like. And your accusation is
unfounded, untrue, and stupid. (I don't deny that some people would
post malicious links; I deny that I would do so or have done so,
and that you have any basis for assuming that I would or did.
In lieu of a voice, I suggest you recant to yourself just exactly what
C/C++/api's writtine on them (f***in all of them) are f***ing good for
you moron.

Quoted text edited for content.

Plonk. (Since you probably won't look that up, it means I'm adding
you to my killfile and am unlikely to see anything you post in the
future.)
 
S

sln

(e-mail address removed) writes:
]
then you have an aversion to most popular languages today. c is still
used and is useful but it isn't good for dynamic data which is what web
apps typically need.

WTF is a Web App, lol !!!!

<http://lmgtfy.com/?q=web+app>

[...]

You know i won't explore any links anybody types here.
Wanna know why? Cause some jack-off like you points to a page that
will load my machine with a virus.

Ok, fine. If you really want an answer to your question, go to
www.google.com and type "web app" into the search box, or do the
research in any other manner you like. And your accusation is
unfounded, untrue, and stupid. (I don't deny that some people would
post malicious links; I deny that I would do so or have done so,
and that you have any basis for assuming that I would or did.
In lieu of a voice, I suggest you recant to yourself just exactly what
C/C++/api's writtine on them (f***in all of them) are f***ing good for ^^^ ^^^
you moron.

Quoted text edited for content.

Plonk. (Since you probably won't look that up, it means I'm adding
you to my killfile and am unlikely to see anything you post in the
future.)

Your a cartoon character from Toon Town. The more plonks the better,
as if silly little people like you who post on usenet from censured html
sites, who post nefarious one-line html links as a reply, mean jack shit to me.

I'm going to say it again. Everything WEB/ANYTHING api is written in assembly/C/C++.
Stop the bullshit and get your head out of your ass!!

-sln
 

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

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top