# CGI question

**URL:** <https://rubytalk.org/t/cgi-question/66788>\
**Category:** ruby-talk\
**Created:** [28 November 2012 10:58 UTC](https://rubytalk.org/t/cgi-question/66788 "2012-11-28T10:58:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [28 November 2012 10:58 UTC](https://rubytalk.org/t/cgi-question/66788/1 "2012-11-28T10:58:17Z")

</div>

Ruby Beginner question.

In the "Ruby and the Web" section of the pickaxe book they talk about  
CGI and give the following example:

> **···**
>
> # ======== require 'cgi' cgi = CGI.new("html3") # add HTML generation methods cgi.out do &nbsp;&nbsp;cgi.html do &nbsp;&nbsp;&nbsp;&nbsp;cgi.head { "\n"+cgi.title { "This Is a Test"} } + &nbsp;&nbsp;&nbsp;&nbsp;cgi.body do "\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.form do"\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[cgi.hr](http://cgi.hr) + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.h1 { "A Form: " } + "\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.textarea("get\_text") +"\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[cgi.br](http://cgi.br) + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.submit &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end
> 
> # They claim that this program produces:
> 
> # Content-Type: text/html Content-Length: 302 \<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"\>\<HTML\>\<HEAD\> \<TITLE\>This Is a Test\</TITLE\>\</HEAD\>\<BODY\> \<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded"\> \<HR\>\<H1\>A Form: \</H1\> \<TEXTAREA NAME="get\_text" COLS="70" ROWS="10"\>\</TEXTAREA\> \<BR\>\<INPUT TYPE="submit"\>\</FORM\>\</BODY\>\</HTML\>
> 
> # When i run this program i get nothing on the console output, and when i tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line (by stuck I mean further ENTERs don't give me the standard irb(main):007:0\> ) and gave the following message:
> 
> # (offline mode: enter name=value pairs on standard input)
> 
> can anyone explain to me what's going on?
> 
> Thanks.
> 
> Assaf.
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [28 November 2012 11:18 UTC](https://rubytalk.org/t/cgi-question/66788/2 "2012-11-28T11:18:20Z")

</div>

Hi,

CGI scripts are executed by a webserver like Apache. It makes no sense  
to run them in the console.

By the way, this code looks ancient. HTML 3.2 is 15(!) years old, it was  
around when the internet was still young.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [28 November 2012 11:28 UTC](https://rubytalk.org/t/cgi-question/66788/3 "2012-11-28T11:28:16Z")

</div>

Quoting Assaf Shomer (lists@ruby-forum.com):

> # When i run this program i get nothing on the console output, and when i tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line (by stuck I mean further ENTERs don't give me the standard irb(main):007:0\> ) and gave the following message:
> 
> # (offline mode: enter name=value pairs on standard input)
> 
> can anyone explain to me what's going on?

I have no experience with the cgi package at all, but the message is  
clear: IRB is waiting for something on its standard input, waiting for  
name=value pairs. If you want to find out exactly what these  
name=value pairs are, I suggest you study the cgi package more. But I  
can tell you that (at least in Linux) you tell the shell (and IRB)  
that you are done with providing data from the standard input with  
Ctrl-D. Indeed, if you press Ctrl-D at the above prompt, the object  
creation call returns successfully.

Carlo

> **···**
>
> Subject: CGI question  
> &nbsp;&nbsp;Date: Wed 28 Nov 12 07:58:17PM +0900
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![Eliezer\_Croitoru](https://avatars.discourse-cdn.com/v4/letter/e/6f9a4e/32.png) [@Eliezer\_Croitoru](https://rubytalk.org/u/Eliezer_Croitoru)\
**Post date:** [29 November 2012 05:00 UTC](https://rubytalk.org/t/cgi-question/66788/4 "2012-11-29T05:00:47Z")

</div>

Hey Assaf,

In a real server case it would send parameters to the script.  
In a command line case once the cgi code scope ended it will require the parameters entered one by one and CTRL+D should run it.

I am using ruby for CGI scripts but formatting a form this way can be very painful.  
I would go for writing the RAW html by myself with touches of variables if needed.

Regards,  
Eliezer

> **···**
>
> On 11/28/2012 12:58 PM, Assaf Shomer wrote:
> 
> > Ruby Beginner question.
> > 
> > In the "Ruby and the Web" section of the pickaxe book they talk about  
> > CGI and give the following example:
> > 
> > # ======== require 'cgi' cgi = CGI.new("html3") # add HTML generation methods cgi.out do &nbsp;&nbsp;&nbsp;cgi.html do &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.head { "\n"+cgi.title { "This Is a Test"} } + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.body do "\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.form do"\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.hr + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.h1 { "A Form: " } + "\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.textarea("get\_text") +"\n"+ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.br + &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.submit &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;end end
> > 
> > # They claim that this program produces:
> > 
> > # Content-Type: text/html Content-Length: 302 \<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"\>\<HTML\>\<HEAD\> \<TITLE\>This Is a Test\</TITLE\>\</HEAD\>\<BODY\> \<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded"\> \<HR\>\<H1\>A Form: \</H1\> \<TEXTAREA NAME="get\_text" COLS="70" ROWS="10"\>\</TEXTAREA\> \<BR\>\<INPUT TYPE="submit"\>\</FORM\>\</BODY\>\</HTML\>
> > 
> > # When i run this program i get nothing on the console output, and when i tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line (by stuck I mean further ENTERs don't give me the standard irb(main):007:0\> ) and gave the following message:
> > 
> > # (offline mode: enter name=value pairs on standard input)
> > 
> > can anyone explain to me what's going on?
> > 
> > Thanks.
> > 
> > Assaf.
> 
> --  
> Eliezer Croitoru  
> [https://www1.ngtech.co.il](https://www1.ngtech.co.il)  
> sip:ngtech@sip2sip.info  
> IT consulting for Nonprofit organizations  
> eliezer \<at\> ngtech.co.il

---

<div class="post-metadata">

**Author:** ![Prasadhnc\_C](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@Prasadhnc\_C](https://rubytalk.org/u/Prasadhnc_C)\
**Post date:** [28 November 2012 11:38 UTC](https://rubytalk.org/t/cgi-question/66788/5 "2012-11-28T11:38:45Z")

</div>

entering control-z will print the html contents on the console,  
If you want to pass some input to the script, you can enter name-value  
pairs in the console  
like, name=xyx.

> **···**
>
> On Wed, Nov 28, 2012 at 4:58 PM, Carlo E. Prelz \<fluido@fluido.as\> wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject: CGI question  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date: Wed 28 Nov 12 07:58:17PM +0900
> > 
> > Quoting Assaf Shomer (lists@ruby-forum.com):
> > 
> > \> When i run this program i get nothing on the console output, and when i  
> > \> tried to run it in irb, it got stuck on the cgi = CGI.new("html3") line  
> > \> (by stuck I mean further ENTERs don't give me the standard  
> > \> irb(main):007:0\> ) and gave the following message:  
> > \> ==============  
> > \> (offline mode: enter name=value pairs on standard input)  
> > \> ==============  
> > \> can anyone explain to me what's going on?
> > 
> > I have no experience with the cgi package at all, but the message is  
> > clear: IRB is waiting for something on its standard input, waiting for  
> > name=value pairs. If you want to find out exactly what these  
> > name=value pairs are, I suggest you study the cgi package more. But I  
> > can tell you that (at least in Linux) you tell the shell (and IRB)  
> > that you are done with providing data from the standard input with  
> > Ctrl-D. Indeed, if you press Ctrl-D at the above prompt, the object  
> > creation call returns successfully.
> > 
> > Carlo
> > 
> > --  
> > &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> > \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> > &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [28 November 2012 22:24 UTC](https://rubytalk.org/t/cgi-question/66788/6 "2012-11-28T22:24:09Z")

</div>

not true:

10001 % ruby19  
require 'cgi'  
cgi = CGI.new("html3") # add HTML generation methods  
cgi.out do  
cgi.html do  
&nbsp;&nbsp;&nbsp;cgi.head { "\n"+cgi.title { "This Is a Test"} } +  
&nbsp;&nbsp;&nbsp;cgi.body do "\n"+  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.form do"\n"+  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.hr +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.h1 { "A Form: " } + "\n"+  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.textarea("get\_text") +"\n"+  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.br +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.submit  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
end  
^d  
(offline mode: enter name=value pairs on standard input)  
^d  
Content-Type: text/html  
Content-Length: 302

\<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"\>\<HTML\>\<HEAD\>  
\<TITLE\>This Is a Test\</TITLE\>\</HEAD\>\<BODY\>  
\<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded"\>  
\<HR\>\<H1\>A Form: \</H1\>  
\<TEXTAREA NAME="get\_text" COLS="70" ROWS="10"\>\</TEXTAREA\>  
\<BR\>\<INPUT TYPE="submit"\>\</FORM\>\</BODY\>\</HTML\>

> **···**
>
> On Nov 28, 2012, at 03:18 , Jan E. \<lists@ruby-forum.com\> wrote:
> 
> > CGI scripts are executed by a webserver like Apache. It makes no sense  
> > to run them in the console.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [29 November 2012 00:04 UTC](https://rubytalk.org/t/cgi-question/66788/7 "2012-11-29T00:04:48Z")

</div>

Ryan Davis wrote in post #1086967:

> > CGI scripts are executed by a webserver like Apache. It makes no sense  
> > to run them in the console.
> 
> not true:

I did not mean it's impossible. Of course CGI scripts are normal  
programs.

But I don't see the point of writing CGI scripts without actually  
running them on a webserver and inspecting the result in a browser. What  
do you learn from an obscure script that you have to execute in a  
"strange" way to see some random gibberish on the console? There isn't  
any context.

Anyway, if that was the goal of the exercise, I guess he can move on  
now.

> **···**
>
> > On Nov 28, 2012, at 03:18 , Jan E. \<lists@ruby-forum.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [29 November 2012 12:35 UTC](https://rubytalk.org/t/cgi-question/66788/8 "2012-11-29T12:35:41Z")

</div>

Thanks Ryan, That definitely did the trick.  
I had no idea that i can just type 'ruby1.9.1' and then type ruby code  
and CTRL+D executes it.

What would be the equivalent thing to do if I'm inside eclipse, that is,  
how can I see this outputs puked to the console?

Thanks again,

Assaf.

> **···**
>
> ---------------------------------------------
> 
> Ryan Davis wrote in post #1086967:
> 
> > On Nov 28, 2012, at 03:18 , Jan E. \<lists@ruby-forum.com\> wrote:
> > 
> > > CGI scripts are executed by a webserver like Apache. It makes no sense  
> > > to run them in the console.
> > 
> > not true:
> > 
> > 10001 % ruby19  
> > require 'cgi'  
> > cgi = CGI.new("html3") # add HTML generation methods  
> > cgi.out do  
> > cgi.html do  
> > &nbsp;&nbsp;&nbsp;cgi.head { "\n"+cgi.title { "This Is a Test"} } +  
> > &nbsp;&nbsp;&nbsp;cgi.body do "\n"+  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.form do"\n"+  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.hr +  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.h1 { "A Form: " } + "\n"+  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.textarea("get\_text") +"\n"+  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.br +  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cgi.submit  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > end  
> > ^d  
> > (offline mode: enter name=value pairs on standard input)  
> > ^d  
> > Content-Type: text/html  
> > Content-Length: 302
> > 
> > \<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"\>\<HTML\>\<HEAD\>  
> > \<TITLE\>This Is a Test\</TITLE\>\</HEAD\>\<BODY\>  
> > \<FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded"\>  
> > \<HR\>\<H1\>A Form: \</H1\>  
> > \<TEXTAREA NAME="get\_text" COLS="70" ROWS="10"\>\</TEXTAREA\>  
> > \<BR\>\<INPUT TYPE="submit"\>\</FORM\>\</BODY\>\</HTML\>
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
