Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PerlTK getting selected item from List box

Status
Not open for further replies.

inforeqd

Technical User
Jan 8, 2001
95
0
0
US
Here are my subroutines for doing an ftp using a perlTK script. I've left out all the items above because it all works except for my subroutine in actually selecting an item and then pushing my "get file" button.

Subroutine to get files and list them to listbox. This sub works fine
============================================================
sub ftp_site_dell
{
my $ftp = Net::FTP->new ($ftp_site_dell)
or die "Could not connect";

$ftp->login($ftp_user, $ftp_password)
or die "Could not login";
$ftp->binary(); # Set binary mode
$ftp->cwd($ftp_dir)
or die "Could not change directories";

# List the files in the directory
my @remote_files = $ftp->ls($list_files);
foreach my $file (@remote_files)

# Put the files from ftp ls into the listbox
{
$files_lb->insert('end', $file);

}
========================================================
This is where I have problems. I just cannot figure out how to select the item i want and then when I push my button "get" that item via ftp get.

sub get_files
{
my @selected_files = $files_lb->curseselection();
foreach (@selected_files) {
my $ftp->get(@selected_files);
}
========================================================

I don't think I copied the code over with all the right curly brackets so just disregard that. I know I'm not using the right perl here but I just can't figure it out. Do I need separate subroutines??

Any help again is always appreciated. Also, just note this is really ugly code and I apologize to the real perl programmers out there.

TIA!


 
You really need to use some print statement to help you see what is happening in cases like this.

Lets go through it, first plain old Perl.
foreach (@selected_files)
{
my $ftp->get(@selected_files);
}

foreach (@selected_files)
This statement goes through each item in the array one at a time, and puts them by default in $_

Next statement:
my $ftp->get(@selected_files);

Where is the my $ftp initialized? The 'my' says that this is a new variable scoped in this context from { to }. So you are trying to use an undefined variable $ftp with the ->. As soon as you tried this it should have given an error about using ->get on an undefined variable.

You created $ftp in another function. You either need to make this a more global variable or better yet pass it to the function (since these are callbacks you are probably stuck with global).

Next you have $ftp->get(@selected_files); You have the whole array in there again not just the one item in the array. You want:
$ftp->get($_);

Will that work? Nope. Because if you look up the documentation for the TK list box what curseselection() returns is the index number not the string (in your case file name).

So if you had:

foreach (@selected_files)
{
print "$_\n";
$ftp->get($_);
}

You would have seen:
0
1
2

This link shows how they put something like this together:

But let me throw together a quick hack:
# Makes sure available to to both functions. These must be executed before the functions below.
my @listBoxFiles;
my $ffp;

sub ftp_site_dell
{
$ftp = Net::FTP->new ($ftp_site_dell)
or die "Could not connect";
$ftp->login($ftp_user, $ftp_password)
or die "Could not login";
$ftp->binary(); # Set binary mode
$ftp->cwd($ftp_dir)
or die "Could not change directories";

# List the files in the directory
# Put the files from ftp ls into the listbox
@listBoxFiles = $ftp->ls($list_files);
foreach my $file (@listBoxFiles)
{
$files_lb->insert('end', $file);
}
}

sub get_files
{
my @selected_files = $files_lb->curseselection();
foreach my $fileIndex (@selected_files)
{
print "Sending $listBoxFiles[$fileIndex], index $fileIndex\n";
my $ftp->get($listBoxFiles[$fileIndex]);
}
}
 
Opps copy and paste error:
my $ftp->get($listBoxFiles[$fileIndex]);

should be
$ftp->get($listBoxFiles[$fileIndex]);
 
A couple more notes.
If all you want is the string from the one selected you can use "get()" (instead of maintaining a separate @listBoxFiles):

my $curValue = $files_lb->get($fileIndex);

If you need to maintain secondary information like you display one thing in the list box, but you need to return yourself a different value you can maintain that secondary value in a list in the same way the @listBoxFiles was done.

You can also not use a global variable it you setup the callback function to take these a parameter, like:

... -command => sub {get_files(\@listBoxFiles)});
 
WOW!! Thanks so much for the information and help! That was very informative and I will go through each line and revisit the code to see how to better put it together. You provided a lot more than what I've been able to google for in reference to this.

In reference to your question about starting the ftp. yes that was in another function and yes I was getting that undefined variable. I was looking and looking and didn't understand why until you pointed it out. Hopefully I can use what you passed and tailor my script out better to do what I'm looking for it to do. Thanks so much for the input!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top