Possible to run a subroutine on a user click? 
Author Message
 Possible to run a subroutine on a user click?
Hi.  I'm a Perl beginner and have written a working Perl script that I
am hoping to make a bit more efficient.  I've searched and searched
the web, but cannot find an answer to what I'd like to do.  A
bookstore is out since I live in Japan and can barely read Japanese.
Any help would be greatly appreciated.

My script opens a text file and loads the contents into an array

on a HTML page generated by the script.

The user then has the option of viewing the next set from the array,
going back to the previous set, or starting again from the beginning
by clicking on one of several buttons displayed at the bottom of the
script-generated HTML page.



button is clicked.  This seems to me like it might put a heavy load on
the server.


any way to run a subroutine within the script (when a user clicks a
button on the HTML output page) without having to reload the text file

HREF="data.cgi?set=$NextSet">NEXT</A> tag.

Is it possible?

Thanks in advance!

-Kevin



Fri, 24 Sep 2004 14:08:38 GMT  
 Possible to run a subroutine on a user click?

Quote:

> Hi.  I'm a Perl beginner and have written a working Perl script that I
> am hoping to make a bit more efficient.  I've searched and searched
> the web, but cannot find an answer to what I'd like to do.  A
> bookstore is out since I live in Japan and can barely read Japanese.
> Any help would be greatly appreciated.

> My script opens a text file and loads the contents into an array

> on a HTML page generated by the script.

> The user then has the option of viewing the next set from the array,
> going back to the previous set, or starting again from the beginning
> by clicking on one of several buttons displayed at the bottom of the
> script-generated HTML page.

> As it is now, every time the user decides to view a new set from


> button is clicked.  This seems to me like it might put a heavy load on
> the server.


> any way to run a subroutine within the script (when a user clicks a
> button on the HTML output page) without having to reload the text file

> HREF="data.cgi?set=$NextSet">NEXT</A> tag.

> Is it possible?

> Thanks in advance!

> -Kevin

Maybe (I don't know if this is possible) you could send all the data
to the page and Javascript would be called to display the data.
This would mean your CGI script would have to writing some Javascript
on the fly (once you figure out the real code, adding the data would
be easy).
When the user clicks on the Next button it would call a Javascript
function to display the next set of data (store the current line index
in a variable; increment it when the user clicks Next and display the
data from that line).

The part I would be concerned with is whether it is possible in
Javascript to change the contents of a page, removing some old data
first. It may not be possible (I think you could do it as a list box
or TEXTAREA field but both would look quite wierd).

Even if this was possible, the user may only want to see the first
page and all that extra data downloaded would have been downloaded
unnecessarily.

Quote:
> <A HREF="data.cgi?set=$NextSet">NEXT</A> tag.

Even though what you suggested yourself does require rerunning the
script on your server, it's not uncommon (think of Yahoo! when you
search and then can click to get the next 10 entries).


Sat, 25 Sep 2004 01:31:28 GMT  
 Possible to run a subroutine on a user click?
Thanks for the suggestion.  I will look into doing it with
Javascript... but my Javascript experience is very, very minimal and
so I am still hoping for a Perl solution. =)

Quote:
> > <A HREF="data.cgi?set=$NextSet">NEXT</A> tag.
> Even though what you suggested yourself does require rerunning the
> script on your server, it's not uncommon (think of Yahoo! when you
> search and then can click to get the next 10 entries).

I guess I am concerned about overworking the server (I'm on a shared
hosting plan).  I anticipate this script to be called MANY times from
many users simultaneously.

Each time the script executes, it has to open the text data file and
load from 40 to 90 short lines (often just one word or two per line)

simultaneously, I worry that something may "break".

I don't have enough knowledge about servers and file handling to know
if my script will overload the host's server... and that's why I'm
concerned. =S

-Kevin

Quote:

