#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0

    Flush/ buffering POE::kernel


    simply i face 2 problems on POE:: modules.
    here is the code ;

    use strict;
    use warnings;
    use POE qw/
    Component::IRC::State
    Component::IRC::Plugin::AutoJoin
    Component::IRC::Plugin::Connector
    Component::IRC::Plugin::NickServID
    Component::IRC::Plugin::NickReclaim
    Component::IRC::State Component::IRC::Plugin::Proxy Component::IRC::Plugin::Connector
    /;

    my $nickname = 'nick';
    my $ircname = 'Lunaa-chkr';
    my $server = 'server';
    my $admin = 'admin';
    my @channels = ('channel');


    my $irc = POE::Component::IRC->spawn(
    nick => $nickname,
    ircname => $ircname,
    server => $server,
    ) or die "Oh noooo! $!";

    POE::Session->create(
    package_states => [
    main => [ qw( _default _start irc_public) ],
    ],
    heap => { irc => $irc },
    );

    $poe_kernel->run;

    sub _start {
    my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
    my $irc = $heap->{irc};
    $irc->yield( register => 'all' );
    $irc->yield( connect => { } );
    my %connector;
    $connector{delay} = $irc->{Opts}->{StonedCheck} || 300;
    $connector{reconnect} = $irc->{Opts}->{ReconnectDelay} || 60;
    $irc->plugin_add('Connector' =>
    POE::Component::IRC::Plugin::Connector->new(
    %connector
    ),
    );
    $irc->plugin_add('AutoJoin' =>
    POE::Component::IRC::Plugin::AutoJoin->new(
    Channels => \@channels,
    RejoinOnKick => $irc->{Opts}->{Chan_RetryAfterKick} // 5,
    Rejoin_delay => $irc->{Opts}->{Chan_RejoinDelay} // 10,
    NickServ_delay => $irc->{Opts}->{Chan_NickServDelay} // 10,
    Retry_when_banned => $irc->{Opts}->{Chan_RetryAfterBan} // 60,
    ),
    );
    $irc->plugin_add('NickReclaim' =>
    POE::Component::IRC::Plugin::NickReclaim->new(
    poll => $irc->{Opts}->{NickRegainDelay} // 30,
    ),
    );
    $irc->plugin_add( 'NickServID', POE::Component::IRC::Plugin::NickServID->new(
    Password => 'pass',
    ) );
    return;
    }

    sub irc_public {
    my ($kernel ,$sender, $who, $where, $what) = @_[KERNEL, SENDER, ARG0 .. ARG2];
    my $nick = ( split /!/, $who )[0];
    my $channel = $where->[0];



    if ($what =~ /^!hi/) {
    $irc->send( privmsg => $channel => "mssg 1" );

    my $countdown = 1*15;
    $| = 1;
    my $beg_time = time;
    my $end_time = $beg_time + $countdown;
    for (; {
    my $time = time;
    last if ($time >= $end_time);
    printf("\r%02d",($end_time - $time) % 60, );
    sleep(1);}

    $irc->yield( privmsg => $channel => "mssg 2" );

    }

    return;
    sub _default {
    my ($event, $args) = @_[ARG0 .. $#_];
    my @output = ( "$event: " );
    for my $arg ( @$args ) {
    if (ref $arg eq 'ARRAY') {
    push( @output, '[' . join(', ', @$arg ) . ']' );
    }else {
    push ( @output, "'$arg" );
    }
    }
    print join ' ', @output, "\n";
    return 0;
    }
    }

    first there is an error about arg warning.
    and 2nd its all about flush the out going messages. because when i type !hi the bot will start the timer and then dispatch all out going messages while i want it to dispatch a message and then start timer. any help???
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,941
    Rep Power
    1225
    Please use the code tags to retain the code indentation, which makes it much easier to read/follow the code.

    I have not worked with any POE scripts, but will see what I can do to help.

    first there is an error about arg warning.
    You should always post the error messages.

    and 2nd its all about flush the out going messages.
    Have you tried turning off output buffering?

    Add this after the use statements.
    Code:
    $|++;
    It's best practice to not define a sub inside another sub.

    I ran your script through Perl::Tidy to fix the lost indentation and made the couple adjustments I suggested.

    Code:
    use strict;
    use warnings;
    use POE qw/
      Component::IRC::State
      Component::IRC::Plugin::AutoJoin
      Component::IRC::Plugin::Connector
      Component::IRC::Plugin::NickServID
      Component::IRC::Plugin::NickReclaim
      Component::IRC::State
      Component::IRC::Plugin::Proxy
      Component::IRC::Plugin::Connector
      /;
    
    $|++;
    
    my $nickname = 'nick';
    my $ircname  = 'Lunaa-chkr';
    my $server   = 'server';
    my $admin    = 'admin';
    my @channels = ('channel');
    
    my $irc = POE::Component::IRC->spawn(
        nick    => $nickname,
        ircname => $ircname,
        server  => $server,
    ) or die "Oh noooo! $!";
    
    POE::Session->create(
        package_states => [ main => [qw( _default _start irc_public)], ],
        heap           => { irc  => $irc },
    );
    
    $poe_kernel->run;
    
    sub _start {
        my ( $self, $kernel, $heap ) = @_[ OBJECT, KERNEL, HEAP ];
        my $irc = $heap->{irc};
        $irc->yield( register => 'all' );
        $irc->yield( connect  => {} );
        my %connector;
        $connector{delay}     = $irc->{Opts}->{StonedCheck}    || 300;
        $connector{reconnect} = $irc->{Opts}->{ReconnectDelay} || 60;
        $irc->plugin_add(
            'Connector' => POE::Component::IRC::Plugin::Connector->new(%connector),
        );
        $irc->plugin_add(
            'AutoJoin' => POE::Component::IRC::Plugin::AutoJoin->new(
                Channels          => \@channels,
                RejoinOnKick      => $irc->{Opts}->{Chan_RetryAfterKick} // 5,
                Rejoin_delay      => $irc->{Opts}->{Chan_RejoinDelay} // 10,
                NickServ_delay    => $irc->{Opts}->{Chan_NickServDelay} // 10,
                Retry_when_banned => $irc->{Opts}->{Chan_RetryAfterBan} // 60,
            ),
        );
        $irc->plugin_add(
            'NickReclaim' => POE::Component::IRC::Plugin::NickReclaim->new(
                poll => $irc->{Opts}->{NickRegainDelay} // 30,
            ),
        );
        $irc->plugin_add( 'NickServID',
            POE::Component::IRC::Plugin::NickServID->new( Password => 'pass', ) );
        return;
    }
    
    sub irc_public {
        my ( $kernel, $sender, $who, $where, $what ) =
          @_[ KERNEL, SENDER, ARG0 .. ARG2 ];
        my $nick = ( split /!/, $who )[0];
        my $channel = $where->[0];
    
        if ( $what =~ /^!hi/ ) {
            $irc->send( privmsg => $channel => "mssg 1" );
    
            my $countdown = 1 * 15;
            $| = 1;
            my $beg_time = time;
            my $end_time = $beg_time + $countdown;
            for (
                ;
                {
                    my $time = time;
                    last if ( $time >= $end_time );
                    printf( "\r%02d", ( $end_time - $time ) % 60, );
                    sleep(1);
                }
    
            $irc->yield( privmsg => $channel => "mssg 2" ) ;
    
            }
    
            return;
    }
    
    sub _default {
        my ( $event, $args ) = @_[ ARG0 .. $#_ ];
        my @output = ("$event: ");
        for my $arg (@$args) {
            if ( ref $arg eq 'ARRAY' ) {
                push( @output, '[' . join( ', ', @$arg ) . ']' );
            }
            else {
                push( @output, "'$arg" );
            }
        }
        print join ' ', @output, "\n";
        return 0;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    12
    Rep Power
    0
    use of uninitialized value $arg in concatenation <.> or string at test.pl line x " push ( @output, "'$arg" ); "

    2nd. I tried to use $|++ for auto flush but it don't work for me :S
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,941
    Rep Power
    1225
    use of uninitialized value $arg in concatenation <.> or string at test.pl line x " push ( @output, "'$arg" ); "
    That's most likely due the the syntax error in this line:
    Code:
    my ($event, $args) = @_[ARG0 .. $#_];
    It's unclear to me what you're wanting to assign, but assuming @_ has more than 2 elements and you want the first and last, then the proper array slice syntax would be:
    Code:
    my ($event, $args) = @_[0, -1];
    Last edited by FishMonger; April 25th, 2013 at 11:05 AM.

IMN logo majestic logo threadwatch logo seochat tools logo