[16311] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3723 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 18 10:38:18 2000

Date: Tue, 18 Jul 2000 07:37:56 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <963931076-v9-i3723@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 18 Jul 2000     Volume: 9 Number: 3723

Today's topics:
        Perl can't add ! (Stuart Horner)
    Re: Perl can't add ! (Hermann Fass)
    Re: Perl can't add ! (Kevin M. Sproule)
    Re: Perl can't add ! (Logan Shaw)
    Re: Perl can't add ! ()
    Re: Perl can't add ! (Abigail)
    Re: Perl can't add ! (Abigail)
    Re: Perl can't add ! (Logan Shaw)
    Re: Perl can't add ! (Abigail)
    Re: Perl can't add ! (Ilya Zakharevich)
    Re: Perl CGI for Remote Control? (Marcelo Narvaja)
        perl cgi that makes web pages from text file (Ðyl@ñ)
    Re: perl cgi that makes web pages from text file (Bob Walton)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 15 Jul 2000 09:10:01 GMT
From: stuart@unidev.com.bbs@openbazaar.net (Stuart Horner)
Subject: Perl can't add !
Message-Id: <3bPRdP$UWG@openbazaar.net>

I'm having a Perl adding error!

Here is the code:

foreach $key (@Sorted)
{
  print "\"$InitialAmount\" + \"$SearchResults{$key}{'Amount'}\" = ";
  $InitialAmount = $SearchResults{$key}{'Amount'} + $InitialAmount;
  print "\"$InitialAmount\"\n";
}

Here is a snippit of the output:

"823.35" + "24.95" = "848.3"
"848.3" + "24.95" = "873.25"
"873.25" + "24.95" = "898.200000000001"
"898.200000000001" + "24.95" = "923.150000000001"
"923.150000000001" + "24.95" = "948.100000000001"
"948.100000000001" + "24.95" = "973.050000000001"
"973.050000000001" + "24.95" = "998.000000000001"
"998.000000000001" + "24.95" = "1022.95"
"1022.95" + "24.95" = "1047.9"
"1047.9" + "24.95" = "1072.85"
"1072.85" + "24.95" = "1097.8"
"1097.8" + "24.95" = "1122.75"
"1122.75" + "24.95" = "1147.7"
<snip>
"2999.15" + "24.95" = "3024.1"
"3024.1" + "24.95" = "3049.05"
"3049.05" + "24.95" = "3074"
"3074" + "24.95" = "3098.95"
"3098.95" + "24.95" = "3123.9"
"3123.9" + "24.95" = "3148.84999999999"
"3148.84999999999" + "64.95" = "3213.79999999999"
"3213.79999999999" + "24.95" = "3238.74999999999"
"3238.74999999999" + "24.95" = "3263.69999999999"
"3263.69999999999" + "24.95" = "3288.64999999999"
"3288.64999999999" + "2.95" = "3291.59999999999"
"3291.59999999999" + "24.95" = "3316.54999999999"
"3316.54999999999" + "24.95" = "3341.49999999999"
"3341.49999999999" + "2.95" = "3344.44999999999"
"3344.44999999999" + "24.95" = "3369.39999999999"
"3369.39999999999" + "2.95" = "3372.34999999999"

What is going on with this?? I'm just adding two numbers together, from a
hash of a hash!

I've had this problem on two different machines running two different
operating systems, on two different versions of Perl.

