2008-11-01から1ヶ月間の記事一覧

comparison operators

comparison operators Perl $a = 1; $b = 1; if ($a == $b) { print "$a == $b\n" } $b = 2; if ($a != $b) { print "$a != $b\n" } $a = 2; $b = 1; if ($a > $b) { print "$a > $b\n" } $a = 1; $b = 1; if ($a >= $b) { print "$a >= $b\n" } $a = 1; $b …

logical operators

logical operators Perl sub return_true { print "return_true"; return 1; } sub return_false { print "return_false"; return 0; } print "&& "; if (return_false() && return_true()) { } print "\n"; print "and "; if (return_false() and return_tr…

function declaration

function declaration Perl sub add { $_[0] + $_[1] } print add(1, 2); # 3 Ruby def add(a, b) a + b end print add(1, 2) # 3 PHP Python def add(a, b): return a + b print add(1, 2) # 3

break, continue

break, continue Perl $n = 0; foreach $i (0..10) { print "$i, $n\n"; $n++; next if ($n % 2 == 0); redo if ($n % 3 == 0); last if ($n > 5); print "\n"; } print "$n\n"; L:\>perl lesson010.pl 0, 0 1, 1 2, 2 2, 3 3, 4 4, 5 5, 6 7 Ruby n = 0 for…

for, foreach

for, foreach Perl for ( $i=0; $i <= 10; $i++ ) { print "$i\n" # 0 - 10 } print "\n"; for $i (0..10) { print "$i\n" # 0 - 10 } print "\n"; foreach $i (0..10) { print "$i\n" # 0 - 10 } Ruby for i in 0..10 puts i # 0 - 10 end puts (0..10).eac…

while

while Perl $n = 0; while ($n < 10) { print $n++, "\n"; # 0 - 9 } print $n, "\n\n"; # 10 $n = 0; print $n++, "\n" while ($n < 10); # 0 - 9 print $n, "\n\n"; # 10 $n = 0; until ($n >= 10) { print $n++, "\n" # 0 - 9 } print $n, "\n\n"; # 10 $…

do 〜 while

do 〜 while Perl $n = 0; do { print $n++, "\n" # 0 - 9 } while ($n < 10); print $n, "\n\n"; # 10 $n = 0; print $n++, "\n" while ($n < 10); # 0 - 9 print $n, "\n\n"; # 10 $n = 0; do { print $n++, "\n" # 0 - 9 } until ($n >= 10); print $n, "…

if, else

if, else Perl $n = 2; if ( 0 == $n ) { print "no hits\n" } elsif ( 1 == $n ) { print "1 hit\n" } else { print "$n hits\n" # 2 hits } unless ( 0 != $n ) { print "no hits\n" } else { print "$n hits\n" # 2 hits } print "$n hits\n" if ($n > 1)…

operators

operators Perl $a = 5; $b = 2; printf("a + b = %d\n", $a + $b ); # a + b = 7 printf("a - b = %d\n", $a - $b ); # a - b = 3 printf("a * b = %d\n", $a * $b ); # a * b = 10 printf("a ** b = %d\n", $a ** $b ); # a ** b = 25 printf("a / b = %f\…

swap

swap Perl $a = 1; $b = 2; print "a = ", $a, "\n"; # a = 1 print "b = ", $b, "\n"; # b = 2 print "\n"; ($a, $b) = ($b, $a); print "a = ", $a, "\n"; # a = 2 print "b = ", $b, "\n"; # b = 1 Ruby a = 1 b = 2 print "a = ", a, "\n" # a = 1 print…

parallel assignment

parallel assignment Perl ($a, $b, $c) = (1, 2, 3); print "a = ", $a, "\n"; # a = 1 print "b = ", $b, "\n"; # b = 2 print "c = ", $c, "\n"; # c = 3 print "\n"; # 6 is ignored: ($a, $b) = (4, 5, 6); print "a = ", $a, "\n"; # a = 4 print "b =…

assignment

assignment Perl $a = 1; print "a = ", $a, "\n"; # a = 1 $a = $b = 2; print "a = ", $a, "\n"; # a = 2 print "b = ", $b, "\n"; # b = 2 Ruby a = 1 print "a = ", a, "\n" # a = 1 a = b = 2 print "a = ", a, "\n" # a = 2 print "b = ", b, "\n" # b…

Hello, World!

Hello, World! Perl print 'Hello, World!\n'; # Hello, World!\n print "\n"; print "Hello, World!\n"; # Hello, World! $var1 = "Hello"; $var2 = "World"; print $var1, ", ", $var2, "!", "\n"; # Hello, World! print "$var1, $var2!\n"; # Hello, Wor…

reflection and hooks

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 reflection and hooks perl php python ruby class ref $a type(a) a.class has method? $a->can('reverse') hasattr(a,'reverse') a.respond…

objects

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 objects perl php python ruby define class package Int; use Moose; has 'value' => ( is => 'rw' ); around BUILDARGS => sub { my $orig = …

libraries and modules

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 libraries and modules perl php python ruby load library require 'Foo.pm'; # or require Foo; # or use Foo; import foo require 'foo' # …

environment and i/o

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 environment and i/o perl php python ruby external command system('ls'); import os os.system('ls') system('ls') backticks `ls`; impor…

execution control

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 execution control perl php python ruby if if ( 0 == $n ) { print "no hits\n" } elsif ( 1 == $n ) { print "1 hit\n" } else { print "$n …

functions

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 functions perl php python ruby function declaration sub add { $_[0] + $_[1] } def add(a,b): return a+b def add(a,b); a+b; end functio…

containers

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 containers perl php python ruby array literal @nums = (1,2,3,4); nums = [1,2,3,4] nums = [1,2,3,4] array size $#nums + 1 or scalar(@n…

strings

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 strings perl php python ruby character literal none none none none chr and ord chr(65) ord("A") chr(65) ord('A') 65.chr "A".ord strin…

arithmetic and logic

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 arithmetic and logic perl php python ruby true and false 1 0 True False true false falsehoods undef 0 0.0 '' '0' () False None 0 0.0…

スクリプト言語の比較

スクリプト言語の比較 http://hyperpolyglot.org/scripting よりshow versionPHP$ php --versionPerl$ perl --versionPython$ python -VRuby$ ruby --version interpreterPHP$ php -f foo.phpPerl$ perl foo.plPython$ python foo.pyRuby$ ruby foo.rb replP…