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

Dear Monks

I have TK window with two drop down fields. The Idea is that if users specify parameters for thoes drop down fields, or else the script will pick up the data specified in __DATA__. the script works ok the first time its run. Problem is, the seek (DATA ,0 ,0);seems to do a strange thing! I get a bit of the code dumped on the screen too when its called for the second time! instead of the seek()command rewinding to the top of the __DATA__! its rewinding half way up the actual code instead!!!

I have never came across this kind of behaviour where chunks of the actual code gets dumped on the screen!!

Can somewone please enlighten me why is this happening? And the way around it?

Thanks.
#!c:/perl/bin/perl.exe -w use strict; use vars qw /%data %tk/; use Win32; use Tk; use Tk::HistEntry; $data{analyst}=Win32::LoginName(); $data{sysadmin}='c$\program files\sysadmin\log'; $tk{main} = MainWindow->new; $tk{main}->geometry('400x130'); $tk{top_frame}=$tk{main}->Frame->pack(qw/-side top -fill x/); $tk{top_box} = $tk{top_frame}->Label(-text=>"Hosts : ", )->pack(qw/-side left -padx 10 -pady 10/); $tk{top_entry}=$tk{top_frame}->HistEntry (-width=>40, -background =>'white', -textvariable => \ $data{source}, )->pack(qw/-side right -fill both -padx 10 -pady 10 -expand 1/); $tk{middle_frame} = $tk{main}->Frame->pack(qw/-side top -fill x/); $tk{middle_box}=$tk{middle_frame}->Label(-text=>"Path : " )->pack(qw/-side left -padx 10 -pady 10/); $tk{middle_entry}=$tk{middle_frame}->HistEntry (-width=>40, -background =>'white', -textvariable => \ $data{xls}, )->pack(qw/-side right -fill both -padx 10 -pady 10 -expand 1/); $tk{bottom_frame}=$tk{main}->Frame->pack(qw/-side top -fill x/); $tk{go_buttn}=$tk{bottom_frame}->Button ( -text=>'GO!', -borderwidth=>5, -width=>60, -command=>sub{ &check_process, $tk{top_entry}->invoke; $tk{middle_entry}->invoke;}, )->pack(qw/-side bottom -padx 10 -pady 10/); sub check_process { my @DB_info; my @work_file; if ($data{source} ne "") { open (LST, "$data{source}") || &error_display($data{source}); chomp (@work_file=<LST>); close (LST); } else { chomp ( @work_file=<DATA>); seek (DATA ,0 ,0); } for (@work_file) { next if ($_ =~ /^\s*$/); my $data = {}; ($data->{bu}, $data->{group}, $data->{function}, $data->{source_id}, $data->{source_pc}, $data->{target_pc},) = split (/,/,$_); push (@DB_info, $data); print "$_\n"; undef $data; } } sub error_display { my (@input) = @_; Win32::MsgBox("$!\n\n@input\n",MB_ICONSTOP,"$0 : Operation Failed"); } MainLoop; exit 0; __DATA__ IED,AMM,,manjeet,cw036348,SN02RDS03a IED,AMM,,manjeet,cw036348,SN02RDS04a IED,Cash Sales,Specialist European Global Japanese Far East Global & W +hitephone,maxwill,cw036647,SN02BDW05a IED,Cash Sales,US Sales,breugelw,cw035660,SN02BDZ05a IED,Cash Sales,US Sales,breugelw,cw035660,SN02BDZ06a IED,Cash Sales,US Sales,breugelw,cw035660,SN02BDZ07a
Blackadder

Replies are listed 'Best First'.
Re: BoSelecta __DATA__ and rewind.
by dave_the_m (Monsignor) on Oct 04, 2004 at 13:13 UTC
    The DATA filehandle is simply a filehandle that is opened on the script being compiled, and which is left positioned at the line following the __DATA__ at the end of compilation. If you reset the handle to BOF, you'll just read the script again.

    Before reading from DATA, use tell() to get the current file postion, then seek to there when you want to rewind the file handle.

    Dave.

      This what I changed to,...still did,'t work because I not familier with Seek(), Tell(),..etc.

      Can you please put me out of my misry

      Thanks
      else { my $dpos = tell(DATA); print "\n$dpos"; chomp ( @work_file=<DATA>); seek (DATA ,2,"SEEK_SET"); }
      Blackadder
        blackadder,
        Perhaps this will help:
        #!/usr/bin/perl use strict; use warnings; my $pos = tell DATA; print while <DATA>; seek DATA, $pos, 0; print uc while <DATA>; __DATA__ one two three four

        Cheers - L~R

Re: BoSelecta __DATA__ and rewind.
by PodMaster (Abbot) on Oct 04, 2004 at 13:10 UTC
    instead of the seek()command rewinding to the top of the __DATA__! its rewinding half way up the actual code instead!!!
    why would it rewind to the top of __DATA__? seek handle,0,0 means seek to the beginning of the file, and __DATA__ is not the beginning of the file. If you read perldoc -f seek you'll see the name of a related function which you can use to learn where to seek to.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      perldoc -f seek Very Usefull.

      I will use it from now on

      Thanks

      Blackadder