Two Perl programming questions

Discussion in 'Perl Misc' started by Tim, Nov 15, 2007.

  1. Tim

    Tim Guest

    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.
     
    Tim, Nov 15, 2007
    #1
    1. Advertising

  2. 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^<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,
     
    Michele Dondi, Nov 15, 2007
    #2
    1. Advertising

  3. Tim

    Tim Guest

    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.
     
    Tim, Nov 15, 2007
    #3
  4. Tim

    Paul Lalli Guest

    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=<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
     
    Paul Lalli, Nov 15, 2007
    #4
  5. Tim

    Tim Guest

    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.
     
    Tim, Nov 15, 2007
    #5
  6. Tim

    Justin C Guest

    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.
     
    Justin C, Nov 15, 2007
    #6
  7. 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
     
    Jürgen Exner, Nov 15, 2007
    #7
  8. Tim

    Tim Guest

    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.
     
    Tim, Nov 15, 2007
    #8
  9. Tim

    Tim Guest

    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.
     
    Tim, Nov 15, 2007
    #9
  10. Tim

    Tim Guest

    I've found a great set of articles here:
    http://www.wdvl.com/Authoring/Languages/Perl/PerlfortheWeb/conclusion4.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.
     
    Tim, Nov 15, 2007
    #10
  11. 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^<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,
     
    Michele Dondi, Nov 15, 2007
    #11
  12. Tim

    J. Gleixner Guest

    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:
    >
    > <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.
     
    J. Gleixner, Nov 15, 2007
    #12
  13. 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/"
     
    Tad McClellan, Nov 16, 2007
    #13
  14. 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^<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,
     
    Michele Dondi, Nov 16, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GenxLogic
    Replies:
    3
    Views:
    1,379
    andrewmcdonagh
    Dec 6, 2006
  2. Tak
    Replies:
    27
    Views:
    918
    Army1987
    Jun 3, 2007
  3. Weirong Zhu

    Two questions about perl

    Weirong Zhu, Aug 13, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    89
    Simon Andrews
    Aug 14, 2003
  4. Charlie

    two questions about perl

    Charlie, May 12, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    102
    Walter Roberson
    May 12, 2004
  5. Rong Hu
    Replies:
    3
    Views:
    98
    Rong Hu
    Apr 5, 2006
Loading...

Share This Page