wiki:Dev/Perl

Version 54 (modified by mitty, 11 years ago) (diff)

--

perlbrew

CPAN

cpanminus

misc

  • JPerl Advent Calendar 2010

    今年も JPerl Advent Calendar の季節がやってきました。 Perl に関連する tips を25本おとどけしちゃいます。 12月1日から毎日一本ずつ tips をアップしていっちゃうという企画です。 くわしくはこのあたりをみてください。

  • htmltree => cpan:HTML::TreeBuilder
    #!/usr/bin/perl
    # Time-stamp: "2000-10-02 14:48:15 MDT"
    #
    # Parse the given HTML file(s) and dump the parse tree
    # Usage:
    #  htmltree -D3 -w file1 file2 file3
    #    -D[number]  sets HTML::TreeBuilder::Debug to that figure.
    #    -w  turns on $tree->warn(1) for the new tree
    
    (snip)
    
  • CPAN:Parallel::ForkManager

    SYNOPSIS

    use Parallel::ForkManager;
    
    $pm = new Parallel::ForkManager($MAX_PROCESSES);
    
    foreach $data (@all_data) {
      # Forks and returns the pid for the child:
      my $pid = $pm->start and next; 
    
      ... do some work with $data in the child process ...
    
      $pm->finish; # Terminates the child process
    }
    
  • CPAN:Convert::Bencode

    No error detection of bencoded data. Damaged input will most likely cause very bad things to happen, up to and including causeing the bdecode function to recurse infintly.

    • oh...
  • cpan:Class::Accessor::Fast
    • newする際に初期値を与えないと、getterはundefを返す
    1. Foo.pm
      package Foo;
      use base qw(Class::Accessor::Fast);
      
      __PACKAGE__->mk_accessors( qw(bar bazz) );
      
      1;
      
    2. main.pl
      use Foo;
      use feature say;
      
      my $foo = Foo->new( {bar => 1} );
      if (defined $foo->bar) { say $foo->bar; }
      if (! defined $foo->bazz) { say "undef"; }
      $foo->hoge;
      
    3. $ ./main.pl
      1
      undef
      Can't locate object method "hoge" via package "Foo" at main.pl line 7.
      
  • 放り込んだハッシュリファレンスは元の構造のままアクセスできる
    1. main.pl
      use Foo;
      use feature say;
      
      my $foo = Foo->new( {hoge => 1, fuga => 2, piyo => { moge => [ a => b => c => 1 ] }} );
      if (defined $foo->bar) { say $foo->bar; }
      
      use Data::Dumper;
      say Dumper $foo;
      
    2. $ ./main.pl
      $VAR1 = bless( {
                       'piyo' => {
                                   'moge' => [
                                               'a',
                                               'b',
                                               'c',
                                               1
                                             ]
                                 },
                       'fuga' => 2,
                       'hoge' => 1
                     }, 'Foo' );
      
      
  • Class::Accessor::Fast - アクセサの作成 / Perlモジュール徹底解説 - サンプルコードによるPerl入門
    • newのオーバーライドは$class->SUPER::new()で可能。