Two Perl programming questions

T

Tim

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.
 
M

Michele Dondi

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
 
T

Tim

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.
 
P

Paul Lalli

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
 
T

Tim

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.
 
J

Justin C

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.
 
J

Jürgen Exner

Tim said:
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 said:
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
 
T

Tim

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.
 
T

Tim

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.
 
M

Michele Dondi

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

No, you have to build the html code yourself. Fortunately, you can
have Perl (possibly through some module) help you to do so.
contents dynamic based off what they select for the first. I wanted

Nothing to do with Perl, but you may want to concoct up some
JavaScript code to do so.


Michele
 
J

J. Gleixner

Tim said:
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.

You have a static file of HTML and you want to make
the contents more dynamic? As others have suggested,
use one of the many template modules: Template Toolkit,
HTML::Template, Template::Simple, etc.

http://search.cpan.org/search?query=template&mode=all

Read through the documentation for a few, and
pick one you like and install it.

They're each different, but once you install one, your HTML
will have some special 'tags', for the parts you
want to populate dynamically. You set the value(s) and
use the methods to dynamically populate those tags.
 
T

Tad McClellan

Tim said:
I'm just starting out with Perl and I have two questions:


Then there should be two separate posts, each with a Subject
header that indicates the subject of your article.

Subject: Two Perl programming questions

Does not tell us what your article is about.

Please see the Posting Guidelines that are posted here frequently.
 
M

Michele Dondi

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.

Strictly speaking, you can.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top