Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Two Perl programming questions

Reply
Thread Tools

Two Perl programming questions

 
 
Tim
Guest
Posts: n/a
 
      11-15-2007
I'm just starting out with Perl and I have two questions:

1) I want to be able to read a file and populate an HTML drop-down
list using Perl. How can I do this? I've found a Perl example to
read the contents of a text file and populate an array like this:

$data_file="SomeInput.txt";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

The data is in the raw_data array. How can I return this value back
to HTML?

2) I want to be able to read a directory structure on the server and
use the directory names to populate an HTML dropdown list using Perl.
How can I do this?

Thank you in advance for any help. I greatly appreciate it.

Tim.

 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      11-15-2007
On Thu, 15 Nov 2007 05:22:50 -0800 (PST), Tim <(E-Mail Removed)>
wrote:

>1) I want to be able to read a file and populate an HTML drop-down
>list using Perl. How can I do this? I've found a Perl example to
>read the contents of a text file and populate an array like this:


This may help:

perldoc -q menu

>2) I want to be able to read a directory structure on the server and
>use the directory names to populate an HTML dropdown list using Perl.
>How can I do this?


You probably want File::Find or one of its relatives.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
 
Tim
Guest
Posts: n/a
 
      11-15-2007
File::Find works great to the get the values I need and I know how to
read the values from a flat file. Would my Perl just create the HTML
as output? I have two dropdowns. I wanted to make the second ones
contents dynamic based off what they select for the first. I wanted
to fill the first with values from the sever directories. Is this
possible with Perl? I also wanted to call a different Perl script on
a post which seems pretty easy. Something like this:

<form name="StartForm" action="SomePerlScript.pl" method="POST">

Let me know. Thank you for your help.

Tim.
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      11-15-2007
On Nov 15, 8:22 am, Tim <(E-Mail Removed)> wrote:
> I'm just starting out with Perl and I have two questions:


http://learn.perl.org
is a great place to find some tutorials to read.

> 1) I want to be able to read a file and populate an HTML drop-down
> list using Perl. How can I do this? I've found a Perl example to
> read the contents of a text file and populate an array like this:
>
> $data_file="SomeInput.txt";
> open(DAT, $data_file) || die("Could not open file!");
> @raw_data=<DAT>;
> close(DAT);


Very bad code. Do not use. Among the problems with it:
* using global filehandles
* using two-arg form of open
* not including open failure reason in error message
* reading entire file into memory at once.

> The data is in the raw_data array. How can I return this value
> back to HTML?


You need a very basic tutorial. I suggest you start with the
tutorials at the learn.perl.org site. When you've completed it, have
a read of:
perldoc -f open
perldoc -f readline
perldoc -f print
perldoc perlsyn
perldoc perldata
by typing each of those commands at your console.

Those will show you how to 1) Open the file, 2) Loop through the open
file, reading one line at a time, 3) print out a string containing the
values you've read from the file. Your string in this case will
obviously be the HTML text you'd like to print.

> 2) I want to be able to read a directory structure on the server
> and use the directory names to populate an HTML dropdown list
> using Perl. How can I do this?


perldoc -f opendir
perldoc -f readdir


Paul Lalli
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      11-15-2007
I get the basics of retriving text from a flat file and getting
directory names using Perl. I can debug through my Perl script and
see I'm geting those. How would Perl create the dynamic HTML that I
want? Does it just do it by Print statements? So my Perl script
would generate all the HTML? Would the Perl sciprt be called first
instead of my HTML? I have two dropdowns. The second dropdown I
wanted to have dynamic results based off what was entered for the
first. Is this possible?

Thanks for your help.

Tim.
 
Reply With Quote
 
Justin C
Guest
Posts: n/a
 
      11-15-2007
On 2007-11-15, Tim <(E-Mail Removed)> wrote:
> File::Find works great to the get the values I need and I know how to
> read the values from a flat file. Would my Perl just create the HTML
> as output? I have two dropdowns. I wanted to make the second ones
> contents dynamic based off what they select for the first.


