I'm not sure what the while loop is for if you only want to show one page. Here is slightly refactored code with it removed.
sub browse_ten {
# work out how many users
my @users = glob("users_dir/*");
my $no_of_users = scalar(@users);
# calculate how many pages to display
my $page_last = ceil($no_of_users / 10) ;
# get page to show from param or default 1
my $page_show = param('page') || 1;
my $page_prev = $page_show - 1;
my $page_next = $page_show + 1;
# decide whether to show links
print h2("Page $page_show of $page_last"); # optional
if ($page_show > 1) {
# Show a Previous link
print a({href => "?page=$page_prev"},"Previous");
}
if ($page_show < $page_last) {
# Show a Next link
print a({href => "?page=$page_next"},"Next");
}
# calc start and end user index number starting
# page 1 user start is 0, page 2 is 10
# page 1 end is start + 9 unless on last page
my $user_start = ($page_show - 1) * 10;
my $user_end = ($page_show < $page_last) ? $user_start + 9 : $no_of_
+users - 1 ;
# Show the range of entries.
for my $i ($user_start .. $user_end) {
my $user_to_show = $users[$i];
# user image
my $img_src = "$user_to_show/image.jpg";
unless (-r $img_src){
$img_src = 'blank_profile.jpg';
}
print img({src =>$img_src, width=>150, height=> 150 });
# user profile
my $profile_filename = "$user_to_show/profile";
if (open my $p, $profile_filename){
my $profile = join '<br/>', <$p>;
close $p;
print $profile;
} else {
print "Can not open $profile_filename : $!";
}
print br;
}
}
poj |