SlideShare ist ein Scribd-Unternehmen logo
1 von 131
Downloaden Sie, um offline zu lesen
.
                                                                                      .
.
                          Perl
..                                                                                .




                                                                                      .
                               @yoshiyuki kondo

                          PerlCasual #4 (2011/4/28)




     (@yoshiyuki kondo)           Perl                PerlCasual #4 (2011/4/28)   1 / 44
(          )




(@yoshiyuki kondo)       Perl       PerlCasual #4 (2011/4/28)   2 / 44
(                  )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/




(@yoshiyuki kondo)          Perl                  PerlCasual #4 (2011/4/28)   2 / 44
(                    )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                   Perl
                          Perl




(@yoshiyuki kondo)          Perl                  PerlCasual #4 (2011/4/28)   2 / 44
(                        )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                       Perl
                            Perl

                         C
                         Java




(@yoshiyuki kondo)              Perl              PerlCasual #4 (2011/4/28)   2 / 44
(                        )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                       Perl
                             Perl

                         C
                         Java

                         :



(@yoshiyuki kondo)              Perl              PerlCasual #4 (2011/4/28)   2 / 44
(                           )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                          Perl
                                 Perl

                         C
                         Java

                         :
                             :


(@yoshiyuki kondo)                 Perl              PerlCasual #4 (2011/4/28)   2 / 44
(                           )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                          Perl
                                 Perl

                         C
                         Java

                         :
                             :   Emacs

(@yoshiyuki kondo)                 Perl              PerlCasual #4 (2011/4/28)   2 / 44
(                             )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                            Perl
                                 Perl

                         C
                         Java

                         :
                             :   Emacs
                                        :

(@yoshiyuki kondo)                 Perl                PerlCasual #4 (2011/4/28)   2 / 44
(                           )
       Twitter
                 @yoshiyuki_kondo

                 http://www.kondoyoshiyuki.com/

                                          Perl
                                 Perl

                         C
                         Java

                         :
                             :   Emacs
                                   : T-Code (2                             )

(@yoshiyuki kondo)                 Perl              PerlCasual #4 (2011/4/28)   2 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   3 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   4 / 44
Perl




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   4 / 44
Perl




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   4 / 44
Perl

                      Perl




 (@yoshiyuki kondo)          Perl   PerlCasual #4 (2011/4/28)   4 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   5 / 44
use strict
use warnings




 (@yoshiyuki kondo)   Perl    PerlCasual #4 (2011/4/28)   5 / 44
use strict
 use warnings

#!/usr/bin/perl

use strict;
use warnings;




   (@yoshiyuki kondo)   Perl    PerlCasual #4 (2011/4/28)   5 / 44