Sun OS 7 Perl 5.003 and RedHat 6.1 (I don't know the kernal version) Perl
5.6

All the printing is only to show the results at each step for debugging.
(i.e. finding out where the heck that .000000000001 is coming from).

Any responses appreciated!!

Regards,

Stuart Horner
stuart@NOSPAMPLEASE.core-comm.com

Please remove the nospamplease. to e-mail.


------------------------------

Date: 16 Jul 2000 13:20:01 GMT
From: hermann@holzerath.de.bbs@openbazaar.net (Hermann Fass)
Subject: Re: Perl can't add !
Message-Id: <3bQNe1$USV@openbazaar.net>

With the use of printf rather than just print
your output could be formatted as well.

Herm.

"Kevin M. Sproule" wrote:
>
> "Stuart Horner" <stuart@unidev.com> wrote in message
> news:NUVb5.16320$EQ3.572715@news-east.usenetserver.com...
> > I'm having a Perl adding error!
> >
> > Here is the code:
> >
> > foreach $key (@Sorted)
> > {
> >   print "\"$InitialAmount\" + \"$SearchResults{$key}{'Amount'}\" = ";
> >   $InitialAmount = $SearchResults{$key}{'Amount'} + $InitialAmount;
> >   print "\"$InitialAmount\"\n";
> > }
> >
> > Here is a snippit of the output:
> >
> > "823.35" + "24.95" = "848.3"
> > "848.3" + "24.95" = "873.25"
> > "873.25" + "24.95" = "898.200000000001"
> > "898.200000000001" + "24.95" = "923.150000000001"
> > "923.150000000001" + "24.95" = "948.100000000001"
> > SNIP
> > What is going on with this?? I'm just adding two numbers together, from a
> > hash of a hash!
> >
> > I've had this problem on two different machines running two different
> > operating systems, on two different versions of Perl.
> >
> > Sun OS 7 Perl 5.003 and RedHat 6.1 (I don't know the kernal version) Perl
> > 5.6
> >
> > All the printing is only to show the results at each step for debugging.
> > (i.e. finding out where the heck that .000000000001 is coming from).
> >
> > Any responses appreciated!!
> >
> > Regards,
> >
> > Stuart Horner
> > stuart@NOSPAMPLEASE.core-comm.com
> >
> > Please remove the nospamplease. to e-mail.
> >
>
> Stuart,
>
> What you are now learning about is called Floating Point Approximation.
> This occurs in all computer languages.  The following example is in C:
>
> // fptest.c  -  Kevin Sproule  7/15/2000
>
> #include <stdio.h>
> #include <dos.h>
>
> int main()
> {
>    double init=823.35,
>           inc=24.95;
>    int i;
>
>    for (i=0; i<3; i++)
>    {
>     printf("%.13f + %.13f = ",init,inc);
>     init += inc;
>     printf("%.13f\n",init);
>    }
>    return 0;
> }
>
> Results:
>
> 823.3500000000000 + 24.9500000000000 = 848.3000000000001
> 848.3000000000001 + 24.9500000000000 = 873.2500000000001
> 873.2500000000001 + 24.9500000000000 = 898.2000000000002
>
> These results are similar to what you encountered.  The errors creep in
> because computers are only capable of approximating the floating point
> values.  It is necessary to use whole numbers or to round your results to
> keep things accurate.  Some folks like to do all integer math while other
> prefer to just round the results of calculations.
>
> Simple Perl rounding:
>
> $result = int(($result + .005) * 100) / 100;   # round to 2 places
>
> Yours truly,
>
> Kevin Sproule


------------------------------

Date: 16 Jul 2000 03:10:01 GMT
From: kmsproule@worldnet.att.net.bbs@openbazaar.net (Kevin M. Sproule)
Subject: Re: Perl can't add !
Message-Id: <3bQ7jP$Ujp@openbazaar.net>

"Stuart Horner" <stuart@unidev.com> wrote in message
news:NUVb5.16320$EQ3.572715@news-east.usenetserver.com...
> I'm having a Perl adding error!
>
> Here is the code:
>
> foreach $key (@Sorted)
> {
>   print "\"$InitialAmount\" + \"$SearchResults{$key}{'Amount'}\" = ";
>   $InitialAmount = $SearchResults{$key}{'Amount'} + $InitialAmount;
>   print "\"$InitialAmount\"\n";
> }
>
> Here is a snippit of the output:
>
> "823.35" + "24.95" = "848.3"
> "848.3" + "24.95" = "873.25"
> "873.25" + "24.95" = "898.200000000001"
> "898.200000000001" + "24.95" = "923.150000000001"
> "923.150000000001" + "24.95" = "948.100000000001"
> SNIP
> What is going on with this?? I'm just adding two numbers together, from a
> hash of a hash!
>
> I've had this problem on two different machines running two different
> operating systems, on two different versions of Perl.
>
> Sun OS 7 Perl 5.003 and RedHat 6.1 (I don't know the kernal version) Perl
> 5.6
>
> All the printing is only to show the results at each step for debugging.
> (i.e. finding out where the heck that .000000000001 is coming from).
>
> Any responses appreciated!!
>
> Regards,
>
> Stuart Horner
> stuart@NOSPAMPLEASE.core-comm.com
>
> Please remove the nospamplease. to e-mail.
>

Stuart,

What you are now learning about is called Floating Point Approximation.
This occurs in all computer languages.  The following example is in C:

// fptest.c  -  Kevin Sproule  7/15/2000

#include <stdio.h>
#include <dos.h>

int main()
{
   double init=823.35,
          inc=24.95;
   int i;

   for (i=0; i<3; i++)
   {
    printf("%.13f + %.13f = ",init,inc);
    init += inc;
    printf("%.13f\n",init);
   }
   return 0;
}

Results:

823.3500000000000 + 24.9500000000000 = 848.3000000000001
848.3000000000001 + 24.9500000000000 = 873.2500000000001
873.2500000000001 + 24.9500000000000 = 898.2000000000002

These results are similar to what you encountered.  The errors creep in
because computers are only capable of approximating the floating point
values.  It is necessary to use whole numbers or to round your results to
keep things accurate.  Some folks like to do all integer math while other
prefer to just round the results of calculations.

Simple Perl rounding:

$result = int(($result + .005) * 100) / 100;   # round to 2 places

Yours truly,

Kevin Sproule


------------------------------

Date: 16 Jul 2000 22:10:01 GMT
From: logan@cs.utexas.edu.bbs@openbazaar.net (Logan Shaw)
Subject: Re: Perl can't add !
Message-Id: <3bQbUP$WTv@openbazaar.net>

In article <2O9c5.10477$tI4.832438@bgtnsc05-news.ops.worldnet.att.net>,
Kevin M. Sproule <kmsproule@worldnet.att.net> wrote:
>These results are similar to what you encountered.  The errors creep in
>because computers are only capable of approximating the floating point
>values.  It is necessary to use whole numbers or to round your results to
>keep things accurate.

Actually, it isn't the compiler.  The folks who figured out how
computers were going to work were faced with a choice.  Either (1) make
computers do all arithmetic correctly, or (2) make computers which can
do built-in arithmetic operations without literally taking forever.
They chose #1, which I think was the correct choice.

Let's imagine you want to work with the number 1/5.  In decimal, this
happens to be easy.  It's equal to 0.2.  In binary, it's a bit tougher
because it's actually equal to

  0.00110011001100110011001100110011001100110011001100110011001100110011
  0011001100110011001100110011001100110011001100110011001100110011001100
  1100110011001100110011001100110011001100110011001100110011001100110011
  0011001100110011001100110011001100110011001100110011001100110011001100
  1100110011001100110011001100110011001100110011001100110011001100110011

Except that I lied -- it's not equal to that because I haven't yet
typed "0011" enough times.  I have to keep typing it forever if I want
an exact representation.

Anyway, the computer gets tired too and quits after some number of
digits.  Let's assume it quits after 25 digits after the "decimal"
(actually binary) point.  In that case, when you write 0.2, it actually
stores the number 0.199999988079071044921875.  That's the best it can
do because the next closest number that can be represented with 25
fractional binary digits is 0.20000001788139343261718750.

So when you go adding 0.2 and 0.2 together, you get
0.0110011001100110011001100 in binary or 0.39999997615814208984375 in
decimal, which isn't exactly equal to 0.4.

But oh well, you can't write 0.4 in binary anyway so it was impossible
to get an answer back that was exactly 0.4.

So why do the numbers seem right some of the time?  Well, some of the
time they are right.  1/4 and 3/16 can both be written exactly in a
finite number of binary digits.  So if you add them, you'll get exactly
the right answer.  Other times they seem right because you convert a
number like 0.2 into an approximation (in binary) and then when you
convert it back (to decimal), 0.2 is the closest decimal approximation
for your binary approximation.  It just depends on the numbers you're
working with whether you luck out.

Another way of solving this problem is to have the computer work the
numbers as fractions.  This completely eliminates the problem with
repeating decimals, but it has some of its own problems.  It's slower,
you've got to have a library that supports it, and eventually if you
add enough fractions with different denominators, you'll get a least
common denominator that's bigger than the largest integer you can
store.  (Try doing 1/2 + 1/3 + 1/5 + 1/7 + 1/11 + 1/13 + 1/17 + . . .;
there's no limit to how big the denominator can get.)

Binary isn't the problem either -- some numbers like (1/7) can't be
written in decimal without repeating digits.  And even if you solved
the problem for fractions, somebody would complain that the square of
the square root of two doesn't come out to exactly two.

Moral: some arithmetic is always an approximation, and computers are no
exception.

  - Logan


------------------------------

Date: 17 Jul 2000 03:30:04 GMT
From: jcano@mmcnet.com.bbs@openbazaar.net ()
Subject: Re: Perl can't add !
Message-Id: <3bQjkS$XLd@openbazaar.net>

logan@cs.utexas.edu (Logan Shaw) writes:

> In article <2O9c5.10477$tI4.832438@bgtnsc05-news.ops.worldnet.att.net>,
> Kevin M. Sproule <kmsproule@worldnet.att.net> wrote:
> >These results are similar to what you encountered.  The errors creep in
> >because computers are only capable of approximating the floating point
> >values.  It is necessary to use whole numbers or to round your results to
> >keep things accurate.
>
> Actually, it isn't the compiler.  The folks who figured out how
> computers were going to work were faced with a choice.  Either (1) make
> computers do all arithmetic correctly, or (2) make computers which can
> do built-in arithmetic operations without literally taking forever.
> They chose #1, which I think was the correct choice.
>
> Let's imagine you want to work with the number 1/5.  In decimal, this
> happens to be easy.  It's equal to 0.2.  In binary, it's a bit tougher
> because it's actually equal to
>
>   0.00110011001100110011001100110011001100110011001100110011001100110011
>   0011001100110011001100110011001100110011001100110011001100110011001100
>   1100110011001100110011001100110011001100110011001100110011001100110011
>   0011001100110011001100110011001100110011001100110011001100110011001100
>   1100110011001100110011001100110011001100110011001100110011001100110011
>
> Except that I lied -- it's not equal to that because I haven't yet
> typed "0011" enough times.  I have to keep typing it forever if I want
> an exact representation.

Actually, if you are dealing with rational real numbers (numbers that
can be expressed as fractions whose denominator and numerator are
integers) then it is possible to do all arithmetic precisely in a
finite amount of time.  The emacs based calculator, calc, does just
this.  It combines arbitrary precision integers with a fraction
oriented mode to give precise rational number arithmetic.

Of course in practice it is possible for the numerators and
denominators to get so large that computations take an unreasonable
amount of time...

Now if you need perfect representation combined with irrational
numbers you are in trouble.

Cheers,
  --jfc


------------------------------

Date: 17 Jul 2000 19:40:05 GMT
From: abigail@delanet.com.bbs@openbazaar.net (Abigail)
Subject: Re: Perl can't add !
Message-Id: <3bRNBE$WO6@openbazaar.net>

Stuart Horner (stuart@unidev.com) wrote on MMDX September MCMXCIII in
<URL:news:NUVb5.16320$EQ3.572715@news-east.usenetserver.com>:
__ I'm having a Perl adding error!


No, you don't. Now go read the FAQ and find out why you get this
behaviour.


Abigail
--
perl -e '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
         / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
         % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %;
         BEGIN {% % = ($ _ = " " => print "Just Another Perl Hacker\n")}'


------------------------------

Date: 17 Jul 2000 23:00:02 GMT
From: abigail@delanet.com.bbs@openbazaar.net (Abigail)
Subject: Re: Perl can't add !
Message-Id: <3bRSL3$VuA@openbazaar.net>

jcano@mmcnet.com (jcano@mmcnet.com) wrote on MMDXII September MCMXCIII in
<URL:news:lzlmz1cypc.fsf@mmcnet.com>:
``
`` Now if you need perfect representation combined with irrational
`` numbers you are in trouble.


Algebraic numbers can be represented perfectly in finite amount of memory.
After all, algebraic numbers are roots of finite degree polynooms,
with integer coordinates. The square root of 2 for instance is a root
of f(x):x^2 - 2.

Now, if only I can remember the rules for arithmetic.



Abigail
--
$;                                   # A lone dollar?
=$";                                 # Pod?
$;                                   # The return of the lone dollar?
{Just=>another=>Perl=>Hacker=>}      # Bare block?
=$/;                                 # More pod?
print%;                              # No right operand for %?


------------------------------

Date: 17 Jul 2000 23:40:04 GMT
From: logan@cs.utexas.edu.bbs@openbazaar.net (Logan Shaw)
Subject: Re: Perl can't add !
Message-Id: <3bRTN5$UuU@openbazaar.net>

In article <slrn8n75h5.ibq.abigail@alexandra.delanet.com>,
Abigail <abigail@delanet.com> wrote:
>jcano@mmcnet.com (jcano@mmcnet.com) wrote on MMDXII September MCMXCIII in
><URL:news:lzlmz1cypc.fsf@mmcnet.com>:
>``
>`` Now if you need perfect representation combined with irrational
>`` numbers you are in trouble.
>
>
>Algebraic numbers can be represented perfectly in finite amount of memory.
>After all, algebraic numbers are roots of finite degree polynooms,
>with integer coordinates. The square root of 2 for instance is a root
>of f(x):x^2 - 2.

Hmm...

If you can represent them, that's great.  But, can you do arithmetic on
them?  You can always do things symbolically, but ultimately you may
want to have an actual value for your expression.

Furthermore, it's really handy if the representation of the result of
an operation isn't more complicated than the representation of the
operands.  If it is more complicated (or if there isn't an upper bound
on how complicated it'll get), you have the disheartening property that
running time is more than proportional to the number of arithmetic
operations to be performed.

  - Logan


------------------------------

Date: 18 Jul 2000 02:30:03 GMT
From: abigail@delanet.com.bbs@openbazaar.net (Abigail)
Subject: Re: Perl can't add !
Message-Id: <3bRXhS$UuO@openbazaar.net>

Logan Shaw (logan@cs.utexas.edu) wrote on MMDXII September MCMXCIII in
<URL:news:8l05qi$nk2$1@provolone.cs.utexas.edu>:
"" In article <slrn8n75h5.ibq.abigail@alexandra.delanet.com>,
"" Abigail <abigail@delanet.com> wrote:
"" >jcano@mmcnet.com (jcano@mmcnet.com) wrote on MMDXII September MCMXCIII in
"" ><URL:news:lzlmz1cypc.fsf@mmcnet.com>:
"" >``
"" >`` Now if you need perfect representation combined with irrational
"" >`` numbers you are in trouble.
"" >
"" >
"" >Algebraic numbers can be represented perfectly in finite amount of memory.
"" >After all, algebraic numbers are roots of finite degree polynooms,
"" >with integer coordinates. The square root of 2 for instance is a root
"" >of f(x):x^2 - 2.
""
"" Hmm...
""
"" If you can represent them, that's great.  But, can you do arithmetic on
"" them?  You can always do things symbolically, but ultimately you may
"" want to have an actual value for your expression.

Yes, you can (the set algebraic numbers is closed under addition,
multiplication, subtraction, division and root extraction). There was
an (invited) talk at the Canadian Conference for Computational Geometry
(Waterloo, 1993) by Chee Yap where he did exactly this. Unfortunally, the
paper doesn't appear in the conference proceedings and I have forgotten
the details.

Polynomes with integer coordinates can be representated as simple
lists of integers. x^3 + x - 3, for instance, can be represented
as [-3, 1, 0, 1].

"" Furthermore, it's really handy if the representation of the result of
"" an operation isn't more complicated than the representation of the
"" operands.  If it is more complicated (or if there isn't an upper bound
"" on how complicated it'll get), you have the disheartening property that
"" running time is more than proportional to the number of arithmetic
"" operations to be performed.

Considering that you cannot represent sqrt(2) as a (binary) float using
finite precision, I'm not quite sure what you mean by "more complicated".

Some things are easy. For instance, if z is a root of the polynome

     a_n x^n + a_(n-1) x^(n-1) + ... + a_1 x + a_0

then 1/z is a root of the polynome

     a_0 x^n + a_1 x^(n-1) + ... + a_(n-1) x + a_n


Abigail
--
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
 .qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
 .qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'


------------------------------

Date: 18 Jul 2000 05:00:03 GMT
From: ilya@math.ohio-state.edu.bbs@openbazaar.net (Ilya Zakharevich)
Subject: Re: Perl can't add !
Message-Id: <3bRbd4$VWC@openbazaar.net>

[A complimentary Cc of this posting was sent to Abigail
<abigail@delanet.com>],
who wrote in article <slrn8n7hpb.ibq.abigail@alexandra.delanet.com>:
> "" >Algebraic numbers can be represented perfectly in finite amount of memory.
> "" >After all, algebraic numbers are roots of finite degree polynooms,
> "" >with integer coordinates. The square root of 2 for instance is a root
> "" >of f(x):x^2 - 2.

> "" If you can represent them, that's great.  But, can you do arithmetic on
> "" them?  You can always do things symbolically, but ultimately you may
> "" want to have an actual value for your expression.
>
> Yes, you can (the set algebraic numbers is closed under addition,
> multiplication, subtraction, division and root extraction). There was
> an (invited) talk at the Canadian Conference for Computational Geometry
> (Waterloo, 1993) by Chee Yap where he did exactly this. Unfortunally, the
> paper doesn't appear in the conference proceedings and I have forgotten
> the details.

perl -MMath::Pari=:DEFAULT,Mod -wle '
  $x = PARIvar "x"; $root_2 = Mod($x, $x**2 - 2);
  print 12 if $root_2*$root_2 == 2'
12

perl -MMath::Pari=:DEFAULT,Mod -wle '
  $x = PARIvar "x"; $y = PARIvar "y";
  $root_2 = Mod($x, $x**2 - 2);
  $root_3 = Mod($y, $y**2 - 3);
  $t = $root_2 + $root_3;
  print 12 if ($t**2 - 5)**2 == 24'
12

But working with algebraic numbers as solutions to equations is not
enough.  $s = sqrt(2) is not completely described by $s**2 - 2 == 0,
since -sqrt(2) is also a root.  Thus to work with algebraic numbers
one needs two pieces of info: the equation, and an approximation: say,
a pair (x**2 - 2, 1) denotes "the root of x**2 - 2 = 0 which is closest
to 1".

Given such two such representation for x and y, Math::Pari allows
finding the equation which, say, x+y satisfies.  However, to find an
*apropriate* approximation to x+y it is not enough to add the *given*
approximations of x and y.  Say, 2 is an appropriate approximation for
sqrt(2), 1 is appropriate for sqrt(3), but 2-1 is not appropriate for
sqrt(2)-sqrt(3).  [It corresponds to sqrt(3)-sqrt(2) which is the root
of the same equation.]

One may need to *improve* the given approximations before doing the
arithmetic operation.  [To simplify this, one may require that the
given approximation is in the basin of attraction of Newton method.]
Additionally, the equation for x+y may be decomposable (consider
sqrt(2)+sqrt(2), which satisfies not s**2 - 8 = 0, but s*(s**2 - 8) ==
0, so one may need to find which of the factors the exact sum satisfies.

Anyway, I think that writing a package (based on Math::Pari) for such
manipulations may be a nice exercise for a rainy evening.  Any takers?

Ilya


------------------------------

Date: 11 Jul 2000 18:30:02 GMT
From: mnarvaja@ar.geocities.xyz.com.bbs@openbazaar.net (Marcelo Narvaja)
Subject: Re: Perl CGI for Remote Control?
Message-Id: <3bMaJQ$YKP@openbazaar.net>

Try this implementacion of CGI.pm ,

#!/usr/local/bin/perl

use CGI;
$query = new CGI;
print $query->header;
$TITLE="Commands Test";

# We use the path information to distinguish between calls
# to the script to:
# (1) create the frameset
# (2) create the query form
# (3) create the query response

$path_info = $query->path_info;

# If no path information is provided, then we create
# a side-by-side frame set
if (!$path_info) {
    &print_frameset;
    exit 0;
}

# If we get here, then we either create the query form
# or we create the response.
&print_html_header;
&print_query if $path_info=~/query/;
&print_response if $path_info=~/response/;
&print_end;


# Create the frameset
sub print_frameset {
    $script_name = $query->script_name;
    print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="50,50">
<frame src="$script_name/query" name="query">
<frame src="$script_name/response" name="response">
</frameset>
EOF
    ;
    exit 0;
}

sub print_html_header {
    print $query->start_html($TITLE);
}

sub print_end {
    print qq{<P><hr><A HREF="cgi_docs.html">Go to the documentation</A>};
    print $query->end_html;
}

sub print_query {
    $script_name = $query->script_name;
    print "<H1>Frameset Query</H1>\n";
    print
$query->startform(-action=>"$script_name/response",-TARGET=>"response");
    print "Type the command ",$query->textfield('cmd');
    "<P>";
    print $query->submit;
    print $query->endform;
}

sub print_response {
    $stout = "/tmp/stout.$$";
    $sterr = "/tmp/sterr.$$";
    print "<H1>Frameset Result</H1>\n";
    unless ($query->param) {
 print "<b>No command submitted yet.</b>";
 return;
    }
    $cmd = $query->param(cmd);
    system("$cmd > $stout 2>$sterr");
    print "<B><I>Standard output:</I></B> <BR><XMP>",`cat
$stout`,"</XMP>\n";
    print "<P><B><I>Standard error:</I></B> <BR><XMP>",`cat
$sterr`,"</XMP>\n";
    unlink "$stout";
    unlink "$sterr";
}



Darren Ward wrote:

> Hi All,
>
> I'm trying to find a way to have a CGI window/frame to act as if it was
> an interactive program running on the web server.
>
> Specifically telnet and something like minicom to control a serial
> device.
> So it would appear that you have a shell prompt within a CGI frame and
> you can type in commands etc just like normal.
>
> So has anyone any thoughts on how to re-route a command line interface
> via a CGI? Any modules?
>
> Darren
>
> PS Solution will be posted to the groups.


------------------------------

Date: 16 Jul 2000 20:00:11 GMT
From: pad755@libero.it.bbs@openbazaar.net (Ðyl@ñ)
Subject: perl cgi that makes web pages from text file
Message-Id: <3bQYCB$T5Z@openbazaar.net>

Hi, I'm making a script that makes web pages from text files, what I don't
know is how to tell to the cgi wich text file it has to use to make the web
page.

this is the link that I wanto to use to build the web page
http://sito.com/script.cgi?fileA.txt


thanks


#!/usr/local/bin/perl

print "Content-type: text/html\n\n" ;

print <<EOF ;
<html>
<head><title>$title</title></head>
<body>


||||||||| here should go the text file


</body>
</html>
EOF


------------------------------

Date: 16 Jul 2000 22:20:02 GMT
From: bwalton@rochester.rr.com.bbs@openbazaar.net (Bob Walton)
Subject: Re: perl cgi that makes web pages from text file
Message-Id: <3bQbh4$UyH@openbazaar.net>

"Ðyl@ñ" wrote:
>
> Hi, I'm making a script that makes web pages from text files, what I don't
> know is how to tell to the cgi wich text file it has to use to make the web
> page.
>
> this is the link that I wanto to use to build the web page
> http://sito.com/script.cgi?fileA.txt
>
> thanks
>
> #!/usr/local/bin/perl
>
> print "Content-type: text/html\n\n" ;
>
> print <<EOF ;
> <html>
> <head><title>$title</title></head>
> <body>
>
> ||||||||| here should go the text file
>
> </body>
> </html>
> EOF

First, you are missing the line:

   use CGI;

from the beginning of your script.  Put that in and read the docs about
the CGI module.  Your question will be answered.
--
Bob Walton


------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 3723
**************************************


home help back first fref pref prev next nref lref last post