RE: Help with POE::Filter::JSON

"Votava, Craig M \(Craig\)" <[email protected]>
Newsgroups gmane.comp.lang.perl.poe
Message-ID <EDC714147512684D9BFBE6AB9C53AED101AB3BA4@ILEXC1U03.ndc.lucent.com>
Alex-

 

Thanks for the response.

 

No, I'm not doing any $filter->get() in my program. This is what
confuses me a bit, as I don't have to do a $filter->get when I use other
filters such as POE::Filter::Line(), or POE::Filter::Stream(). Is
POE::Filter::JSON different from the others, such that I have to
explicitly do a get, even after I've put it in the filter stack (I'm
guessing yes here, but it is rather confusing for a first time user)?

 

Given that I have to do it, where should it appear in my code? I've
tried changing the InputEvent in display to be:

 

            display => sub{

                InputEvent=>$_[KERNEL]->post(Display=>data=>

                                    $filters->get([ $_[ARG0] ]) ), },


 

But that's not right. I guess I'm still confused.

 

Thanks

 

-Craig

 

________________________________

From: Alex _ [mailto:[email protected]] 
Sent: Monday, October 27, 2008 9:26 AM
To: Votava, Craig M (Craig); POE Mailing List
Subject: Re: Help with POE::Filter::JSON

 

are you doing filter->get() anywhere to do the conversion? 

 

 

----- Original Message ----
From: "Votava, Craig M (Craig)" <[email protected]>
To: POE Mailing List <[email protected]>
Sent: Monday, October 27, 2008 8:54:28 AM
Subject: Help with POE::Filter::JSON

Folks-



I'm trying to learn how to use POE::Filter::JSON, but am having some
problems (none of the input is being un-JSON-ified).



Attached is my code so far.



Any suggestions or pointers are much appreciated.



Thanks



-Craig



Use this as the contents of the input file (or any json input):
{"Text":["Hi there...","How are
you?","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15",
"16","17","18","19","20","21","22","23","24","24","26","27","28","29"]}

Here is the script:
use strict;
use warnings;

use Tk; # This MUST come before use POE...
use POE;
use POE::Wheel::FollowTail;
use POE::Filter::Stackable;
use POE::Filter::Line;
use POE::Filter::JSON;
use Data::Dumper;

my $infile = shift || die "Missing Input File...";

_displayData();

my $sess = _processData($infile);

# This should simply create a dummy postback on the tail session 
my $subref = $sess->postback('DontDie');

print STDERR "STARTING to process data...\n";
POE::Kernel->run;
exit;

sub _processData{
    my $ifile=shift || die "Missing input file";
    POE::Session->create(
        inline_states=>{
            _start=>sub {
                my $filters;
                print "Session ", $_[SESSION]->ID, " has started.\n";
                $_[KERNEL]->alias_set("myClient");
                my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
                # Setup filters in filter stack...
                $filters = POE::Filter::Stackable->new(
                    Filters=>[
                        POE::Filter::Line->new(),
                        POE::Filter::JSON->new(
                        json_any => {
                            allow_nonref =>1,
                        },
                        ),
                    ],
                );
                $_[HEAP]->{wheel}=POE::Wheel::FollowTail->new(
                    Seek=>0,
                    Filename=>$ifile,
                    InputEvent=>'display',
                    Filter=> $filters,
                );
            },
            display => sub{
                InputEvent=>$_[KERNEL]->post(Display=>data=>$_[ARG0]),
            },    
            sendStop  => sub{
                print "Stopping file input\n";
                my $heap = $_[HEAP];
                delete $heap->{wheel};
            },    
        },
    ),
}


sub _displayData{    

    my $hwid = shift;
    my $text;

    POE::Session->create( inline_states => {
        _start => sub { 
                $_[KERNEL]->alias_set("Display");
                my $kernel=$_[KERNEL];    

                my $top = $poe_main_window;

                # Create Stop button...
                $top->Button(-text=>"Stop", -command=>sub{
                    &stop($kernel);
                    $top->update;
                } )->pack(-side=>'bottom');    

                # Create top level Tree...
                $text = $top->Scrolled('Text',
                    -scrollbars=>'osow')->
                    pack(-fill=>'both', -side=>'left',
                          -expand=>1);

            },
        data=>sub{
            my $node=$_[ARG0] || return;
            #my $txt = join("\n", @{$node->{Text}});
            $text->insert('end', Dumper($node) . "\n");

            $text->update;
        },
    });
}

sub stop{
    my $ker = shift;
    if ($ker){    
print STDERR "Sending sendStop to myClient\n";
        $ker->call("myClient","sendStop");
    }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.