Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comments requested: brief summary of Perl

Reply
Thread Tools

Comments requested: brief summary of Perl

 
 
Adam Barr
Guest
Posts: n/a
 
      02-24-2004
For a book I am working on, I have written a brief (13 page) summary
of Perl. The intent of this is that an experienced programmer who did
not know Perl would be able to get enough information to read and
understand Perl programs. It is not meant to be a 100% complete
summary of the language, but everything in there should be correct.

If anyone is interested, please feel free to read it and send me
comments. Note that the material is copyrighted, this document is
provided for review only, and the ideas contained in any comments may
be used in the book, in whole or in part, with no rights granted to
the person who provided the comments. I will however acknowledge by
name (if desired) anyone who sends comments.

The information is in PDF form per my publisher's request, but if this
is a problem for anyone, email me and I can send it to you in plain
text.

The file is at

http://www.proudlyserving.com/language/perl.pdf

If you follow other newsgroups (or get comp.lang.perl, where I first
posted this), you may have noticed that I have written similar
summaries for other languages, that I have also posted about.

Thank you.

- Adam Barr
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      02-24-2004
Adam Barr wrote:
>
> For a book I am working on, I have written a brief (13 page) summary
> of Perl. The intent of this is that an experienced programmer who did
> not know Perl would be able to get enough information to read and
> understand Perl programs. It is not meant to be a 100% complete
> summary of the language, but everything in there should be correct.
>
> If anyone is interested, please feel free to read it and send me
> comments. Note that the material is copyrighted, this document is
> provided for review only, and the ideas contained in any comments may
> be used in the book, in whole or in part, with no rights granted to
> the person who provided the comments. I will however acknowledge by
> name (if desired) anyone who sends comments.
>
> The information is in PDF form per my publisher's request, but if this
> is a problem for anyone, email me and I can send it to you in plain
> text.
>
> The file is at
>
> http://www.proudlyserving.com/language/perl.pdf


From page 2:

> Strings can be quoted with either single or double quotes. Within single quotes, the only
>special characters in a string are the backslash and the single quote; to put an actual backslash in a
>single-quoted string you use \\ , and to put a single quote you use \' , as in these examples:


A backslash does not have to be escaped unless it is the last character in the string.

$ perl -le' $_ = q/ab\cd/; print'
ab\cd
$ perl -le' $_ = q/ab\cd\/; print'
Can't find string terminator "/" anywhere before EOF at -e line 1.


From page 3:

>The range operator can be used as a shortcut for a list that is a sequence of numbers:


That should be "a sequence of numbers or letters"

$ perl -le' @number1to5 = (1..5); @letterAtoE = ("A".."E"); print "@number1to5 @letterAtoE"'
1 2 3 4 5 A B C D E


From page 4:

> Remember that negative indices count back from the end of the list, so @arr[0..-1] is the
>whole array.


When you use the range operator in a list context the right-hand number has to be greater
than the left-hand number so [0..-1] won't work. You can use @arr[0..$#arr] for the
whole array.

>push ($newelement, @mylist);
>unshift (1, 2, 3), @numberlist;


The array has to be the first argument:

push( @mylist, $newelement );
unshift @numberlist, 1, 2, 3;


From page 5:

>splice ($listA, -1, 1, "last"); # replace last element
>splice ($listB, 1, 2); # remove second and third elements


The first argument of splice has to be an array:

$ perl -le' splice $scalar, 0, 1, "item"; print $scalar'
Type of arg 1 to splice must be array (not scalar dereference) at -e line 1, near ""item";"
Execution of -e aborted due to compilation errors.


>The entire contents of a hash are referred to by precending the name with % . The functions
>keys() and values() return lists of the keys and values of a hash:
>
>$machinenames = keys %iphash;
>$ipaddrs = values %iphash;


Your description is correct however your example uses scalar context which returns the
number of keys/values in the hash.


From page 8:

>foreach @mylist {
> print $_;
>}


That is a syntax error. Parenthesis are required around @mylist.

