Perl symantics

S

sln

There's been written guidelines naming 'C/C++' style
variables based on type.

Perl seems to be all caps, first letter caps, no caps.
And that is it.

What's that all about?

-sln
 
J

Josef Moellers

There's been written guidelines naming 'C/C++' style
variables based on type.

Perl seems to be all caps, first letter caps, no caps.
And that is it.

What's that all about?

It's about having a name contain information about the variable:

- constant vs. variable (most people use ALLCAPS to name a constant)
- global vs. local vs. object attribute (some people use a leading
Uppercase to name a global variable, alllowercase for local variables
and a leading "m_" to name an object m_ember)
- variable type (integer, char, ...) (some people use a leading "c_" to
name a c_haracter variable, a leading "i_" to name an i_nteger variable
etc).
Occasionally, it comes in handy: how often have you wondered what type a
variable is, whether it is a global or a local variable etc.

's that what you mean?

Groetjes,

Josef
 
R

Randal L. Schwartz

sln> There's been written guidelines naming 'C/C++' style
sln> variables based on type.

sln> Perl seems to be all caps, first letter caps, no caps.
sln> And that is it.

sln> What's that all about?

In C/C++, the *variables* are typed, and as the programmer, you have to be
aware of that almost constantly, or you can't even enter valid code. Hence,
people come up with naming schemes for variables so they can do the work that
should be done by automation. :)

In Perl, the *values* are typed, and you're free to say what you mean, and the
runtime system just *does the right thing*, and your productivity is
dramatically increased.

I still don't understand why people like typed variables. :(

print "Just another Perl hacker,"; # and smalltalk, and lisp, and python ...
 
T

Tim McDaniel

I still don't understand why people like typed variables.

For me, I like the sanity checking that can happen with strong typing.
If I know that variable "count" is used for the count of the number of
items that it's supposed to process, for examle, then I know it should
contain only an integer and I'd really like to know if code tries to
assign 'frog' to it, ideally at compile time.

I'd like to try stronger enforced assertions, like "this must be an
integer >= 0". I hope someone will reply with the five Perl CPAN
modules that implement such notions.
 
C

ccc31807

print "Just another Perl hacker,"; # and smalltalk, and lisp, and python ....

Your opinion, please.

I've made an effort to learn Lisp in the past year and am now writing
some small scripts in Lisp. I've also played with Erlang and Clojure,
and am inclined to give priority to Erlang because (1) it seems to do
better with multi core processors, and (2) I have very little need for
Java libraries that Clojure provides. Do you have an opinion as to the
future of Lisp/Erlang/Clojure?

In an interview recently, Tim Bray said that he had switched from Perl
to Ruby. Do you have an opinion as to the future of Perl/Python/Ruby?
I guess I'm a little curious as to Parrot, whether it will be able to
compete with the JVM and the CLR.

CC
 
P

Peter J. Holzer

For me, I like the sanity checking that can happen with strong typing.
If I know that variable "count" is used for the count of the number of
items that it's supposed to process, for examle, then I know it should
contain only an integer and I'd really like to know if code tries to
assign 'frog' to it, ideally at compile time.

Yup, the ability to catch such errors at compile time is the big
advantage of typed variables. While it is theoretically possible to
catch many such errors by static analysis of the program code (and some
newer dynamically typed languages do this) it is expensive and you don't
want to do this every time a script is executed.
I'd like to try stronger enforced assertions, like "this must be an
integer >= 0". I hope someone will reply with the five Perl CPAN
modules that implement such notions.

Smart::Comments comes to mind, although I haven't used it myself. I just
sprinkle my scripts with lots of 'die "bla" unless $foo;' statements.

hp
 
R

Randal L. Schwartz

Tim> For me, I like the sanity checking that can happen with strong typing.
Tim> If I know that variable "count" is used for the count of the number of
Tim> items that it's supposed to process, for examle, then I know it should
Tim> contain only an integer and I'd really like to know if code tries to
Tim> assign 'frog' to it, ideally at compile time.

You mean, you won't be writing unit tests for that, or relying on
the + operator to properly blow up?

Compile-time checking is insufficient to perform all tests, hence you will
need to write tests. Since you're writing tests anyway, compile-time checking
isn't necessary, since you're already checking for proper behavior.

Hence, compile-time checking is irrelevant.
 
T

Tim McDaniel

Tim> For me, I like the sanity checking that can happen with strong
Tim> typing. If I know that variable "count" is used for the count
Tim> of the number of items that it's supposed to process, for
Tim> examle, then I know it should contain only an integer and I'd
Tim> really like to know if code tries to assign 'frog' to it,
Tim> ideally at compile time.

You mean, you won't be ... relying on the + operator to properly blow
up?

At compile time? Only if it does flow analysis.
Compile-time checking is insufficient to perform all tests, hence you
will need to write tests. Since you're writing tests anyway,
compile-time checking isn't necessary, since you're already checking
for proper behavior.

There's variable amounts of effort involved, though. See how many
replies here say some version of "Add use warnings; use strict;".
Sure, those checks are not sufficient to catch ALL problems, but that
little effort gets a big payback. Similarly, "int" and such give
similar large-grained control but can give large (if not complete)
paybacks.

Also, where it is possible make a declaration that sticks, that's an
assertion provided in one place and used everywhere, instead of adding
an assertion everywhere the value gets assigned to or used.


I have a vague memory of Perl adding some sort of attributes facility
for variables, but the same vague memory suggests that they were
half-baked and have never been really supported or popular. Am I
mistaken? If they are solid now, where can I read up on them?
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top