| Brian Volk 2006-10-30, 7:03 pm |
| Hello~
I'm trying to assign STDIN to a hash ref... I think? :-) I have a
working program (w/o references) but I think it will easily get out of
control if I don't learn to use references. Here is what I would like to
do: User selects vendor and then selects the type of image (regular or
thumbnail). I'm not sure how to access a hash ref when the user selects
option 1 and so on.
I think option 1 hash ref should look like this:
My %baywest_info = (
ftp_reg_dir => "bayw",
ftp_thumb_dir => "bayw/th",
local_reg_dir => 'c:/brian/jmt/baywest',
local_thumb_dir => 'c:/brian/jmt/baywest/thumbs',
)
My %baywest_ref =\%baywest_info;
Here is my program:
#!/usr/bin/perl
use strict;
use warnings;
use Net::FTP;
my $hostname = 'ftp.ftpsite.com';
my $username = 'username';
my $password = 'password';
&menu;
sub menu {
print "\n\nPlease choose a vendor from the
list:\n=================================
===\n";
print "\n";
print " 1. Bay West\n";
print " 2. Kimberly Clark\n";
print " 3. Vendor\n";
print " 4. Vendor\n";
print " 5. Vendor\n";
print " 6. Vendor\n";
print " 7. Vendor\n";
print " 8. Vendor\n";
print " 9. Vendor\n";
print "10. Vendor\n";
print "11. Quit\n\n\n";
print "Choose vendor option (1-11): ";
chomp (my $vendor = (<STDIN> ));
$vendor = 0 if ($vendor eq '');
if ($vendor =~ /q/
or
($vendor =~ /^\d+$/
and
$vendor == 11))
{
die("\nGoodbye!\n");
}
if ($vendor =~ /^\d+$/
and
$vendor == 0)
{
&menu;
}
# Start Vendor Option
if ($vendor =~ /^\d+$/
and
$vendor == 1) # *here is where I can't figure out how to access the
hash ref*
{
print "\nChoose image type:\n=================\n\n";
print "1. Regular images\n";
print "2. Thumbnails\n\n";
print "Choose image type (1-2): ";
chomp (my $type = (<STDIN> ));
$type = 0 if ($vendor eq '');
if ($type =~ /^\d+$/
and
$type == 0)
{
&menu; # if enter go back to main main menu
}
if ($type =~ /^\d+$/
and
$type == 1)
{
get_images("bayw", 'c:/brian/jmt/baywest');
###### w/ use of hash ref... get_images(${$baywest_info}{'ftp_reg_dir
'};
}
if ($type =~ /^\d+$/
and
$type == 2)
{
get_images("bayw", 'c:/brian/jmt/baywest/thumbs');
###### w/ use of hash ref...
get_images(${$baywest_info}{'ftp_local_d
ir'};
}
}
}
# and the get_images sub
sub get_images {
my $vendor = shift;
my $dir = shift;
chdir $dir or die "Cannot change dir to $dir: $!\n";
# Open the connection to the host
my $ftp = Net::FTP->new($hostname);
$ftp->login($username, $password); # log in
# Use binary or the images will be fuzzy
$ftp->binary;
# hardcode the directory and pass the $vendor from option
$ftp->cwd("/hpp0r1dc/$vendor") # change ftp_dir depending on option
or die "Cannot change working directory ", $ftp->message;
my @all_files = $ftp->ls();
# print "@all_files\n";
foreach my $file(@all_files) {
# Now get the files and leave
$ftp->get($file)
or die "Can't get file:\n", $ftp->message;
}
$ftp->quit;
} # end get_images sub
I'm reading Learning Perl Objects, References & Modules over and over
but I think I'm in over my head... :-) Any help would be greatly
appreciated!
Thank You!
Brian
|