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

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

Thread Previous | Thread Next
From:
Brian Fraser
Date:
February 1, 2012 18:43
Subject:
Re: [perl #109408] Documentation that refers to Perl 5 as new
Message ID:
CA+nL+nanqeHpTsfBv=yMi4+ym1RLK174=+4dS4kYMrMdaY3Nww@mail.gmail.com
On Wed, Feb 1, 2012 at 12:27 AM, Tom Christiansen <tchrist@perl.com> wrote:

> 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.
>

Eh. I only really know Ruby of those, so I'll tackle that. I'll be the
first to admit that I'm rather rusty, so yeah.


>
>
> =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"
>
>
It's not bare braces, but #{}:

$ ruby -e 'x=1; puts "{x}#{x}"'
{x}1

As a point of reference, "#{ ... }" in ruby is roughly "@{[ ... ]}" in perl.



> =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)
>

I thought this one was phrased very confusingly, but in any case it's also
worth noting that Ruby has similar constructs:

>
       %q/That's all, folks/

>        %q(No interpolation for $100)
       %(Interpolation for $animal)

(also %r!! for regexen, %w<> for qw(), and %W() for qw() with
interpolation.. And I think I'm missing some)

And on the topic of strings, ruby has a special kind of heredoc, <<-END,
which automagically deals with indentation of the text quoted.


> =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}>.
>

Eh, sigils in ruby don't do that either, unless you stress the definition
of variable type a lot. They denote scope; $var is a global, @var is a
instance variable, @@var is a class attribute.


>
> =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.
>

Ruby sort of does this, too. Or, well, doesn't unless you ask it to, but
pretends that it does even if you don't:

$ ruby -e '[ [1,2, [3,4]], [[[5]],6] ].each { |x| puts x }'

Which outputs 1..5. If you check the structure, it's still an AoA, but most
usual traversal methods seem to call something like .flatten before
starting.
You can get a totally flattened structure by calling .flatten or .flatten!
though.


>
> =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;
>

Also, in ruby the fat comma doesn't quote it's left-handed argument, so
instead people use 'symbols', e.g. :symbol => "yadda".

And in the topic of hashes, hash keys in ruby can be any object, whereas in
perl they can only be strings, tie magic notwithstanding.


>
> =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
>
>
>
If it helps, I posted a handful of things here a while ago:
http://blogs.perl.org/users/brian_d_foy/2011/10/perl-traps-for-ruby-programmers.html

I'll pod them and send a patch to the list later.

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