foreach ( @mylist ) {
print $_;
}


From page 9:

>When Perl reads a line out of a file, it includes the newline character ( '\n' ) at the end. Since
>it is common to want to remove this, Perl provides a built-in function chomp() whose only
>function is to remove the last character from a string if it is \n .


This isn't actually what chomp() does. It removes whatever the $/ variable has been set to
which can be a string of any size.


From page 11:

>if ($number =~ /([0-9a-fA-F]+) {
> print ("$number is a valid hexadecimal number\n");
>}


That is missing the closing delimiter and parenthesis, it should be:

if ($number =~ /([0-9a-fA-F]+)/) {
print ("$number is a valid hexadecimal number\n");
}




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      02-24-2004
Also sprach Adam Barr:

> For a book I am working on, I have written a brief (13 page) summary
> of Perl. The intent of this is that an experienced programmer who did
> not know Perl would be able to get enough information to read and
> understand Perl programs. It is not meant to be a 100% complete
> summary of the language, but everything in there should be correct.
>
> If anyone is interested, please feel free to read it and send me
> comments. Note that the material is copyrighted, this document is
> provided for review only, and the ideas contained in any comments may
> be used in the book, in whole or in part, with no rights granted to
> the person who provided the comments. I will however acknowledge by
> name (if desired) anyone who sends comments.
>
> The information is in PDF form per my publisher's request, but if this
> is a problem for anyone, email me and I can send it to you in plain
> text.
>
> The file is at
>
> http://www.proudlyserving.com/language/perl.pdf
>
> If you follow other newsgroups (or get comp.lang.perl, where I first
> posted this), you may have noticed that I have written similar
> summaries for other languages, that I have also posted about.


comp.lang.perl doesn't exist contrary to popular belief.

Anyway, some comments not yet done by John:

Page 1:
> Perl stores all numbers as floating point; numbers and strings
> together are known as "scalars".


The first part is wrong. Perl stores simple integers as integers and
upgrades the variable if necessary. What you probably meant to say: Perl
transforms numbers from floating point to integer and the other way
round if necessary.

The second part is incomplete. References are also scalars. I find the
wording "numbers and strings together" a little misleading. Maybe you
better just write that a scalar variable can only hold one value,
whatever that is.

Page 2:
> Perl also does variable interpolation within double-quoted strings, meaning
> variables are replaced with their values:


This only applies to scalars and arrays. Hashes are not interpolated.

Naturally, your summary isn't always accurate and sometimes a bit
sloppy (for instance the bits you wrote about lists is - technically
speaking - wrong). But for a crude introduction this is in order.

Maybe you also want to add some notes about the perldocs. Someone
deciding to stick with Perl for a while thus knows where to find more
(and more precise) information.

Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus}) !JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexi ixesixeseg;y~\n~~dddd;eval
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-24-2004
John W. Krahn <(E-Mail Removed)> wrote:
> Adam Barr wrote:


> From page 5:
>
>>splice ($listA, -1, 1, "last"); # replace last element



For a brief introduction, I'd leave splice() uncovered, freeing
up room for something more frequently used.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tony Muler
Guest
Posts: n/a
 
      02-24-2004
Had a quick view and here are 2 hints free of charge:

1.
The sentence "Variable names start with $" is wrong/incomplete.
I would write the paragraph rather as:
"Variables do not need to be declared ...
Perl stores all numbers ... as "scalars". Scalar variable
names start with $ (mnemonic: looks like the S in scalar)."

2.
"As a shortcut for list of strings you can use qw (the letter q followed
by the letter w)":
Instead of explaining idiots how to type a 'q' and a 'w' I would rather
write:
"As a shortcut for list of strings you can use qw (stands for _q_uoted
_w_ords)". It is shorter and more usefull.