> Maybe (I don't know if this is possible) you could send all the data
> to the page and Javascript would be called to display the data.
> This would mean your CGI script would have to writing some Javascript
> on the fly (once you figure out the real code, adding the data would
> be easy).
> When the user clicks on the Next button it would call a Javascript
> function to display the next set of data (store the current line index
> in a variable; increment it when the user clicks Next and display the
> data from that line).

> The part I would be concerned with is whether it is possible in
> Javascript to change the contents of a page, removing some old data
> first. It may not be possible (I think you could do it as a list box
> or TEXTAREA field but both would look quite wierd).

> Even if this was possible, the user may only want to see the first
> page and all that extra data downloaded would have been downloaded
> unnecessarily.

> > <A HREF="data.cgi?set=$NextSet">NEXT</A> tag.
> Even though what you suggested yourself does require rerunning the
> script on your server, it's not uncommon (think of Yahoo! when you
> search and then can click to get the next 10 entries).


> > Hi.  I'm a Perl beginner and have written a working Perl script that I
> > am hoping to make a bit more efficient.  I've searched and searched
> > the web, but cannot find an answer to what I'd like to do.  A
> > bookstore is out since I live in Japan and can barely read Japanese.
> > Any help would be greatly appreciated.

> > My script opens a text file and loads the contents into an array

> > on a HTML page generated by the script.

> > The user then has the option of viewing the next set from the array,
> > going back to the previous set, or starting again from the beginning
> > by clicking on one of several buttons displayed at the bottom of the
> > script-generated HTML page.

> > As it is now, every time the user decides to view a new set from


> > button is clicked.  This seems to me like it might put a heavy load on
> > the server.


> > any way to run a subroutine within the script (when a user clicks a
> > button on the HTML output page) without having to reload the text file

> > HREF="data.cgi?set=$NextSet">NEXT</A> tag.

> > Is it possible?

> > Thanks in advance!

> > -Kevin



Sat, 25 Sep 2004 08:07:19 GMT  
 Possible to run a subroutine on a user click?

(snipped)

Quote:
> My script opens a text file and loads the contents into an array

> on a HTML page generated by the script.

If your data file is not too large, somewhere in the range of fifty
to one-hundred kilobytes, include your data within your script;
no need to open a data file. I frequently write scripts in excess
of one-hundred kilobytes and have found including data within a
script is faster and more efficient even with fairly sizable data,
including when data inclusion bumps script size to two-hundred
kilobytes or more.

However, I only do this for data which will be used for each
and every script run. Data which is accessed only periodically
is better stored elsewhere.

Quote:
> The user then has the option of viewing the next set from the array,
> going back to the previous set, or starting again from the beginning
> by clicking on one of several buttons displayed at the bottom of the
> script-generated HTML page.

> any way to run a subroutine within the script (when a user clicks a
> button on the HTML output page) without having to reload the text file

> HREF="data.cgi?set=$NextSet">NEXT</A> tag.

Only one sub-routine is needed if you plan carefully. Fairly simple
math is involved for scripts like this.

Beneath my signature you will discover an absolute bare bones script
which displays the most basic of logic for a script which you want.
I have added extra diagnostic prints to assist understanding what
happens. This methodology is easily adaptable for array stored data.
This method may also be adapted to tail __DATA__ with careful use
of the tell() and the seek() functions.

This is a deliberately verbose skeleton script to enhance ease
in comprehension.

A logical alternative to this type of method is to use radios buttons
keyed to various data files, each holding ten entries. Use of smaller
data files lends to better efficiency reducing server loading. Logic
similar to my experimental script may be adapted for radio buttons.

Name this script  "test.cgi" and point your browser to its URL. I use
an array populated with one through thirty, inclusive, to represent
three blocks of data, each containing ten entries.

Godzilla!
--

#!perl

print "Content-type: text/html\n\n
     <HTML>
     <BR><BR>";

print "Query String: $ENV{QUERY_STRING}<P>";

if (!($ENV{QUERY_STRING}))
 { $previous = 0; $next = 10; }

else
 {
  ($key, $value) = split (/=/, $ENV{QUERY_STRING});

  if ($key eq "Begin")
   { $previous = 0; $next = 10; }

  if ($key eq "Next")
   { $previous = $value; $next = $value + 10; }

  if ($key eq "Previous")
   {
    $previous = $value - 10; $next = $value;
    if ($previous == -10)
     { $previous = 0; $next = 10; }
   }

  print "     <P>Next = $next<P>Previous = $previous<P>";

  &Display;
 }

print "
     <P>
     <A HREF=\"test.cgi?Begin=$begin\">Begin Data Set</A>
     <P>
     <A HREF=\"test.cgi?Next=$next\">Next Data Set</A>
     <P>
     <A HREF=\"test.cgi?Previous=$previous\">Previous Data Set</A>
     <P>";

sub Display
 {
  print "Data Set:<BR>";
  for ($iterate = $next - 10; $iterate < $next; $iterate++)
   { print "$Data[$iterate]  "; }


   { $next -= 10; print "<P>End Of Data"; }
 }

print "     </HTML>";

exit;

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Sat, 25 Sep 2004 13:02:52 GMT  
 Possible to run a subroutine on a user click?

 (snipped)

Quote:
>      <A HREF=\"test.cgi?Begin=$begin\">Begin Data Set</A>

I forgot to add my variable $begin only appears in one place
as shown. This inclusion demonstrates how to set values, in
this case, my key Begin could be set to a value of zero
or a different value.

$begin = 0;
$begin = 5;
$begin = 10;

Each of those values will initiate very different responses.

Another perspective, your opening page could display your
first ten results right off. You may decide to add a scroll
back function such as, "Scroll back 40" which would make
good use of this $begin variable with a more appropriate
variable name. Simple matter of adding a little math to
keep all variable values relative, as needed.

Godzilla!

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Sat, 25 Sep 2004 13:29:09 GMT  
 Possible to run a subroutine on a user click?
Hi, Godzilla.

Thanks for the suggestion and going through the effort of writing a
sample script.


short... under 1KB... there are MANY different files available.  Which
file and which set of data within that file to use is selected by the
user and passed to the script through the query string.  To embed all
of the files within the script (using arrays within an array) would be
ungainly.

My concern is server overload if many different users are accessing
the script and working with the same data file (which is only read,
not written to).  As my script is now, each time the script
re-executes, the data file is re-opened and all of its contents stored

working with one set of data at a time is feasible for my
application), he or she goes to the next set, but to get there the
script is re-executed and thus the data file is re-opened and the

picked out.

It works... but I'm guessing that it could put a pretty big load on
the server during peak usage times.

One alternative I thought of and already coded this afternoon as a
test was to avoid data files altogether and instead store all of the
data in a MySQL database (one table within the database would contain
the information held in one data file from my previous way of doing
it).  The script would then connect to the database and retrieve only
the necessary row of data.

Now my question becomes....  Which method puts less of a load on the
server -- and therefore, which method could handle a greater number of
users simultaneously.  I have absolutely no idea.

Could somebody advise me on this please?

Thanks!

-Kevin

Quote:



>  (snipped)

> >      <A HREF=\"test.cgi?Begin=$begin\">Begin Data Set</A>

> I forgot to add my variable $begin only appears in one place
> as shown. This inclusion demonstrates how to set values, in
> this case, my key Begin could be set to a value of zero
> or a different value.

> $begin = 0;
> $begin = 5;
> $begin = 10;

> Each of those values will initiate very different responses.

> Another perspective, your opening page could display your
> first ten results right off. You may decide to add a scroll
> back function such as, "Scroll back 40" which would make
> good use of this $begin variable with a more appropriate
> variable name. Simple matter of adding a little math to
> keep all variable values relative, as needed.

> Godzilla!



Sat, 25 Sep 2004 18:05:41 GMT  
 Possible to run a subroutine on a user click?

  (snipped)

Quote:
> Thanks for the suggestion and going through the effort of writing a
> sample script.

> short... under 1KB... there are MANY different files available.  Which
> file and which set of data within that file to use is selected by the
> user and passed to the script through the query string.  To embed all
> of the files within the script (using arrays within an array) would be
> ungainly.

