develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #109408] Documentation that refers to Perl 5 as new

Thread Previous | Thread Next
From:
Tom Christiansen
Date:
January 31, 2012 19:28
Subject:
Re: [perl #109408] Documentation that refers to Perl 5 as new
Message ID:
22860.1328066822@chthon
Brian Fraser <fraserbn@gmail.com> wrote

> There's only one big deletion, which was most of perltrap.pod -- It dealt
> with traps for Perl 4 programmers migrating to Perl 5. It would be really
> swell if someone could update perltrap for their other favorite language of
> choice, since right now the document is severely lacking, only dealing with
> awk, shell, C/C++, and Perl itself.

Brian and I wrote substantial sections for Python, Ruby, and Java for Camel4.
It's way too long, but if you want to cut it down to something reasonable,
that might do. The Java I'm pretty firm on, Python is ok, but I haven't a clue
about Ruby. You have like only 3 days to tell us if any of this is wrong. :(
Chromatic checked the Java and Python, but those I wasn't too nervous about,
especially Java.

--tom

=head2 Python Traps

Perl and Python are both dynamic languages that share some common ancestors
and appeared within 5 years of each other (1987 and 1991).  Perl 4 even
stole Python’s object system for Perl 5.  Although the two languages are
more alike than their superficial syntax would suggest, they also see many
of same things from different perspectives.

=item *

Python and Perl sometimes use different words for the same concepts,
and sometimes use the same words for different concepts:

=begin table picture A mapping of Python to Perl jargon

=headrow

=cell Python

=cell Perl

=bodyrows

=row

=cell Tuple

=cell List

=row

=cell List

=cell Array

=row

=cell Dictionary

=cell Hash

=end table

=item *

X<variables;Perl vs. Python, names of>
Variables begin with C<$>, C<@>, or C<%> in Perl.  Using sigils like
C<$str> lets Perl keep its nouns and its verbs separate, so you never have
to worry about accidentally overwriting some important built-in the way you
do in Python if you use C<str> for your purposes.  You I<can> override
built-ins in Perl, but never accidentally the way you can in Python.

=item *

Don’t forget to C<use warnings> so Perl notices things that in Python give
you exceptions.  Otherwise in Perl you only learn about these things if you
test for them, so if you forget, you never know.    See also M<warnings>
and M<autodie> in A<29>.

=item *

Many functions take default arguments or have default behavior for their
most common cases. See A<29>.

=item *

Perl knows about patterns and compiles them for you at compile-time along
with the rest of your program.  

=item *

Perl’s C<\N{R<NAME>}> construct allows shortcuts, aliases, and custom
names (which can even be different in different lexical scopes);
Python’s C<\N{R<NAME>}> doesn’t.

=item *

Perl characters are abstract code points, not low-level code units as in Python.

=item *

Perl pattern matching uses Unicode rules for case-insensitivity, but Python
uses only ASCII casefolding rules, but Perl uses Unicode casefolding rules,
so (for example) all three Greek sigmas match case-insensitively in Perl.

=item *

Perl’s casemapping functions like C<uc> and C<lc> follow full Unicode rules,
so work on all cased codepoints, not just on letters.

=item *

Perl understands (potentially nested) lexical scope, and so is completely
comfortable with full lexical closures.  Python doesn’t and isn’t.

=item *

Perl uses full Unicode casing, so the casemap of a string can be longer
than the original.  Python uses only simple Unicode casing (when it bothers
to use it at all), which doesn’t give as good results on strings.

=item *

Any subroutine that returns a blessed reference is a Perl constructor.
There’s no special name for a constructor method.

=item *

Perl methods are just methods, and they always receive their invocant as a
bonus, initial argument.  Perl as a language makes no distinction between
object methods, class methods, or static methods in the Python sense of these tings.

=item *

Perl’s object-orientation is optional, not mandatory. Perl doesn’t enforce
pervasive object-orientation on its built-in types unless you ask it to:
not everything has methods.  You might like M<autobox> though.

=item *

In Perl, you call a function with arguments:

  my $string = join("|", qw(Python Perl Ruby) );

In Python, there’s likely a main argument with a method to do it:

  new = "|".join(["Python", "Perl", "Ruby"])

=item *

Perl pattern matches float unless explicitly you anchor your pattern, like
Python’s C<re.search()> method but unlike its C<re.match()>, which can only
match at the beginning of a line.

=item *

Perl’s strings aren’t arrays of characters, so you can’t use array
operators on them.  On strings you use string operators, natch.

=item *

Except for a backslash itself or a backslashed delimiter,
Perl never expands backslash escapes in single-quoted strings,
but Python does.  Perl’s singled-quoted strings like C<'\t'>
are more like Python’s raw strings like C<r'\t'>.

=item *

Perl uses backticks to quote literals to execute arbitrary system commands
and return their output, as in C<$file = `cat foo.c`>. 

=item *

You don’t have to preallocate memory in Perl the way you do in Python,
because arrays and other data structures grow on demand, sometimes
via G<autovivification>.  In Python you have to explicitly grow 
your lists, and explicitly allocate new lists and dictionaries
to grow them.

=item *

Python throws exceptions for normal operations like C<open> failures,
while Perl uses special return values, usually C<undef>.  That means
if you forget to check for that error return, you will miss it.
You can use M<autodie> to make failed system calls raise exceptions.  

=item *

Perl does not by default throw exceptions on failed or partial numeric
conversions from strings, nor on treating C<undef> as a defined value.  You
can make it do so with:

    use warnings FATAL => q(numeric uninitialized);

=item *

Perl lists never nest, even if you add extra parens.  Use square brackets
to make nested arrays (of array references) instead.

=item *

Perl’s range operator is inclusive on both sides, so C<0..9>
includes C<0> and C<9>. 

=item *

Perl’s interactive shell is its debugger (A<19>), but M<Devel::REPL> is
good too.  Calling Perl without arguments does not drop you into an
interactive read-eval-print loop as it does in Python.  Use C<perl -de0>
X<read-eval-print loop>
for that.

=back

=head2 Ruby Traps

Matz, the creator of Ruby, stole heavily from Perl (and we think
he chose a pretty good starting point). Actually, he put a Perl
and a Smalltalk in a room and let them breed.

=over 4

=item *

There’s no B<irb>. See the Python section.

=item *

Perl just has numbers. It doesn’t care whether they have fractional
portions or not.

=item *

You don’t need to surround variables with C<{}> to interpolate them,
unless you need to disambiguate the identifier from the string around
it:

    "My favorite language is $lang"

=item *

Perl interpolated strings don’t have to be double-quoted: they can uses
C<qq> with arbitrary delimiters.  Similarly generic
uninterpolated strings don’t have to use single quotes: they can use a
C<q> with arbitrary delimiters.

	q/That's all, folks/
	q(No interpolation for $100)
	qq(Interpolation for $animal)

=item *

You need to separate all Perl statements with a C<;>, even if
they are on different lines. The final statement in a block doesn’t
need a final C<;>.

=item *

The case of variable names in Perl don’t mean anything to B<perl>.

=item *

The sigils don’t denote variable type. A C<$> in Perl is a single
item, like C<$scalar>, C<$array[0]>, or C<$hash{$key}>.

=item *

Perl compares strings with C<lt>, C<le>, C<eq>, C<ne>, C<ge>, and
C<gt>.

=item *

No magic blocks, but see M<PerlX::MethodCallWithBlock>.

=item *

Perl’s subroutine definitions are compile-phase. So

    use v5.10;
    sub foo { say "Camelia" }
    foo();
    sub foo { say "Amelia" };
    foo();

This prints C<Amelia> twice because the last definition is in place
before the run phase statements execute. This also means that the call
to a subroutine can appear earlier in the file than the subroutine’s
definition.

=item *

Perl doesn’t have class variables, but people try to fake them with
lexical variables.

=item *

The range operator in Perl returns a list, but see M<PerlX::Range>.

=item *

The C</s> pattern modifier makes Perl’s C<.> match a newline, whereas
Ruby uses the C</m> for the same thing. The C</m> in Perl makes the
C<^> and C<$> anchors match at the beginning and end of logical
lines.

=item *

Perl flattens lists.

=item *

Perl’s C<< => >> can stand in almost anywhere you can use a comma, so
you’ll often see Perler’s use the arrow to indication direction:

  rename $old => $new;

=item *

In Perl, C<0>, C<"0">, C<"">, C<()>, and C<undef> are false in
boolean contexts. Basic Perl doesn’t require a special boolean
value. You might want the M<boolean> module.

=item *

Perl often fakes the job of C<nil> with an C<undef>.

=item *

Perl allows you to be a bit sloppier because some of the characters
aren’t that special. A C<?> after a variable doesn’t do anything
to the variable, for instance:

    my $flag = $foo? 0 :1;

=back

=head2 Java Traps

=over 

=item * 

There is no C<main> in Perl, or rather, no

    public static void main(String[ ] argv) throws IOException

=item * 

Perl allocates memory for you as you need it by growing its array and
hashes on demand.  Autovifivication means that if you assign to it,
there'll be room for it.

=item * 

Perl doesn’t make you declare your variables in advance unless you C<use strict>.


=item * 

In Perl there is a difference between a thing and a reference to a thing, so you
(usually) have to explicitly dereference the latter.

=item * 

Not all functions need be methods in Perl.

=item * 

String and numeric literals aren't usually objects in Perl—but they can be.


=item * 

Java programmers looking to define a data structure using a class may
be surprised that Perl builds these up out simple data declarations mixing
anonymous hashes and arrays.  See  A<9>

=item *

Instance data on a Perl object is (usually) just a value in the hash
used for the object, where the name of that hash field corresponds to 
the name of instance data in Java.

=item *

Privay is optional in Perl.

=item *

The function that a method invocation ends up calling is not determined
until run time, and any object or class with a method by that name is 
just fine by Perl.  Only the interface matters.

=item * 

Perl supports operator overloading.

=item * 

Perl does not support function overloading by signature.  See 
the M<Class::Multimethod> module on CPAN.

=item *

Perl allows multiple inheritance, although this more corresponds to
multiple interfaces in Java, since Perl classes inherit only methods, not
data.

=item * 

A Java C<char> is not an abstract Unicode code point; it is a UTF-16 code
unit, which means it takes two of Java C<char>s, and special coding, to work
outside the Basic Multilingual Plane in Java.  In contrast, a Perl character
I<is> an abstract code point, whose underlying implementation is
intentionally hidden from the programmer.  Perl code automatically works 
on the full range of Unicode—and beyond.

=item * 

Unlike in Java, Perl’s string literals can have literal newlines in them.
It’s still usually better to use a “here” document, though.

=item * 

Functions typically indicate failure by returning C<undef>, not 
by raising an exception.  Use the M<autodie> pragma if you like the other way.

=item * 

Perl does not use named parameters; arguments to a program show up in each
function’s C<@_> array.  They’re typically given names right away, though.

=item * 

The things Perl refers to as function prototypes work nothing at all like Java’s.

=item * 

Perl supports pass by named parameter, allowing optional arguments to be omitted 
and the argument order freely rearranged.

=item * 

Perl’s garbage collection system is based on reference counting, so it is possible
to write a destructor to automatically clean up resources like open file descriptors,
database connections, file locks, etc.

=item * 

Perl regexes don’t need extra backslashes.

=item * 

Perl has regex literals, which the compiler compiles and syntax checks them at
compile time, and stores for efficiency. 

=item * 

Pattern matches in Perl do not silently impose anchors on your patterns the way
Java’s C<match> method does.  Perl’s matching works more like Java’s C<find> method.

=item * 

A Perl pattern can have more than one capture group by the same name.

=item * 

Perl patterns can recurse.

=item * 

Java patterns need a special option to make them use Unicode casefolding for
case-insensitive matches, but Perl patterns use Unicode casefolding by
default.  When doing so, Perl uses full casefolding but Java uses simple
casefolding only.

=item * 

In Java patterns, classic character classes like C<\w> and C<\s> are by
default ASCII-only, and it takes a special option to upgrade them to
understand Unicode.  Perl patterns are Unicode-aware by default, so it
instead takes a special option to downgrade classic character classes
(or POSIX classes) back to working only on legacy ASCII.

=item *

Java’s JNI corresponds to Perl’s XS, at least in spirit.  Perl modules
often have compiled C/C++ components, but Java’s rarely do.

=item *

Not everything need be rewritten in Perl; Perl makes it easy to call your
system’s native programs using `backticks`, C<system>, and pipe-opens.

=back

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About