The document looks very complete and very clear.
(I like the way you do NOT explain local vs. my on filehandles ... I
don't really understand that either

T.

 
Reply With Quote
 
Stuart Moore
Guest
Posts: n/a
 
      02-24-2004
The if block on page 10 - should be

if (-f $file) {

not

(if -f $file) {

surely?

References to some websites on perl would be useful- saying modules can
be got from cpan for example.

It might be worth stressing what perl is good at - the regexps are a big
strong point, as are the hashes and lists. Regexps are difficult to
summarise well, you've done pretty well there, but might want to look at:

For [^abcd] explain it matches any character except abcd (how you wrote
it doesn't make it clear that it's the ^ that does the magic here)

You don't need a \ before the - in the phone regexp.
Both the phone and the hex regexp possibly ought to have ^ at the start
and $ at the end.


Other stuff:
Might be worth mentioning that print is used to send stuff to files too.

An explanation of use strict and or use warnings might be useful.

In the list stuff I'd look at mentioning "see also push" for the
$array[$#arrayname+1]= $value
bit - actually, surely those should both be array or arrayname?

Stuart
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      02-24-2004
Adam Barr <(E-Mail Removed)> wrote:

> For a book I am working on, I have written a brief (13 page) summary
> of Perl. The intent of this is that an experienced programmer who did
> not know Perl would be able to get enough information to read and
> understand Perl programs.



So at this point I'm thinking to myself:

I'd better see these Big Things when I get to reviewing it:

defaulting to $_ all over the place
while(<FH>) special case (input gets assigned to $_)
package variables vs. lexical variables
list context vs. scalar context
list (data) vs. array (variable)
strict and warnings

Because they are the things that are likely to invoke a "surprise factor"
for folks accustomed to other programming languages.


> If anyone is interested, please feel free to read it and send me
> comments.



I don't have time to review it right now, maybe tonight, but I
just wanted to throw out The Biggies before I forgot about it.


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Adam Barr
Guest
Posts: n/a
 
      02-24-2004
Excellent comments, thank you (especially John for actually trying my
broken examples). I constantly get the $ and @ wrong when typing array
names.

I have a question on storage of numbers. It says in the llama book
that Perl always stores numbers as double-precision floating point. Is
this incorrect?


- adam
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-24-2004
Adam Barr <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Excellent comments, thank you (especially John for actually trying my
> broken examples). I constantly get the $ and @ wrong when typing array
> names.
>
> I have a question on storage of numbers. It says in the llama book
> that Perl always stores numbers as double-precision floating point. Is
> this incorrect?


That's technically incorrect. Numbers in the native integer range are
internally stored and treated as such. However, since all conversions
happen automatically, you wouldn't know the difference. Maybe the book
is talking about Perl's numeric range, which would be rightly described
as double precision floating point.

Then again, Randal is known for, and stands by, what he calls pedagogical
lies. It may be a deliberate simplification. Cite? Randal?

Anno
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-24-2004
Adam Barr <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Excellent comments, thank you (especially John for actually trying my
> broken examples). I constantly get the $ and @ wrong when typing array
> names.
>
> I have a question on storage of numbers. It says in the llama book
> that Perl always stores numbers as double-precision floating point. Is
> this incorrect?


That's technically incorrect. Numbers in the native integer range are
internally stored and treated as such. However, since all conversions
happen automatically, you wouldn't know the difference. Maybe the book
is talking about Perl's numeric range, which would be rightly described
as double precision floating point.

Then again, Randal is known for, and stands by, what he calls pedagogical
(didactic?) lies. It may be a deliberate simplification. Cite? Randal?

Anno

 
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
Comments requested: brief summary of C Adam Barr C Programming 22 03-01-2004 09:30 AM
Comments requested: brief summary of Perl Adam Barr Java 6 02-27-2004 09:36 PM
Comments requested: brief summary of Python Adam Barr Python 9 02-25-2004 10:55 PM
RE: Comments requested: brief summary of Python Brian Quinlan Python 5 02-25-2004 01:58 PM
Comments requested: brief summary of Perl Adam Barr Perl 1 02-24-2004 06:03 PM



Advertisments