prev Translate | Page next |
All Sample codes are tested on LLEval
<?php print "0xF00" == 3840 ?>
By and large PHP seems to be making the same progression of mistakes as early Perl did, only slower.
/usr/bin/perl
is very likely 5.8.xuse feature
doesn't workuse feature
/usr/bin/perl
to 5.10.xgit clone git://github.com/rakudo/rakudo.git cd rakudo perl Configure.pl --gen-parrot make make install
my $scalar = 42; my @array = (1, 2, 3); my %hash = (one => 1, two => 2); print $scalar, "\n"; print @array, "\n"; print %hash, "\n";
my @array = (1, 2, 3); my %hash = (one => 1, two => 2); print $array[0], "\n"; print $hash{one}, "\n";
my @array = (1, 2, 3); my %hash = (one => 1, two => 2); print @array[0], "\n"; print %hash<one>, "\n";
my $aref = [1, 2, 3]; my $href = { one => 1, two => 2 }; print $aref[0], "\n"; print $href<one>, "\n";
#!/usr/bin/perl package Point; sub new { my $class = shift; my $self = bless {}, $class; my %arg = @_; $self->$_($arg{$_}) for keys %arg; $self; } sub x { my $self = shift; $self->{x} = shift if @_; $self->{x} } sub y { my $self = shift; $self->{y} = shift if @_; $self->{y} } package Point3D; use base 'Point'; sub z { my $self = shift; $self->{z} = shift if @_; $self->{z} } package main; my $p = Point3D->new(x => 1, y => 2, z => 3); local $, = ", "; print $p->x, $p->y, $p->z;
class Point { has $.x; has $.y; } class Point3D is Point { has $.z; } my $a = Point3D.new(x => 1, y => 2, z => 3); say [$a.x, $a.y, $a.z];
#!/usr/bin/perl package Point; use Moose; has 'x' => (is => 'rw'); has 'y' => (is => 'rw'); package Point3D; use Moose; extends 'Point'; has 'z' => (is => 'rw'); package main; my $p = Point3D->new(x => 1, y => 2, z => 3); local $, = ", "; print $p->x, $p->y, $p->z;
my $c3 = class { has $.x; has $.y; has $.z }; my $q = $c3.new(x => 1, y => 2, z => 3); say [$q.x, $q.y, $q.z];
use MooseX::Declare; # Not included in Moose my $class = class { has 'x' => ( is => 'rw'); has 'y' => ( is => 'rw'); has 'z' => ( is => 'rw'); }; my $q = $class->new_object(x => 1, y => 2, z => 3); local $, = ", "; print $q->x, $q->y, $q->z;
my $r = (class{ has $.x; has $.y; has $.z }).new(x => 1, y => 2, z => 3); say [$r.x, $r.y, $r.z];
use MooseX::Declare; # Danke, rafl! my $r = (class { has 'x' => ( is => 'rw'); has 'y' => ( is => 'rw'); has 'z' => ( is => 'rw'); })->new_object(x => 1, y => 2, z => 3); local $, = ", "; print $r->x, $r->y, $r->z;
print sub{ $_[0] * $_[0] }->(10);
say (sub($x){ $x * $x })(10);
print((function(x){ return x * x })(10)); // or alert() on browsers
say ->$x{ $x * $x }(10);
for (1..10) -> $x { say $x * $x };
say { $^x * $^x }(10);
say { $^x ~ $^y ~ $^z }('I', 'love', 'perl');
print((function(n){ return n <= 1 ? n : n * arguments.callee(n-1) })(10));
say { $^n <= 1 ?? $^n !! $^n * &?BLOCK($^n - 1) }(10);
say { $^n <= 1 ?? $^n !! $^n * &?BLOCK($^n - 1) }(10);
Waiting for jnthn to implement &?BLOCK
say { [*] (1..$^n) }(10);
say [ [1..9] >>*<< [1..9] ];
say [ [1..9]>>.sqrt ];
say [ [1..9].map({$_ ** 2}) ];
say [ [1..9].map:{$_ ** 2} ];
Works on Pugs, not yet on Rakudo
say [1..9] >>*<< [1..9];
my $t = 0; my $f = 1; say $t | $f ?? 'true' !! 'false'; say $t & $f ?? 'true' !! 'false'; say $t | $f; say ($t | $f).perl;
my $t = 0; my $f = 1; say $t | $f; say $t +| $f; say $t & $f; say $t +& $f;
for @question -> $q { $q.answer }