From: ahoward Date: 2003-04-01T04:57:21+09:00 Subject: Re: [Q] CGI::Session On Tue, 1 Apr 2003, Bermejo, Rodrigo wrote: > Have nice & productive week everyone. > > I am trying to create a CGI:Session , but I am having troubles > understanding the CGI behavior. > > I am trying to use the CGI class just to handle session information, and > use a simple 'print' > to generate my html document, > > After creating a CGI object it takes control of the STDOUT ? > > #!/usr/bin/env ruby > #-----------------> > #Abstraction of my problem > #This code does not work > def head(title) > require 'cgi' > require 'cgi/session' > print "Content-type: text/html\r\n\r\n" > > #Session handling > cgi=CGI::new("html3") > session=CGI::Session(cgi) > print "You are logged in as: #{session[\"login\"]}" if > session["login"] > > html_out= "" + > "#{title}" + > "" > print html_out > > end > #-----------------< > > thks rodrigo- a couple of things, some of which may or may not apply to you - but all of which i hacked through : * shebang '#!/usr/bin/env ruby' may or may NOT work depending on whether or not ruby is in user 'nobody's (or whatever you web server runs as) path, you may want to use '#!/path/to/ruby' instead. * placing 'cgi=CGI::new("html3")' in a method could cause the cgi program to hang. this method will attempt to read from stdin, and block if it cannot. thus calling this > once (i'm assuming you may have other methods other than 'head(title)') may cause your cgi to hang * CGI doesn't 'take over' STDOUT, but does use it. you can use it too if you like. i personally do not use the cgi methods too much, but do tend to do something like content = <<-html #{stuff} html cgi.out { content } when i'm not using amrita. * when composing output for cgi's consider doing something like html_out = '' html_out << "" << "#{title}" << "" instead of String.+() since '+' creates a new object for each call. that being said (sorry if that was more/less than you asked for), i can get your code (modified) to run as : http://eli.fsl.noaa.gov/tmp/tmp.cgi hope that helps. -a ps. unless you have serious speed contraints, i would highly reccomend looking at amrita - it makes cgi programs ridulously short and easy to read. -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ahoward@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================