Your current statements directly contradict your previous statements.
Additionally, your approach is highly illogical. At one kilobyte each,
you may easily include one-hundred sets of data within your script,
which would be highly logical.

Changing parameters mid-stream is a clear indication of trolling.
My presumption is you are not a troll but rather experience typical
difficulties writing clear, concise and coherent articles along with
classic difficulties in avoiding inadvertently contradicting yourself.

Godzilla!

--

-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



Sat, 25 Sep 2004 22:08:35 GMT  
 Possible to run a subroutine on a user click?

Quote:


> > short... under 1KB... there are MANY different files available.  Which
> > file and which set of data within that file to use is selected by the
> > user and passed to the script through the query string.  To embed all
> > of the files within the script (using arrays within an array) would be
> > ungainly.

> Your current statements directly contradict your previous statements.
> Additionally, your approach is highly illogical. At one kilobyte each,
> you may easily include one-hundred sets of data within your script,
> which would be highly logical.

How have I contradicted myself?  You are assuming things that have not
been stated and jumping to conclusions based on your incorrect
assumptions.  THAT is illogical.

I have several hundred data files available.  A user chooses one file
through a radio button on a form, then calls the script.  Based on the

and use.

Even at under 1KB per file, having all of them written into the script
would cause the script to be in excess of 300KB and it will continue
to grow.  Much of that data won't be used on every call to the script.
 You yourself said that data which will not be used regularly should
be stored elsewhere, so my initial approach of storing the data in
separate data files does make sense.

In trying to reduce strain on the server, I thought of storing all the
data files in a MySQL database since ONLY the necessary set can be
picked out quite easily without loading unnecessary data into the
array.  I see it as a possible alternative which I am simply exploring
at this point.

Why is it so bad to think of alternative methods of accomplishing
something?

I am still searching for a method to call a subroutine on a user click
of a button generated by the script... without re-executing the script
from the very beginning.  It would save me from having to re-write the
entire script based around a MySQL database.  But I have not been able
to find a way to do that.  Your initial response, while at first
appreciated, did not solve the problem.  Now you seem upset that I did
not use your suggestion.

I will ignore your calling me a troll, and I truly hope that you are
not such a rude person all the time.

-Kevin



Sun, 26 Sep 2004 09:28:38 GMT  
 Possible to run a subroutine on a user click?


Quote:

> > > short... under 1KB... there are MANY different files available.

[snip]

Quote:
> In trying to reduce strain on the server,

[snip]

With such small data files, are you sure it's a problem?
Have you any way to quantify this strain?

--
David Thompson



Sun, 26 Sep 2004 14:53:47 GMT  
 Possible to run a subroutine on a user click?

Quote:



> > > > short... under 1KB... there are MANY different files available.
> [snip]

> > In trying to reduce strain on the server,
> [snip]

> With such small data files, are you sure it's a problem?
> Have you any way to quantify this strain?

I am not sure how to measure the script's load on the server (I
suppose I can contact my host and ask them to monitor it, though).

I don't believe it is a problem right now...  I am just trying to take
growth into consideration and make the script more efficient now, and
avoid a possible future problem.

If possible, I would still like to find a way to jump to a subroutine
from a user click without re-starting the script from the very
beginning.  Since all the data would be in the array already,
re-loading all of it each time a new portion is required for display
seems rather redundant... thus inefficient.

-Kevin



Sun, 26 Sep 2004 22:34:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. running a subroutine as a different user

2. accessing MainWindows widget after the user clicks the X (clo

3. accessing MainWindows widget after the user clicks the X (close) button

4. Running scripts by clicking on hypertext in Mosaic ??

5. Click .pl file from GUI to run?

6. run by click in linux

7. HList bug (single click simulates 2 clicks?)

8. single click and double click

9. possible user interfaces?

10. Possible to pass Associative Arrays to Subroutines??

11. Is dynamic loading of subroutines possible?

 

 
Powered by phpBB® Forum Software