[Moscow.pm] weaken with

Vladimir Timofeev vovkasm gmail.com
27 23:39:29 PST 2011


2011/2/28 Ivan Petrov <i.petro.77.00  gmail.com>:
>
>> >  :
>> >
>> > sub method
>> > {
>> >  my $self = shift;
>> >  ...
>> >  $self->{handle} = AnyEvent::Handle fh => $fh, on_error => sub {
>> > $self->{error} = $! };
>> >  ...
>> > }
>> >
>> >       $self     
>> >         handle  .
>> >      - : weaken  .    
>> > weaken       ?
>>
>>  $self = shift;  weaken($self);
>
>         ,  
>  weaken         
> - .
>
>   :
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use utf8;
> use open qw(:std :utf8);
>
> package TestW;
> use AnyEvent::Handle;
> use Scalar::Util qw(weaken);
>
>
> sub new {
>  my ($class, $fh) = @_;
>
>  my $self = bless { } => $class;
> # weaken $self;
>  $self->{handle} = new AnyEvent::Handle
>  fh => $fh,
>  on_error => sub { $self->{error} = $! };
>  return $self;
> }
>
> package main;
> use Data::Dumper;
>
> my $o = new TestW(\*STDIN);
> print Dumper $o;
>
>    :
>
> $VAR1 = bless( {
>  'handle' => bless( {
>  '_activity' => '1298875336.78856',
>  '_ractivity' => '1298875336.78856',
>  '_wactivity' => '1298875336.78856',
>  'fh' => \*::STDIN,
>  'oobinline' => 1,
>  'on_error' => sub { "DUMMY" }
>  }, 'AnyEvent::Handle' )
>  }, 'TestW' );
>
>    weaken,      :
>
> $VAR1 = {
>  'handle' => bless( {
>  '_activity' => '1298875430.28919',
>  '_ractivity' => '1298875430.28919',
>  '_wactivity' => '1298875430.28919',
>  'fh' => \*::STDIN,
>  'oobinline' => 1,
>  'on_error' => sub { "DUMMY" }
>  }, 'AnyEvent::Handle' )
>  };
>
>   ?

   :
sub new {
    my ($class, $fh) = @_;
    my $self = bless( {}, $class);

    my $me = $self;
    weaken $me;

    $self->{handle} = AnyEvent::Handle->new(
        fh => $fh,
        on_error => sub {
          return unless $me;
          $me->{error} = $!
        }
    );

    return $self;
}

    weaken    ,   
 ,      .

> Perl 5.10.1
>
> --
> Moscow.pm mailing list
> moscow-pm  pm.org | http://moscow.pm.org
>
>



-- 
Vladimir Timofeev <vovkasm  gmail.com>


Moscow-pm