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

I'm trying to list the contents of a directory, and associate a radio button for each file in the directory on the fly. Not sure how to do this... any idea's? wolfy.
use Cwd; my $dir = getcwd; opendir (DIR, $dir) or die $!; my @dir = readdir DIR; rewinddir(DIR); my @perlFiles = grep /\.pl/, readdir DIR; foreach my $item (@perlFiles) { print "$item\n"; } closedir DIR;

Replies are listed 'Best First'.
Re: associate radio buttons
by dominix (Deacon) on Jan 08, 2004 at 22:52 UTC
    don't know if it's what you expect but this could help
    be carefull : untested
    use Cwd; my $dir = getcwd; opendir (DIR, $dir) or die $!; my @dir = readdir DIR; rewinddir(DIR); my @perlFiles = grep /\.pl/, readdir DIR; print '<HTML><BODY><FORM ACTION="/cgi-bin/program.pl" METHOD="POST">'; foreach my $item (@perlFiles) { print $item . ':<INPUT TYPE="radio" NAME="filechosen" VALUE="' . $item + . '" >'; } print '<BR></FORM></BODY></HTML>';
    --
    dominix
Re: associate radio buttons
by chromatic (Archbishop) on Jan 08, 2004 at 21:24 UTC

    What kinds of radio buttons do you want and how will you associate them? What's the logical association between a file name and the button? What problem are you trying to solve?

Re: associate radio buttons
by Zed_Lopez (Chaplain) on Jan 08, 2004 at 22:45 UTC

    For the first part, glob is simpler:

    my @perlfiles = glob "*.pl";

    If you're talking about HTML output, this will work:

    use CGI; # ... don't forget to output header, startform print CGI::radio_group(-name => 'perlfiles', -values => \@perlfiles); # ... submit button, endform

    But as thousands of words worth of perlmonks entries will tell you, CGI.pm is a lousy solution for creating HTML for any but the most trivial applications. A template system is much better. See HTML::Template or do a SuperSearch for lots more options.

Re: associate radio buttons
by TomDLux (Vicar) on Jan 08, 2004 at 22:50 UTC

    If you want to allow users to delete files, then you do indeed need radio buttons. On the other hand, if the point is to move, delete or view files, why not use checkbuttons and allow them to process multiple files in one ( slow ) server access.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA