| prev Translate | Page next | |
This document is also available as:
| English | cousin |
|---|---|
| にほんご | いとこ |
| 中国語 | 表姉、表妹、表兄、表弟、堂姉、堂妹、堂兄、堂弟 |
| English | I speak English. He speaks English. |
|---|---|
| にほんご | 私は日本語を話す。 彼は日本語を話す。 |
| 中国語 | 我説漢語。 他説漢語。 |
Perl is like Python and Ruby
print "Hello, world!";
...not like C
#include <stdio.h>
int main(int argc, char **argv){
print "Hello, world!";
return 0;
}
You don't have to say what's obvious -- and tedious.
Perl is like Scheme.
(define Z
(lambda (f)
((lambda (x) (f (lambda (y) ((x x) y))))
(lambda (x) (f (lambda (y) ((x x) y)))))))
Perl is like Scheme (doesn't look like one, though).
our $Z = sub { my $f = shift;
sub { my $x = shift;
sub { my $y = shift;
$f->($x->($x))
}
}->(sub { my $x = shift;
sub { my $y = shift;
$f->($x->($x))
}
});
...not like Haskell.
ch_y = \ f -> (\ x -> f (x x)) (\ x -> f (x x))
Occurs check: cannot construct the infinite type: t = t -> t1 Expected type: t Inferred type: t -> t1 In the first argument of `x', namely `x' In the first argument of `f', namely `(x x)'
irb(main):001:0> 1.class => Fixnum
% perl
print 1->isa('SCALAR');
Can't call method "isa" without a package
or object reference at - line 1.
Not everything is an object.
use autobox;
print 1->isa('SCALAR');
1
Though you can make it so with autobox.
package Whatever;
sub new {
my $class = shift;
bless { @_ }, $class;
};
# ....
package main;
my $obj = Whatever->new(foo => 42);
does NOT have an OO built-in
use CGI; use Data::Dumper; my $q = CGI->new; print Dumper $q;
$VAR1 = bless( {
'.parameters' => [],
'use_tempfile' => 1,
'.charset' => 'ISO-8859-1',
'.fieldnames' => {},
'escape' => 1
}, 'CGI' );
package Mom;
sub speak{
my $self = shift;
print $self->{name}, ": ", @_, "\n";
}
package Daughter;
our @ISA = qw/Mom/;
sub new{
my $class = shift;
bless { @_ }, $class;
}
$hazumi->speak("Hello, world!")?
my $hazumi = Daughter->new(name => 'Hazumi');
$hazumi->speak("Hello, world!");
$hazumi->speak("Hello, world!")?
ref $hazumi → DaughterDaugher::speak exists → No.Daugher has a parent class, using @ISA → Mom$hazumi->speak("Hello, world!")?
Mom::speak exists → yes.Mom::speak($hazumi, "Hello, world!") → Hazumi: Hello, world!
package InsideOut;
use strict;
use warnings;
my %objects;
sub new {
my $class = shift;
bless \eval{ my $scalar }, $class;
}
sub DESTROY{
my $self = shift;
delete $objects{$self+0};
}
sub name{
my $self = shift;
$objects{$self+0}{name} = shift if @_;
$objects{$self+0}{name};
}
sub uri{
my $self = shift;
$objects{$self+0}{uri} = shift if @_;
$objects{$self+0}{uri};
}
1;
use InsideOut;
use Data::Dumper;
my $o = InsideOut->new();
$o->name("dankogai");
$o->uri("http://blog.livedoor.jp/dankogai/");
print $o->name, ",", $o->uri, "\n";
print Dumper $o;
% perl insideout.pl
dankogai,http://blog.livedoor.jp/dankogai/
$VAR1 = bless( do{\(my $o = undef)}, 'InsideOut' );
Moose
Moose
package Point;
use Moose;
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'Int');
sub clear {
my $self = shift;
$self->x(0);
$self->y(0);
}
package Point3D;
use Moose;
extends 'Point';
has 'z' => (is => 'rw', isa => 'Int');
after 'clear' => sub {
my $self = shift;
$self->z(0);
};
# Ruby irb(main):001:0> "1"+"1" => "11"
# JavaScript
alert("1"+"1");
% perl -le 'print "1" + "1"' 2
say "1" + "1"; # numeric context say 1 . 1; # string context
say "1" + "1"; # numeric context say 1 . 1; # string context
my $date_in_string = localtime(); # scalar my ($second, $minute, $hour, $date, $month, $year) = localtime(); # list
# ruby
s="1"
i=2
puts "#{s},#{i}" # really "#{s.to_s}, #{i.to_s}";
% perl -le 'print $ARGV[0]*$ARGV[1]' 10 10 100
% ruby -e 'puts ARGV[0]*ARGV[1]' 10 10
-e:1:in `*': can't convert String
into Integer (TypeError)
from -e:1:in `'
% perl -le 'print localtime'; 4292114410831340
% perl -le 'print scalar localtime'; Wed May 14 21:30:19 2008
% perl -le 'print "".localtime'; Wed May 14 21:30:32 2008
use B::Deparse to find how perl groks it.
% perl -MO=Deparse -le 'print"".localtime' ;
BEGIN { $/ = "\n"; $\ = "\n"; }
print '' . localtime;
-e syntax OK
% perl -MO=Deparse,-p -le 'print"".localtime'
BEGIN { $/ = "\n"; $\ = "\n"; }
print(('' . localtime));
-e syntax OK
while(my $q = Questions->new){
$q->answer;
}