padawan_linuxero has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks
Yesterday you help me with my program which I must say, my boss like it, but now I come to you seeking knowledge or a bit more, now my boss wants to display a status of the activity of some banks, in my case I need to go and get a file from a server using IO::Socket and read the file, this file is a plain text file, which it will have something like this:
bank1:Active bank2:Non Active bank3:Active bank4:Active
this data someone perhaps me or somebody else is going to change it as the status of the bank changes, and the program must go every 3 or 5 minutes to check the file and change the value in the entry, and I need the program that I am working on to be able to read the file and fill the 4 Entry's I have right now in the program
#!/usr/bin/perl -W use strict; use warnings; use Tk; use Tk::BrowseEntry; use Cwd; use List::Util qw(shuffle); my $mw = MainWindow->new( -relief => 'raised', -bd => 2, ); $mw->title("Asociacion de Agentes Aduanales de Matamoros --- Validacio +n Automatica"); $mw->geometry("1024x764"); my $upperframe = $mw->Frame()->pack(-fill=>'x' ); my $topframe = $mw->Frame()->pack(-fill=>'x' ); my $mainframe = $mw->Frame()->pack(-expand=>1, -fill=>'both' ); my $bottomFrame = $mw->Frame()->pack(-fill=>'x'); my $exit_b = $topframe->Button(-text=>'Exit', -command=> sub{ exit } )->pack(-side => 'right', -padx=>20 +); my $label_wert1 = $upperframe->Label( -text => 'Banamex :', )->pack(-side => 'left', -expand => 1); my $entry_wert1 = $upperframe->Entry( -width => 20, )->pack(-side => 'left', -expand => 1); my $label_wert2 = $upperframe->Label( -text => 'HSBC :', )->pack(-side => 'left', -expand => 1); my $entry_wert2 = $upperframe->Entry( -width => 20, )->pack(-side => 'left', -expand => 1); my $label_wert3 = $upperframe->Label( -text => 'Banorte :', )->pack(-side => 'left', -expand => 1); my $entry_wert3 = $upperframe->Entry( -width => 20, )->pack(-side => 'left', -expand => 1); my $label_wert4 = $upperframe->Label( -text => 'Santander :', )->pack(-side => 'left', -expand => 1); my $entry_wert4 = $upperframe->Entry( -width => 20, )->pack(-side => 'left', -expand => 1); my $text1 = $topframe->Label(-text => "Archivos Por enviar")->pack(-si +de => 'left', -expand => 1); my $text2 = $topframe->Label(-text => "Archivos Por Enviados")->pack(- +side => 'left', -expand => 1); my $text3 = $topframe->Label(-text => "Archivos de Respuesta")->pack(- +side => 'left', -expand => 1); my $text4 = $bottomFrame->Label(-text => "Noticias")->pack(-side => 't +op', -expand => 1); my $box1 = Build_Listbox_One($mw); my $box2 = Build_Listbox_Two($mw); my $box3 = Build_Listbox_Three($mw); my ($enviar, $enviados, $respuestas) = Start_Directory_Monitors( $mw, +$box1, $box2, $box3 ); my $box4 = Build_Listbox_Four($mw); Tk::MainLoop(); sub Build_Listbox_One { my $box1 = $mainframe->Scrolled('Listbox', -scrollbars => 'ose') ->pack( -side => 'left', -expand=>1, -fill=>'both' ); return $box1; } sub Build_Listbox_Two { my $box2 = $mainframe->Scrolled('Listbox', -scrollbars => 'ose') ->pack( -side => 'left', -expand=>1, -fill=>'both' ); return $box2; } sub Build_Listbox_Three { my $box3 = $mainframe->Scrolled('Listbox', -scrollbars => 'ose') ->pack( -side => 'left', -expand=>1, -fill=>'both' ); return $box3; } sub Build_Listbox_Four { my $box4 = $bottomFrame->Scrolled('Listbox', -scrollbars => 'ose') ->pack( -side => 'left', -expand=>1, -fill=>'both' ); return $box4; } sub Start_Directory_Monitors { my $mw = shift; my $enviar_display = shift; my $enviados_display = shift; my $respuesta_display = shift; my $enviar = $mw->repeat(5000, [ \&list_dir_enviar, $enviar_di +splay ] ); my $env = $mw->repeat(5000, [ \&list_dir_enviados, $enviados_d +isplay ] ); my $res = $mw->repeat(5000, [ \&list_dir_respuesta, $respuesta_ +display ] ); return $enviar, $env, $res; } # monitor enviados sub list_dir_enviar { my $box = shift; $box->delete(0,'end'); # empty listbox my @items = glob "c:/AAAvalida/valida/enviar/*.*"; foreach (@items) { $box->insert('end', $_); } } sub list_dir_enviados { my $box1 = shift; $box1->delete(0,'end'); # empty listbox my @items = glob "c:/AAAvalida/valida/enviados/*.*"; foreach (@items) { $box1->insert('end', $_); } } sub list_dir_respuesta { my $box2 = shift; $box2->delete(0,'end'); # empty listbox my @items = glob "c:/AAAvalida/valida/respuestas/*.*"; foreach (@items) { $box2->insert('end', $_); } }
So basicly I need to add this to the program : I need to conect to the server and read a file using IO::Socket then put the data on the Entry for all 4 banks.
Can someone help me ?
thank you Perl Monks

Replies are listed 'Best First'.
Re: Get data from a file and put it in a Tk Entry
by apl (Monsignor) on May 08, 2008 at 19:00 UTC
    Every time you modify this program you ask the board to tell you how to make the modification. You will never learn Perl if you never write Perl.

    Take a look at the Network Programming tutorials. read the CPAN documentation, look at the qualified Q&A. Then, write a sample program. You'll make mistakes, you'll learn from your mistakes. If you can't resolve the problem you encounter, then you can post the code and ask for help in resolving it.

Re: Get data from a file and put it in a Tk Entry
by pc88mxer (Vicar) on May 08, 2008 at 19:02 UTC
    What kind of server are you connecting to, and is there a protocol involved (like HTTP) to get the data? Or does the data just start coming once you make a connection?

    Making socket connections is easy with the IO::Socket::INET module:

    use IO::Socket::INET; my $hostname = "localhost"; my $port = 19; $sock = IO::Socket::INET->new("$hostname:$port") or die "unable to connect: $!\n"; while (<$sock>) { ... # process line of data here } close($sock);
Re: Get data from a file and put it in a Tk Entry
by starbolin (Hermit) on May 08, 2008 at 19:07 UTC

    The question has several parts:

  • (1)How to connect to the server? Since you mentions IO::Socket I assume the server is listening on a socket. What protocol is it expecting to service?
  • (2)After you fetch the file you need to extract the data from the file. Usually some combination of split(), grep() and regular expressions are used.
  • (3)Then this data is stored in your $box display object.

    Which of 1, 2, or 3 do you need help with?

    The code posted is unhelpfully adjacent to the question you asked. could you perhaps edit your post to only include an outline of the code? Unless, of course, multiple Perl Monks picking apart your code is what you want?


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}
Re: Get data from a file and put it in a Tk Entry
by zentara (Cardinal) on May 08, 2008 at 19:35 UTC
    It really depends on how the server you connect to behaves. Is it a web site? If so, use LWP, if it's a pure socket server, you need to tell us what protocol it uses. See File Transfer via Sockets for a start.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum