Two Perl programming questions
Two Perl programming questions
am 15.11.2007 14:22:50 von 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=;
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.
Re: Two Perl programming questions
am 15.11.2007 16:02:10 von Michele Dondi
On Thu, 15 Nov 2007 05:22:50 -0800 (PST), Tim
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^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Two Perl programming questions
am 15.11.2007 17:00:39 von 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:
Re: Two Perl programming questions
am 15.11.2007 17:07:34 von Paul Lalli
On Nov 15, 8:22 am, Tim 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=;
> 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
Re: Two Perl programming questions
am 15.11.2007 17:46:03 von 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.
Re: Two Perl programming questions
am 15.11.2007 17:58:40 von Justin C
On 2007-11-15, Tim 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.
Re: Two Perl programming questions
am 15.11.2007 18:09:43 von jurgenex
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 " 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
Re: Two Perl programming questions
am 15.11.2007 20:02:25 von 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:
Re: Two Perl programming questions
am 15.11.2007 20:09:02 von Tim
Its a standard CGI application. One HTML document, one perl script.
The perl script is called from a post in the HTML document:
Re: Two Perl programming questions
am 15.11.2007 21:06:42 von Jim Gibson
In article
<4aacfcff-09c4-4d12-b874-9692762bef2c@p69g2000hsa.googlegroups.com>,
Tim wrote:
> Its a standard CGI application. One HTML document, one perl script.
> The perl script is called from a post in the HTML document:
>
>
Re: Two Perl programming questions
am 15.11.2007 21:08:02 von Tim
I've found a great set of articles here:
http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/c onclusion4.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.
Re: Two Perl programming questions
am 15.11.2007 21:09:31 von Michele Dondi
On Thu, 15 Nov 2007 08:00:39 -0800 (PST), Tim
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
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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Re: Two Perl programming questions
am 15.11.2007 21:22:51 von glex_no-spam
Tim wrote:
> Its a standard CGI application. One HTML document, one perl script.
> The perl script is called from a post in the HTML document:
>
>
Re: Two Perl programming questions
am 16.11.2007 01:42:44 von Tad McClellan
Tim wrote:
> 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.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: Two Perl programming questions
am 16.11.2007 11:42:27 von Michele Dondi
On Thu, 15 Nov 2007 16:58:40 -0000, Justin C
wrote:
>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,