Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

directory Content Drop-down

by lisaw (Beadle)
on Sep 16, 2002 at 22:42 UTC ( [id://198387]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, I am wondering if anyone know how to create a drop-down that will contain the contents of a certain directory without the need for a db? This is mainly for image selection. thanks!

Replies are listed 'Best First'.
Re: directory Content Drop-down
by moxliukas (Curate) on Sep 16, 2002 at 22:56 UTC

    Could you give us a bit more details? Is this for GUI application with Perl/Tk or HTML/CGI?

    I am not familiar with Perl/Tk, but in HTML you can use <SELECT> tag to do this kind of thing (there is also a CGI.pm function that builds this drop down list)

    You can read the directory using functions opendir, readdir and closedir to get the file names

Re: directory Content Drop-down
by valdez (Monsignor) on Sep 16, 2002 at 23:25 UTC

    Why do you need to read the content of a directory using a db? It is possible to read directories using command opendir:

    opendir DIR, $dir; while ($file = readdir DIR) { if ($file =~ /\.jpg$/) { push @images, { 'image' => $file }; } } closedir DIR;

    I didn't understand what kind of drop-down thingy you need :) drop-down menus? HTML? Tk? I know only some HTML and here is what I would do using HTML::Template:

    use HTML::Template; $t = HTML::Template->new(filename => 'dropdown.tmpl'); $t->param(IMAGES => \@images); print "Content-Type: text/html\n\n", $t->output;

    Now, you need a template like this:

    <form action=""> <select name="images"> <TMPL_LOOP IMAGES> <option><TMPL_VAR image></option> </TMPL_LOOP> </form>

    I think that these lines of code are enough to start your work; the Monastery is full of this examples. Please don't forget to begin your script with few magic words:

    #!/usr/bin/perl -w use strict; use vars qw($my $vars $are $here);

    Ciao, Valerio

    Disclaimer: please note that this code wasn't tested and is not approved by other monks.

Re: directory Content Drop-down
by spartacus9 (Beadle) on Sep 17, 2002 at 17:42 UTC
    Try using the opendir function and CGI.pm for the drop-down menu, as such:
    use CGI qw(:standard :html3); opendir THISDIR, "docs/images" or die "failed opening: $!"; @allfiles = grep !/^\.\.?$/, readdir THISDIR; closedir THISDIR; print popup_menu(-name=>'images', -values=>\@allfiles);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://198387]
Approved by valdez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-18 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found