use strict




     (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   6 / 44
use strict
    use strict

                                 OK




     (@yoshiyuki kondo)   Perl    PerlCasual #4 (2011/4/28)   6 / 44
use strict
    use strict

                                     OK
    #!/usr/bin/perl
    use strict;
    use warnings;

    $foo = 10;            #   $foo
    my $bar = 2;
    print $foo, "n";     #   $foo
    print $bar, "n";
    print $x, "n";       #   $x




     (@yoshiyuki kondo)       Perl    PerlCasual #4 (2011/4/28)   6 / 44
use strict
    use strict

                                            OK
    #!/usr/bin/perl
    use strict;
    use warnings;

    $foo = 10;            #   $foo
    my $bar = 2;
    print $foo, "n";     #   $foo
    print $bar, "n";
    print $x, "n";       #   $x




    Global symbol "$foo" requires explicit package name at ....



     (@yoshiyuki kondo)       Perl            PerlCasual #4 (2011/4/28)   6 / 44
warnings




    (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   7 / 44
warnings
   use warnings




    (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   7 / 44
warnings
   use warnings


   #!/usr/bin/perl
   use strict;
   use warnings;

   my $foo = "10xxx";
   my $x    = $foo + 5; # $foo
   my $bar;
   print "$barn";      # $bar   undef




    (@yoshiyuki kondo)    Perl       PerlCasual #4 (2011/4/28)   7 / 44
warnings
   use warnings


   #!/usr/bin/perl
   use strict;
   use warnings;

   my $foo = "10xxx";
   my $x    = $foo + 5; # $foo
   my $bar;
   print "$barn";      # $bar                    undef



   Argument "10xxx" isn’t numeric in addition (+) at ....
   Use of uninitialized value $bar in concatenation (.) or string at ....

    (@yoshiyuki kondo)        Perl                     PerlCasual #4 (2011/4/28)   7 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   8 / 44
#!/usr/bin/perl

use strict;
use warnings;




   (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   8 / 44
#!/usr/bin/perl

use strict;
use warnings;




   (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   8 / 44
#!/usr/bin/perl

use strict;
use warnings;




                               !!
   (@yoshiyuki kondo)   Perl        PerlCasual #4 (2011/4/28)   8 / 44
open




                              open




(@yoshiyuki kondo)     Perl      PerlCasual #4 (2011/4/28)   9 / 44
open




                      Perl
open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n";
while (<INPUT>) {
    print $_;
}
close INPUT;




 (@yoshiyuki kondo)            Perl                 PerlCasual #4 (2011/4/28)   10 / 44
open




                      Perl
open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n";
while (<INPUT>) {
    print $_;
}
close INPUT;




 (@yoshiyuki kondo)            Perl                 PerlCasual #4 (2011/4/28)   10 / 44
open




                      Perl
open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n";
while (<INPUT>) {
    print $_;
}
close INPUT;




open my $input, "<", "mydata" or die "Cannot open ’mydata’: $!n";
while (<$input>) {
    print $_;
}
close $input;




 (@yoshiyuki kondo)            Perl                 PerlCasual #4 (2011/4/28)   10 / 44
open




                      Perl
open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n";
while (<INPUT>) {
    print $_;
}
close INPUT;




open my $input, "<", "mydata" or die "Cannot open ’mydata’: $!n";
while (<$input>) {
    print $_;
}
close $input;



            $input

 (@yoshiyuki kondo)            Perl                 PerlCasual #4 (2011/4/28)   10 / 44
open




3                open
open my $input, "<", "mydata"




 (@yoshiyuki kondo)       Perl   PerlCasual #4 (2011/4/28)   11 / 44
open




3                open
open my $input, "<", "mydata"
             1

                                 1                      my




 (@yoshiyuki kondo)       Perl       PerlCasual #4 (2011/4/28)   11 / 44
open




3                open
open my $input, "<", "mydata"
             1

                                 1                      my

             2




 (@yoshiyuki kondo)       Perl       PerlCasual #4 (2011/4/28)   11 / 44
open




3                open
open my $input, "<", "mydata"
             1

                                 1                      my

             2
             3




 (@yoshiyuki kondo)       Perl       PerlCasual #4 (2011/4/28)   11 / 44
open




                      open    1




(@yoshiyuki kondo)     Perl       PerlCasual #4 (2011/4/28)   12 / 44
open




                       open    1



while (<$input>) { ......
my $line = <$input>;




 (@yoshiyuki kondo)     Perl       PerlCasual #4 (2011/4/28)   12 / 44
open




                       open      1



while (<$input>) { ......
my $line = <$input>;


print {$output} "This is a linen";   #
print $output "This is a linen";     #




 (@yoshiyuki kondo)     Perl              PerlCasual #4 (2011/4/28)   12 / 44
open




                       open      1



while (<$input>) { ......
my $line = <$input>;


print {$output} "This is a linen";   #
print $output "This is a linen";     #
print $output, "This is a linen";    #




 (@yoshiyuki kondo)     Perl              PerlCasual #4 (2011/4/28)   12 / 44
open




                       open      1



while (<$input>) { ......
my $line = <$input>;


print {$output} "This is a linen";   #
print $output "This is a linen";     #
print $output, "This is a linen";    #

IO::File
$output->print("This is a line datan");


 (@yoshiyuki kondo)     Perl               PerlCasual #4 (2011/4/28)   12 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   13 / 44
reference




(@yoshiyuki kondo)      Perl     PerlCasual #4 (2011/4/28)   14 / 44
reference


C




(@yoshiyuki kondo)      Perl     PerlCasual #4 (2011/4/28)   14 / 44
reference


C


                                 etc.




(@yoshiyuki kondo)      Perl            PerlCasual #4 (2011/4/28)   14 / 44
C
       $scaler_ref = $scalar;   #




(@yoshiyuki kondo)   Perl            PerlCasual #4 (2011/4/28)   15 / 44
C
       $scaler_ref = $scalar;    #
       $array_ref    = @array;   #




(@yoshiyuki kondo)    Perl            PerlCasual #4 (2011/4/28)   15 / 44
C
       $scaler_ref = $scalar;    #
       $array_ref    = @array;   #
       $hash_ref     = %hash;    #




(@yoshiyuki kondo)    Perl            PerlCasual #4 (2011/4/28)   15 / 44
C
       $scaler_ref = $scalar;       #
       $array_ref    = @array;      #
       $hash_ref     = %hash;       #
       $func_ref     = &function;   #




(@yoshiyuki kondo)    Perl               PerlCasual #4 (2011/4/28)   15 / 44
C
       $scaler_ref = $scalar;       #
       $array_ref    = @array;      #
       $hash_ref     = %hash;       #
       $func_ref     = &function;   #
       $glob_ref     = *STDOUT;     #




(@yoshiyuki kondo)    Perl               PerlCasual #4 (2011/4/28)   15 / 44
C
       $scaler_ref = $scalar;       #
       $array_ref    = @array;      #
       $hash_ref     = %hash;       #
       $func_ref     = &function;   #
       $glob_ref     = *STDOUT;     #




(@yoshiyuki kondo)    Perl               PerlCasual #4 (2011/4/28)   15 / 44
dereference




(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   16 / 44
dereference

Perl




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   16 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   17 / 44
$sref = $x;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   17 / 44
$sref = $x;

$a = $$ref;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   17 / 44
$sref = $x;

$a = $$ref;

my $x = 100;
my $sref = $x; #                               $sref

print "$$sref $xn";         # 100 100
$$sref += 5;
print "$$sref $xn";         # 105 105
 (@yoshiyuki kondo)   Perl               PerlCasual #4 (2011/4/28)   17 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   18 / 44
$array_ref = @array;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   18 / 44
$array_ref = @array;



@s = @$array_ref;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   18 / 44
$array_ref = @array;



@s = @$array_ref;

$$array_ref[10] = 10;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   18 / 44
$array_ref = @array;



@s = @$array_ref;

$$array_ref[10] = 10;
foreach
foreach (@$array_ref) {




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   18 / 44
compute sum:

sub compute_sum {
    my ($aref) = @_; #           $aref
    my $sum = 0;
    foreach (@$aref) { #
        $sum += $_;
    }
    return $sum;
}




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   19 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   20 / 44
$href = %h;




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   20 / 44
$href = %h;



$x = $$href{’a’};




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   20 / 44
$href = %h;



$x = $$href{’a’};
keys
foreach (keys %$href) {




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   20 / 44
(coderef)




(@yoshiyuki kondo)          Perl   PerlCasual #4 (2011/4/28)   21 / 44
(coderef)

$fref = &func;




 (@yoshiyuki kondo)          Perl   PerlCasual #4 (2011/4/28)   21 / 44
(coderef)

$fref = &func;


&$fref($param);




 (@yoshiyuki kondo)          Perl   PerlCasual #4 (2011/4/28)   21 / 44
$aref = @array;
$$aref = 10;
       Not a SCALAR reference at - line 7.




(@yoshiyuki kondo)      Perl                 PerlCasual #4 (2011/4/28)   22 / 44
ref




                                  REF
                                  SCALAR
                                  ARRAY
                                  HASH
                                  CODE
                                  GLOB



      (@yoshiyuki kondo)   Perl            PerlCasual #4 (2011/4/28)   23 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   24 / 44
print




(@yoshiyuki kondo)           Perl   PerlCasual #4 (2011/4/28)   24 / 44
print
                                16
                     CODE(0xbb5b48), HASH(0xbb59f8)




(@yoshiyuki kondo)           Perl             PerlCasual #4 (2011/4/28)   24 / 44
print
                                16
                     CODE(0xbb5b48), HASH(0xbb59f8)
eq




(@yoshiyuki kondo)           Perl             PerlCasual #4 (2011/4/28)   24 / 44
print
                                16
                     CODE(0xbb5b48), HASH(0xbb59f8)
eq




(@yoshiyuki kondo)           Perl             PerlCasual #4 (2011/4/28)   24 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   25 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   25 / 44
${$sref}




(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   25 / 44
${$sref}

@{&get_array_ref()}




(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   25 / 44
${$sref}

@{&get_array_ref()}
${$a[2]} $$a[2]
       $$a[2]        ${$a}[2]


(@yoshiyuki kondo)        Perl   PerlCasual #4 (2011/4/28)   25 / 44
${$a[2]}           $$a[2]




    (@yoshiyuki kondo)      Perl   PerlCasual #4 (2011/4/28)   26 / 44
->
     $a->[3]              $$a[3]




     (@yoshiyuki kondo)            Perl   PerlCasual #4 (2011/4/28)   27 / 44
->
     $a->[3] $$a[3]
     $h->{key} $$h{key}




     (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   27 / 44
->
     $a->[3] $$a[3]
     $h->{key} $$h{key}
     $f->($param) &$f($param)




     (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   27 / 44
->
     $a->[3] $$a[3]
     $h->{key} $$h{key}
     $f->($param) &$f($param)

            $a->[3]->[2]




     (@yoshiyuki kondo)    Perl   PerlCasual #4 (2011/4/28)   27 / 44
->
     $a->[3] $$a[3]
     $h->{key} $$h{key}
     $f->($param) &$f($param)

            $a->[3]->[2]
     ->                   [],{} ()          ->
            $a->[3]->[2]       $a->[2][3]




     (@yoshiyuki kondo)        Perl              PerlCasual #4 (2011/4/28)   27 / 44
$a[3][2][5]




(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   28 / 44
$a[3][2][5]

                     $a[3]->[2]->[5]




(@yoshiyuki kondo)       Perl          PerlCasual #4 (2011/4/28)   28 / 44
$a[3][2][5]

           $a[3]->[2]->[5]
$a->[3][2][5]




(@yoshiyuki kondo)   Perl    PerlCasual #4 (2011/4/28)   28 / 44
$a[2][1]                 $a->[2][1]




    (@yoshiyuki kondo)         Perl   PerlCasual #4 (2011/4/28)   29 / 44
Perl




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   30 / 44
Perl


$a[2][3]




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   30 / 44
Perl


$a[2][3]

$a[2][1] = 100




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   30 / 44
(@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   31 / 44
compute sum
my @tmp_array = (1, 3, 5, 7, 9);
print "Total: ", compute_sum(@tmp_array), "n";

sub compute_sum {
    my ($aref) = @_;
    my $sum = 0;
    foreach (@$aref) {
        $sum += $_;
    }
    return $sum;
}



 (@yoshiyuki kondo)   Perl         PerlCasual #4 (2011/4/28)   32 / 44
compute sum
my @tmp_array = (1, 3, 5, 7, 9);
print "Total: ", compute_sum(@tmp_array), "n";

sub compute_sum {
    my ($aref) = @_;
    my $sum = 0;
    foreach (@$aref) {
        $sum += $_;
    }
    return $sum;
}
                      @tmp array

 (@yoshiyuki kondo)       Perl           PerlCasual #4 (2011/4/28)   32 / 44
Perl                       (anonymous array)

                      []




 (@yoshiyuki kondo)        Perl            PerlCasual #4 (2011/4/28)   33 / 44
Perl                       (anonymous array)

                      []


   )
        5                                               $aref

        $aref = [1, 2, 3, 4, 5]




 (@yoshiyuki kondo)        Perl            PerlCasual #4 (2011/4/28)   33 / 44
Perl                       (anonymous array)

                      []


   )
        5                                               $aref

        $aref = [1, 2, 3, 4, 5]




 (@yoshiyuki kondo)        Perl            PerlCasual #4 (2011/4/28)   33 / 44
compute sum
print "Total: ", compute_sum([1, 3, 5, 7, 9]), "n";




 (@yoshiyuki kondo)    Perl       PerlCasual #4 (2011/4/28)   34 / 44
compute sum
print "Total: ", compute_sum([1, 3, 5, 7, 9]), "n";
              (1, 2, 3)          [1, 2, 3]




 (@yoshiyuki kondo)       Perl       PerlCasual #4 (2011/4/28)   34 / 44
anonymous
hash
                     {}




(@yoshiyuki kondo)        Perl   PerlCasual #4 (2011/4/28)   35 / 44
anonymous
hash
                     {}




  )                              3

$href = {one => 1, two => 2, three => 3};




(@yoshiyuki kondo)        Perl       PerlCasual #4 (2011/4/28)   35 / 44
=>
=>         (thick arrow)          (
Perl p121)




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)          (
Perl p121)




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)            (
Perl p121)

        =>                      (
            )




 (@yoshiyuki kondo)   Perl   PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)                     (
Perl p121)

        =>                               (
            )


        $href = {one => 1, two => 2, three => 3};




 (@yoshiyuki kondo)   Perl            PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)                     (
Perl p121)

        =>                               (
            )


        $href = {one => 1, two => 2, three => 3};

        $href = {’one’, 1, ’two’, 2, ’three’, 3 };




 (@yoshiyuki kondo)   Perl            PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)                     (
Perl p121)

        =>                               (
            )


        $href = {one => 1, two => 2, three => 3};

        $href = {’one’, 1, ’two’, 2, ’three’, 3 };


                      =>

 (@yoshiyuki kondo)        Perl       PerlCasual #4 (2011/4/28)   36 / 44
=>
=>         (thick arrow)                        (
Perl p121)

           =>                               (
               )


           $href = {one => 1, two => 2, three => 3};

           $href = {’one’, 1, ’two’, 2, ’three’, 3 };


                         =>
(                                                     )
    (@yoshiyuki kondo)        Perl       PerlCasual #4 (2011/4/28)   36 / 44
(                )

my $userinfo = {         name => $name,
                         uid => $uid,
                         gid => [1, 200, 201, 203],
                         env => {
                               HOME => $ENV{HOME},
                               TERM => "VT100" } };




    (@yoshiyuki kondo)   Perl           PerlCasual #4 (2011/4/28)   37 / 44
Data::Dumper


                                ?




    (@yoshiyuki kondo)   Perl       PerlCasual #4 (2011/4/28)   38 / 44
Data::Dumper


                                          ?
                                HASH(0x116360)




    (@yoshiyuki kondo)   Perl                 PerlCasual #4 (2011/4/28)   38 / 44
Data::Dumper


                                                     ?
                             HASH(0x116360)
                 Data::Dumper
   $VAR1 = {
               ’uid’ => 1001,
               ’env’ => {
                           ’HOME’ => ’/home/cond’,
                           ’TERM’ => ’VT100’
                        },
               ’name’ => ’John’,
               ’gid’ => [
                           1,
                           200,
                           201,
                           203
                        ]
             };

    (@yoshiyuki kondo)           Perl                    PerlCasual #4 (2011/4/28)   38 / 44
(   )




                                    (              )




(@yoshiyuki kondo)           Perl       PerlCasual #4 (2011/4/28)   39 / 44
(   )




Perl                                 anonymous
subroutine




 (@yoshiyuki kondo)           Perl        PerlCasual #4 (2011/4/28)   40 / 44
(   )




Perl                                 anonymous
subroutine
        sub { ..... }




 (@yoshiyuki kondo)           Perl        PerlCasual #4 (2011/4/28)   40 / 44
(   )




Perl                                 anonymous
subroutine
        sub { ..... }




 (@yoshiyuki kondo)           Perl        PerlCasual #4 (2011/4/28)   40 / 44
(   )




Perl                                 anonymous
subroutine
        sub { ..... }


                                           fref
$fref = sub { print "Hello, $_[0]n"; }




 (@yoshiyuki kondo)           Perl        PerlCasual #4 (2011/4/28)   40 / 44
(   )




Perl                                 anonymous
subroutine
        sub { ..... }


                                           fref
$fref = sub { print "Hello, $_[0]n"; }

&$fref("world");




 (@yoshiyuki kondo)           Perl        PerlCasual #4 (2011/4/28)   40 / 44
(   )




Perl                                     anonymous
subroutine
        sub { ..... }


                                               fref
$fref = sub { print "Hello, $_[0]n"; }

&$fref("world");
->                                                             (
                                     )
$fref->("world");

 (@yoshiyuki kondo)           Perl            PerlCasual #4 (2011/4/28)   40 / 44
(       )




                         (closure)      ?

                                 (my    )




(@yoshiyuki kondo)               Perl       PerlCasual #4 (2011/4/28)   41 / 44
(   )



     adder:


sub adder {
    my ($increment) = @_;             #

    return sub {
        my ($x) = @_;
        return $x + $increment;
    }
}

my $add1 = adder(1);               # 1
my $add20 = adder(20);             # 20

print $add1->(10), "n";             # 11
print $add20->(10), "n";            # 30
print $add1->(30), "n";             # 31
      (@yoshiyuki kondo)             Perl   PerlCasual #4 (2011/4/28)   42 / 44
(    )



        make counter:

sub make_counter {
    my ($counter) = @_;                  #

    return sub {             return ++$counter };
}

my $foo = make_counter(0);   #                  0
my $bar = make_counter(100); #                  100

print   $foo->(),        "n";     #   1
print   $bar->(),        "n";     #   101
print   $bar->(),        "n";     #   102
print   $foo->(),        "n";     #   2
print   $bar->(),        "n";     #   103
print   $foo->(),        "n";     #   3

        (@yoshiyuki kondo)              Perl          PerlCasual #4 (2011/4/28)   43 / 44
(   )




(@yoshiyuki kondo)           Perl   PerlCasual #4 (2011/4/28)   44 / 44
(   )




                                     use strict
use warnings




 (@yoshiyuki kondo)           Perl   PerlCasual #4 (2011/4/28)   44 / 44

Weitere ähnliche Inhalte

Kürzlich hochgeladen

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 

Kürzlich hochgeladen (20)

The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 

Empfohlen

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Empfohlen (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Perl中級者への道 (近藤 嘉雪, at PerlCasual #4)

  • 1. . . . Perl .. . . @yoshiyuki kondo PerlCasual #4 (2011/4/28) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 1 / 44
  • 2. ( ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 3. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 4. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 5. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 6. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java : (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 7. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java : : (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 8. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java : : Emacs (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 9. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java : : Emacs : (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 10. ( ) Twitter @yoshiyuki_kondo http://www.kondoyoshiyuki.com/ Perl Perl C Java : : Emacs : T-Code (2 ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 2 / 44
  • 11. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 3 / 44
  • 12. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 4 / 44
  • 13. Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 4 / 44
  • 14. Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 4 / 44
  • 15. Perl Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 4 / 44
  • 16. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 5 / 44
  • 17. use strict use warnings (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 5 / 44
  • 18. use strict use warnings #!/usr/bin/perl use strict; use warnings; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 5 / 44
  • 19. use strict (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 6 / 44
  • 20. use strict use strict OK (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 6 / 44
  • 21. use strict use strict OK #!/usr/bin/perl use strict; use warnings; $foo = 10; # $foo my $bar = 2; print $foo, "n"; # $foo print $bar, "n"; print $x, "n"; # $x (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 6 / 44
  • 22. use strict use strict OK #!/usr/bin/perl use strict; use warnings; $foo = 10; # $foo my $bar = 2; print $foo, "n"; # $foo print $bar, "n"; print $x, "n"; # $x Global symbol "$foo" requires explicit package name at .... (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 6 / 44
  • 23. warnings (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 7 / 44
  • 24. warnings use warnings (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 7 / 44
  • 25. warnings use warnings #!/usr/bin/perl use strict; use warnings; my $foo = "10xxx"; my $x = $foo + 5; # $foo my $bar; print "$barn"; # $bar undef (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 7 / 44
  • 26. warnings use warnings #!/usr/bin/perl use strict; use warnings; my $foo = "10xxx"; my $x = $foo + 5; # $foo my $bar; print "$barn"; # $bar undef Argument "10xxx" isn’t numeric in addition (+) at .... Use of uninitialized value $bar in concatenation (.) or string at .... (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 7 / 44
  • 27. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 8 / 44
  • 28. #!/usr/bin/perl use strict; use warnings; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 8 / 44
  • 29. #!/usr/bin/perl use strict; use warnings; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 8 / 44
  • 30. #!/usr/bin/perl use strict; use warnings; !! (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 8 / 44
  • 31. open open (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 9 / 44
  • 32. open Perl open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n"; while (<INPUT>) { print $_; } close INPUT; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 10 / 44
  • 33. open Perl open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n"; while (<INPUT>) { print $_; } close INPUT; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 10 / 44
  • 34. open Perl open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n"; while (<INPUT>) { print $_; } close INPUT; open my $input, "<", "mydata" or die "Cannot open ’mydata’: $!n"; while (<$input>) { print $_; } close $input; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 10 / 44
  • 35. open Perl open INPUT, "<mydata" or die "Cannot open ’mydata’: $!n"; while (<INPUT>) { print $_; } close INPUT; open my $input, "<", "mydata" or die "Cannot open ’mydata’: $!n"; while (<$input>) { print $_; } close $input; $input (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 10 / 44
  • 36. open 3 open open my $input, "<", "mydata" (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 11 / 44
  • 37. open 3 open open my $input, "<", "mydata" 1 1 my (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 11 / 44
  • 38. open 3 open open my $input, "<", "mydata" 1 1 my 2 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 11 / 44
  • 39. open 3 open open my $input, "<", "mydata" 1 1 my 2 3 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 11 / 44
  • 40. open open 1 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 12 / 44
  • 41. open open 1 while (<$input>) { ...... my $line = <$input>; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 12 / 44
  • 42. open open 1 while (<$input>) { ...... my $line = <$input>; print {$output} "This is a linen"; # print $output "This is a linen"; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 12 / 44
  • 43. open open 1 while (<$input>) { ...... my $line = <$input>; print {$output} "This is a linen"; # print $output "This is a linen"; # print $output, "This is a linen"; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 12 / 44
  • 44. open open 1 while (<$input>) { ...... my $line = <$input>; print {$output} "This is a linen"; # print $output "This is a linen"; # print $output, "This is a linen"; # IO::File $output->print("This is a line datan"); (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 12 / 44
  • 45. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 13 / 44
  • 46. reference (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 14 / 44
  • 47. reference C (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 14 / 44
  • 48. reference C etc. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 14 / 44
  • 49. C $scaler_ref = $scalar; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 50. C $scaler_ref = $scalar; # $array_ref = @array; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 51. C $scaler_ref = $scalar; # $array_ref = @array; # $hash_ref = %hash; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 52. C $scaler_ref = $scalar; # $array_ref = @array; # $hash_ref = %hash; # $func_ref = &function; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 53. C $scaler_ref = $scalar; # $array_ref = @array; # $hash_ref = %hash; # $func_ref = &function; # $glob_ref = *STDOUT; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 54. C $scaler_ref = $scalar; # $array_ref = @array; # $hash_ref = %hash; # $func_ref = &function; # $glob_ref = *STDOUT; # (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 15 / 44
  • 55. dereference (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 16 / 44
  • 56. dereference Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 16 / 44
  • 57. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 17 / 44
  • 58. $sref = $x; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 17 / 44
  • 59. $sref = $x; $a = $$ref; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 17 / 44
  • 60. $sref = $x; $a = $$ref; my $x = 100; my $sref = $x; # $sref print "$$sref $xn"; # 100 100 $$sref += 5; print "$$sref $xn"; # 105 105 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 17 / 44
  • 61. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 18 / 44
  • 62. $array_ref = @array; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 18 / 44
  • 63. $array_ref = @array; @s = @$array_ref; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 18 / 44
  • 64. $array_ref = @array; @s = @$array_ref; $$array_ref[10] = 10; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 18 / 44
  • 65. $array_ref = @array; @s = @$array_ref; $$array_ref[10] = 10; foreach foreach (@$array_ref) { (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 18 / 44
  • 66. compute sum: sub compute_sum { my ($aref) = @_; # $aref my $sum = 0; foreach (@$aref) { # $sum += $_; } return $sum; } (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 19 / 44
  • 67. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 20 / 44
  • 68. $href = %h; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 20 / 44
  • 69. $href = %h; $x = $$href{’a’}; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 20 / 44
  • 70. $href = %h; $x = $$href{’a’}; keys foreach (keys %$href) { (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 20 / 44
  • 71. (coderef) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 21 / 44
  • 72. (coderef) $fref = &func; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 21 / 44
  • 73. (coderef) $fref = &func; &$fref($param); (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 21 / 44
  • 74. $aref = @array; $$aref = 10; Not a SCALAR reference at - line 7. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 22 / 44
  • 75. ref REF SCALAR ARRAY HASH CODE GLOB (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 23 / 44
  • 76. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 24 / 44
  • 77. print (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 24 / 44
  • 78. print 16 CODE(0xbb5b48), HASH(0xbb59f8) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 24 / 44
  • 79. print 16 CODE(0xbb5b48), HASH(0xbb59f8) eq (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 24 / 44
  • 80. print 16 CODE(0xbb5b48), HASH(0xbb59f8) eq (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 24 / 44
  • 81. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 25 / 44
  • 82. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 25 / 44
  • 83. ${$sref} (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 25 / 44
  • 84. ${$sref} @{&get_array_ref()} (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 25 / 44
  • 85. ${$sref} @{&get_array_ref()} ${$a[2]} $$a[2] $$a[2] ${$a}[2] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 25 / 44
  • 86. ${$a[2]} $$a[2] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 26 / 44
  • 87. -> $a->[3] $$a[3] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 27 / 44
  • 88. -> $a->[3] $$a[3] $h->{key} $$h{key} (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 27 / 44
  • 89. -> $a->[3] $$a[3] $h->{key} $$h{key} $f->($param) &$f($param) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 27 / 44
  • 90. -> $a->[3] $$a[3] $h->{key} $$h{key} $f->($param) &$f($param) $a->[3]->[2] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 27 / 44
  • 91. -> $a->[3] $$a[3] $h->{key} $$h{key} $f->($param) &$f($param) $a->[3]->[2] -> [],{} () -> $a->[3]->[2] $a->[2][3] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 27 / 44
  • 92. $a[3][2][5] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 28 / 44
  • 93. $a[3][2][5] $a[3]->[2]->[5] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 28 / 44
  • 94. $a[3][2][5] $a[3]->[2]->[5] $a->[3][2][5] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 28 / 44
  • 95. $a[2][1] $a->[2][1] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 29 / 44
  • 96. Perl (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 30 / 44
  • 97. Perl $a[2][3] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 30 / 44
  • 98. Perl $a[2][3] $a[2][1] = 100 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 30 / 44
  • 99. (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 31 / 44
  • 100. compute sum my @tmp_array = (1, 3, 5, 7, 9); print "Total: ", compute_sum(@tmp_array), "n"; sub compute_sum { my ($aref) = @_; my $sum = 0; foreach (@$aref) { $sum += $_; } return $sum; } (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 32 / 44
  • 101. compute sum my @tmp_array = (1, 3, 5, 7, 9); print "Total: ", compute_sum(@tmp_array), "n"; sub compute_sum { my ($aref) = @_; my $sum = 0; foreach (@$aref) { $sum += $_; } return $sum; } @tmp array (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 32 / 44
  • 102. Perl (anonymous array) [] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 33 / 44
  • 103. Perl (anonymous array) [] ) 5 $aref $aref = [1, 2, 3, 4, 5] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 33 / 44
  • 104. Perl (anonymous array) [] ) 5 $aref $aref = [1, 2, 3, 4, 5] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 33 / 44
  • 105. compute sum print "Total: ", compute_sum([1, 3, 5, 7, 9]), "n"; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 34 / 44
  • 106. compute sum print "Total: ", compute_sum([1, 3, 5, 7, 9]), "n"; (1, 2, 3) [1, 2, 3] (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 34 / 44
  • 107. anonymous hash {} (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 35 / 44
  • 108. anonymous hash {} ) 3 $href = {one => 1, two => 2, three => 3}; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 35 / 44
  • 109. => => (thick arrow) ( Perl p121) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 110. => => (thick arrow) ( Perl p121) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 111. => => (thick arrow) ( Perl p121) => ( ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 112. => => (thick arrow) ( Perl p121) => ( ) $href = {one => 1, two => 2, three => 3}; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 113. => => (thick arrow) ( Perl p121) => ( ) $href = {one => 1, two => 2, three => 3}; $href = {’one’, 1, ’two’, 2, ’three’, 3 }; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 114. => => (thick arrow) ( Perl p121) => ( ) $href = {one => 1, two => 2, three => 3}; $href = {’one’, 1, ’two’, 2, ’three’, 3 }; => (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 115. => => (thick arrow) ( Perl p121) => ( ) $href = {one => 1, two => 2, three => 3}; $href = {’one’, 1, ’two’, 2, ’three’, 3 }; => ( ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 36 / 44
  • 116. ( ) my $userinfo = { name => $name, uid => $uid, gid => [1, 200, 201, 203], env => { HOME => $ENV{HOME}, TERM => "VT100" } }; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 37 / 44
  • 117. Data::Dumper ? (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 38 / 44
  • 118. Data::Dumper ? HASH(0x116360) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 38 / 44
  • 119. Data::Dumper ? HASH(0x116360) Data::Dumper $VAR1 = { ’uid’ => 1001, ’env’ => { ’HOME’ => ’/home/cond’, ’TERM’ => ’VT100’ }, ’name’ => ’John’, ’gid’ => [ 1, 200, 201, 203 ] }; (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 38 / 44
  • 120. ( ) ( ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 39 / 44
  • 121. ( ) Perl anonymous subroutine (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 122. ( ) Perl anonymous subroutine sub { ..... } (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 123. ( ) Perl anonymous subroutine sub { ..... } (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 124. ( ) Perl anonymous subroutine sub { ..... } fref $fref = sub { print "Hello, $_[0]n"; } (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 125. ( ) Perl anonymous subroutine sub { ..... } fref $fref = sub { print "Hello, $_[0]n"; } &$fref("world"); (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 126. ( ) Perl anonymous subroutine sub { ..... } fref $fref = sub { print "Hello, $_[0]n"; } &$fref("world"); -> ( ) $fref->("world"); (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 40 / 44
  • 127. ( ) (closure) ? (my ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 41 / 44
  • 128. ( ) adder: sub adder { my ($increment) = @_; # return sub { my ($x) = @_; return $x + $increment; } } my $add1 = adder(1); # 1 my $add20 = adder(20); # 20 print $add1->(10), "n"; # 11 print $add20->(10), "n"; # 30 print $add1->(30), "n"; # 31 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 42 / 44
  • 129. ( ) make counter: sub make_counter { my ($counter) = @_; # return sub { return ++$counter }; } my $foo = make_counter(0); # 0 my $bar = make_counter(100); # 100 print $foo->(), "n"; # 1 print $bar->(), "n"; # 101 print $bar->(), "n"; # 102 print $foo->(), "n"; # 2 print $bar->(), "n"; # 103 print $foo->(), "n"; # 3 (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 43 / 44
  • 130. ( ) (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 44 / 44
  • 131. ( ) use strict use warnings (@yoshiyuki kondo) Perl PerlCasual #4 (2011/4/28) 44 / 44