If the two drop-downs are on the same web-page, then that's not HTML,
it's javascript. You can't 'run' perl on the client machine - you can
serve a page, but you can't make the page alter once it's been served.

Justin.

--
Justin C, by the sea.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      11-15-2007
Tim wrote:
> I get the basics of retriving text from a flat file and getting
> directory names using Perl. I can debug through my Perl script and
> see I'm geting those. How would Perl create the dynamic HTML that I
> want?


Perl is general purpose programming language. As such the programming
language does not "create <some text format>" automatically.

> Does it just do it by Print statements?


That would be one way, albeigh not the recommended way. I am guessing you
are talking about web programming using the Common Gateway Interface. In
that case most people would recommend to use the CGI module. Of course you
can use any of the HTML::Template modules to create HTML-text, too.

> So my Perl script would generate all the HTML? Would the Perl sciprt be
> called first
> instead of my HTML?


_Your_ HTML? You invented your own HyperText Markup Language?

This may seem like nitpicking, but actually terminology is very important.
In this case what do you mean by "call my HTML"? Who is the caller and what
exactly is the callee? HTML is text. It cannot be executed, therefore the
notion of calling HTML is non-sensical.

Are you talking about a browser requesting an HTTP response? In that case
the response would be an HTML text (among many other possible formats, of
course) and that text would have to come from somewhere, e.g. from a file or
being dynamically generated by e.g. a CGI script written in Perl.

> I have two dropdowns. The second dropdown I
> wanted to have dynamic results based off what was entered for the
> first. Is this possible?


Assuming you are talking about a standard CGI application (you didn't even
tell if that is the case) then this question has nothing to do with Perl but
everything with depending on how you implement it either dynamic HTML (*) or
programming in the CGI-environment (**).
*: loading a single page in the browser and then using JavaScript to
dynamically alter the second dropdown whenever the value of the first is
being changed
**: reloading a new page with a different second drop-down each time the
user selects a value from the first one.
But as I said before, neither has anything to do with Perl.

jue


 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      11-15-2007
This is a standard CGI application. Its a single HTML document with 2
dropdowns and a single Perl script. The Perl script is called to
perform an action on a Post from the HTML document:

<form name="SomeForm" action="PerlScript.pl" method="POST">

The dropdowns are currently hardcoded in the single HTML document
something like this:

<option name="Color" value="Green">Green</option>

I wanted to externalize one dropdowns values in a flat file. I also
wanted to fill in the other dropdown with directories on the server.
I wanted to see if I could do both of these tasks using Perl. In your
message you say its not recommended to do this through Perl print
statements. What would be your recommended way?

Tim.
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      11-15-2007
Its a standard CGI application. One HTML document, one perl script.
The perl script is called from a post in the HTML document:

<form name="Someform" action="SomeScript.pl" method="POST">

The HTML document has two dropdowns, both of which are populated from
within the HTML like this:

<option name="Color" value="Blue">Blue</option>

I want to externalize the dropdown lists to 1) a flat file, and 2)
directory paths on the server. I wanted to see if I could create this
dynamic HTML with Perl. You mention that doing this through print
statements isn't recommended. What would be the best way to do this
using HTML and Perl? Thanks.

Tim.
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      11-15-2007
I've found a great set of articles here:
http://www.wdvl.com/Authoring/Langua...nclusion4.html.
Its describes how you can write a Perl script which reads in the HTML
template file, generates the live content, and uses regular expression
substitutions to generate a "new" page.

I've tried a very small test and it appears to work great. Thanks for
everyones useful posts.

Tim.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP!! Two questions from <<the c programming language>> Tak C Programming 27 06-03-2007 02:37 PM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Two questions on making a Perl script work on PC Rong Hu Perl Misc 3 04-04-2006 11:15 PM
two questions about perl Charlie Perl Misc 1 05-12-2004 07:39 AM
Two questions about perl Weirong Zhu Perl Misc 3 08-14-2003 09:25 AM



Advertisments