strfry() has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i realize that this doesn't have to do with perl -exactly-, but it does in the long run, and quite frankly i didn't know where else to ask.
i have two perl written cgi scripts, on seperate servers. one is a shopping cart, the other is a logging mechanism; and it seems the shopping cart's values aren't very compatible with mine.
it will let me use an 'embedded' variable for the total price (FINAL_SUBTOTAL), but places spaces and other garbage within the result.
eg. $ 4.95, as opposed to 4.95 which is what i need.
now, what i did was make a kludgy workaround, because i'm on a limited timeframe (working and all (: ), so i used some javascript on the shopping carts thankyou page to implement JS's weird but mildly effective regex functions, and got this:
<script language="javascript"> var re = /([^\d\.]+)/g; var str = "FINAL_SUBTOTAL"; var newstr = str.replace(re, ""); </script>

now, the main problem is getting my newly regexed variable to show up in a URL (eg.  <img src="http://somedomain/cgi-bin/some.cgi?newstr">)
or something like that. and just to be clear, for reference sake alone, i'll post a perlish version of this stupid JS later on. (:
please dont flame me, i would've patched the other perl script had i been given the time, but a deadline is a deadline.

strfry

Replies are listed 'Best First'.
Re: making perl scripts play nicely together
by tadman (Prior) on Jul 03, 2001 at 17:11 UTC
    I have a feeling that if it weren't for shopping cart scripts, the Internet bubble woldn't have occured, Venture Capital firms would have nowhere to put their money, and 50% of the high tech workforce would be working at the local Wal Mart. Maybe that's just me being cynical.

    Anyway, it's not entirely clear what you are working with, or what you are trying to achieve. If you want to put something into a URL, you can change the HREF of a given object dynamically, or even have an HREF that is of the "javascript:" variety. Either way, you are doing something wierd if the client has to figure out the URLs.

    It might be better to do the calculations in parallel on the server instead of trusting the client to send data back to you. Imagine someone figured out what you were doing and made a page something like:
    <INPUT TYPE=hidden NAME=item_1_id VALUE="XB17 Hovercraft"> <INPUT TYPE=hidden NAME=item_1_price VALUE="$250,000"> <INPUT TYPE=hidden NAME=total VALUE="$5.99">
    You can imagine how bad that would look on the quarterly report.

    If you want to do calculations on the client, go right ahead, but don't send these values back to the server and use them without checking. Remember that data that comes in from the client is guilty until proven innocent. You never know what those crazy users are going to do.
      haha no, i was very unclear apparently. the calculations aren't done client-side; the formatting (regex) is though; the URL is static, i want it to to say  http://www.yahoo.com/cgi-bin/perl.cgi?
      i just want the price to be reformatted from $1.99 to 1.99, for example. or from jkadhsjaa$.99 to .99, if you see what i'm doing. (stripping all but numbers and dots from price variable) the transaction is already processed, all that the user could fool with is a secondary logging system (i mean, if they feel that they have the spare time to fool a survey system, more power to them.) i just want to get the variable that i've regexed to appear within a tag like:  <IMG SRC="http://www.google.com/perl.cgi?amount=newstr">
      just that, and nothing more.
      to be more concise, this is exactly what i want it to do:
      <SCRIPT LANGUAGE="JavaScript"> var re = /([^\d\.]+)/g; var str = "FINAL_SUBTOTAL"; var newstr = str.replace(re, ""); <IMG SRC="https://www.google.com/perl.cgi?amount=newstr"> </script>

      see what i'm trying to do? just making the "newstr" value be automated; i just can't figure out how javascript returns (actually, writes) a variable to a page.
      in perl, i'd be doing this:
      my $str = $other_scripts_input; my $newstr; $newstr =~ s/([^\d\.]+)/g; print "<img src=\"http://www.someurl.com/perl.cgi?amount=$newstr\">";

      but as i've said, i've not been given the time (at the moment) it takes to modify someone else's brainchild ):
      i do appreciate all help given.

      (you see, this is really a temporary workaround until i'm granted the time to modify their perl script and do it the right way)
      strfry
Re: making perl scripts play nicely together
by dash2 (Hermit) on Jul 03, 2001 at 17:22 UTC
    hmm. Clearly does have 0 to do with perl, but i know the feeling of desperation.

    The simplest answer is just

    document.write("<img src='http://somedomain/cgi-bin/some.cgi?'" + news +tr);

    I would think. But you can also change an image's src attribute dynamically. Something like

    document.Images('name_of_your_image').src = newstr;

    dave hj~

      that was exactly what i needed dash2; the first answer i mean (:
      as you can likely tell, i've never touched javascript before... and it was desperation that moved me to ask about JS on here. (i'd just figured, well, you know. if perl monks/saints/wizards didn't know, no one would.)
      i was just told to yesterday to kludge (er, innovate... (: ) a workaround for a client. but if it works, it works, at least until i can get my grubby little hands on their perl shopping cart thingie, and make it good.
      thanks everyone, especially for the speedy replies

      strfry
      CGI.pm has a function for creating Javascript on the client. So I would not agree that the question of what Javascript code to use is unrelated to Perl—with CGI.pm Javascript code is expressed in Perl.

      I have never used CGI.pm's Javascript writing capability, however.

Re: making perl scripts play nicely together
by tachyon (Chancellor) on Jul 03, 2001 at 17:50 UTC

    To be frank patch the perl add:

    $final = s/[^\d.]//g
    and you are done. This is fast and will work, provided the existing code does. If you have a deadline this is the easy way. The short way. It is also the perl way and this is after all a perl site.

    Your question is a completely javascript one. In a nutshell many of the elements rendered by the browser are considered objects. You can access the properties of these objects and both read and write to them in javascript just by naming them and assigning/reading a value. For example this javascript will redirect you to a new page:

    window.location.href="foo.htm"

    I hope you make your deadline but think patching the perl will be quicker and easier.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n\w+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print