1) How to debug CGI.pm cookies 2) Template.pm with CGI.pm

M

Mr I

Hi all,
I would like so help on 2 issues

1) debugging CGI.pm cookies
How does one view the content of a cookie when it is submitted by the
client browser?
Also is there a way of simulating cookie data via the command line?

Currently I use a combination of print statments and Data::Dumper,
surely there must be an inbuilt method.

2) Template.pm with CGI.pm
I have recently started using CGI.pm below is a subsection of code

----
$query = new CGI;
$query->param('file').....
.....
my $ttconfig = {
# allow absolute path statments
ABSOLUTE => 1,
};

my $tt = Template->new($ttconfig);
$tt->process('./foo.tt', { centre => $query }) || die $tt->error;
-------

in foo.tt i have TT syntax like

[% centre.file %]

whenthe program is excuted I get the error

undef error - Undefined subroutine CGI::failure
at (eval 12) line 9

I am aware that I am referencing the data incorrectly in foo.tt, but I
am unable to work out the correct method:
I thought Template.pm could figure out what data type it is passed?
$query is of type CGI. Is it because it is not a builtin type
Template.pm does not know what to do?
What is the best method to pass CGI info to a template

what I'm doing now:
Investigated TT module Template::plugin::CGI
added
[% USE CGI %]
get error:
undef error - Undefined subroutine CGI::failure
at (eval 12) line 13


thank you in advance for any advice.
K
 
T

Tore Aursand

1) debugging CGI.pm cookies
How does one view the content of a cookie when it is submitted by the
client browser?

Personally, almost everything related to debugging is "done by" by a Log
module I'm using. In my code I could write:

my $cookie_value = $cgi->cookie( 'auth_my_site' );
$Log->debug( 'Found cookie: ' . $cookie_value );

On one of my virtual desktops (I'm running Gnome) I have a terminal
windows open where i 'tail' Apache's error log. Voilá! :)
Currently I use a combination of print statments and Data::Dumper,
surely there must be an inbuilt method.

No. Perl is a programming language. It's not intended to be used for
debugging HTTP data. :)

What's your excact problem, really? I'm familiar with the 'I want to
authenticate users by using a cookie'-problem, but I really can't see
what's so hard about it?

Follow these steps:

1. If the user is logging in, authenticate the user based on
username and password.

2. If not logging in, then grab the wanted cookie to see if
the client is logged in.

3. If he is logged, check if he wants to log out.

Many seems to forget to authenticate the user _before_ they check if he
wants to log out. How are you able to log out a user when you don't know
anything about the user? You don't even know if he is logged in. :)
undef error - Undefined subroutine CGI::failure
at (eval 12) line 9

Well. Stop giving us a "subsection of the code" and rather give us what's
on that particular line (and related code)!

On the other hand; Is there a 'failure' subroutine in CGI.pm? Never
heard of it.

Haven't used Template.pm, though, so if that's where the problem is, I
can't help much.


--
Tore Aursand <[email protected]>

"You know the world is going crazy when the best rapper is white, the best
golfer is black, France is accusing US of arrogance and Germany doesn't
want to go to war."
 
F

Frodo Larik

Mr said:
How does one view the content of a cookie when it is submitted by the
client browser?

I use this:

sub get_Cookie()
{
my $lookfor = $_[0];
my $cookie;

if ( defined($ENV{'HTTP_COOKIE'}) )
{
foreach $cookie ( split (/; /,$ENV{'HTTP_COOKIE'}) )
{
if ( $cookie =~ /^$lookfor=/ )
{
my $value = $cookie;
$value =~ s/^$lookfor=//;
return $value;
}
}
}
return "1";
}


and i get the content by using the name of the cookie, so if cookie is
called 'bla', my $value = &get_Cookie('bla');

Sincerly,

Frodo Larik
 
M

Mr I

Tore Aursand said:
Personally, almost everything related to debugging is "done by" by a Log
module I'm using. In my code I could write:

my $cookie_value = $cgi->cookie( 'auth_my_site' );
$Log->debug( 'Found cookie: ' . $cookie_value );

On one of my virtual desktops (I'm running Gnome) I have a terminal
windows open where i 'tail' Apache's error log. Voilá! :)
Ditto.. just not using log module...
No. Perl is a programming language. It's not intended to be used for
debugging HTTP data. :)
clarify my statement:
surely CGI.pm has an inbuilt method (not directed @perl)
What's your excact problem, really? I'm familiar with the 'I want to
authenticate users by using a cookie'-problem, but I really can't see
what's so hard about it?
My problem is tracing errors. I just wish to speed up my error
diagnostic times.
The BEST method would be to do diagnostic via command line (which
CGI.pm supports) however I have no idea how to import cookie
information into my code via the command line (in the same mannor you
can with name, value pairs).
Well. Stop giving us a "subsection of the code" and rather give us what's
on that particular line (and related code)!

All the important parts are there, in fact if you executed the script
minimal changes (i.e. add use statements, adding #!/path/to/perl,
creating foo.tt with TT syntax inside etc) you will get a similar
error.
On the other hand; Is there a 'failure' subroutine in CGI.pm? Never
heard of it.
Indeed. as the error states it seems to be the lack of subroutine
"failure" in CGI.pm that is causing the error...
Another clarification:
The subroutine changes depending on the values of $query->param..
am slllooowwlly getting an understanding... however would like to call
on knowledge of experinced CGI.pm / Template.pm users..
Haven't used Template.pm, though, so if that's where the problem is, I
can't help much.
Thanks in any case :)
 
M

Mr I

Mr said:
Hi all,
I would like so help on 2 issues

1) debugging CGI.pm cookies
How does one view the content of a cookie when it is submitted by the
client browser?
Also is there a way of simulating cookie data via the command line?

Currently I use a combination of print statments and Data::Dumper,
surely there must be an inbuilt method.

2) Template.pm with CGI.pm
I have recently started using CGI.pm below is a subsection of code

----
$query = new CGI;
$query->param('file').....
....
my $ttconfig = {
# allow absolute path statments
ABSOLUTE => 1,
};

my $tt = Template->new($ttconfig);
$tt->process('./foo.tt', { centre => $query }) || die $tt->error;
-------

in foo.tt i have TT syntax like

[% centre.file %]

whenthe program is excuted I get the error

undef error - Undefined subroutine CGI::failure
at (eval 12) line 9

I am aware that I am referencing the data incorrectly in foo.tt, but I
am unable to work out the correct method:
I thought Template.pm could figure out what data type it is passed?
$query is of type CGI. Is it because it is not a builtin type
Template.pm does not know what to do?
What is the best method to pass CGI info to a template

what I'm doing now:
Investigated TT module Template::plugin::CGI
added
[% USE CGI %]
get error:
undef error - Undefined subroutine CGI::failure
at (eval 12) line 13


thank you in advance for any advice.
K
 
M

Mr I

Frodo said:
Mr said:
How does one view the content of a cookie when it is submitted by the
client browser?


I use this:

sub get_Cookie()
{
my $lookfor = $_[0];
my $cookie;

if ( defined($ENV{'HTTP_COOKIE'}) )
{
foreach $cookie ( split (/; /,$ENV{'HTTP_COOKIE'}) )
{
if ( $cookie =~ /^$lookfor=/ )
{
my $value = $cookie;
$value =~ s/^$lookfor=//;
return $value;
}
}
}
return "1";
}


and i get the content by using the name of the cookie, so if cookie is
called 'bla', my $value = &get_Cookie('bla');
Thanks. Once again I have done a similar solution in my code.
But this does not help when in command line mode as i cannot workout how
to pass cookie info to CGI.pm

Thanks still for your advice...
K
 
T

Tore Aursand

Ditto.. just not using log module...

Well. Why not? There are a lot of decent Log modules out there. I use
one I've written myself, though.

As a last resort, you can always use 'print STDERR' combined with a DEBUG
constant in your script. However, you might want to examine 'caller()'
everytime, which "requires" (practically) an own module for the job.
surely CGI.pm has an inbuilt method (not directed @perl)

Well. You can always specify CGI.pm to use the '-debug' flag;

use CGI qw:)cgi -compile -debug);

Never used it, though.
 
A

Alan J. Flavell

But this does not help when in command line mode as i cannot workout how
to pass cookie info to CGI.pm

Cookies are presented to a CGI script by the CGI interface via the
"environment", specifically HTTP_COOKIE. So if you want to test input
to your CGI script from the command line, presumably you'd want to set
the corresponding environment variable prior to calling the script
from the command line.

CGI.pm offers a debugging convenience of accepting query parameters
and their values from the command line (or in -debug mode from
standard input) as shown at
http://stein.cshl.org/WWW/software/CGI/#debugging

But cookies are not query parameters, and I'm not aware of any
trickery offered by CGI.pm to get those to your script from the
command line. I'd say set them up in the environment before you
invoke your script from the command line, if you're sure you want to
debug that way.

There's only a limited amount of useful testing that one can do in
this way, IMHO. I find it more useful to have a local web server
running, and test CGI scripts from that, and then you have the benefit
of the cookie module on the server side, and your browser on the
client side, to take care of the nitty details of HTTP.

On the other hand if browser or server are misbehaving then you might
want some kind of instrumented proxy between the client and the serve
to tell you what's going on. You might want to take a look at Nick
Kew's cg-eye. http://www.htmlhelp.com/tools/cg-eye/

There's very little that's Perl-specific in any of this. Most of this
discussion would seem to me to be better at home on the group
comp.infosystems.www.authoring.cgi
 
M

Mr I

Alan said:
Cookies are presented to a CGI script by the CGI interface via the
"environment", specifically HTTP_COOKIE. So if you want to test input
to your CGI script from the command line, presumably you'd want to set
the corresponding environment variable prior to calling the script
from the command line.
That make complete sense. Very intresting, will investigate...
CGI.pm offers a debugging convenience of accepting query parameters
and their values from the command line (or in -debug mode from
standard input) as shown at
http://stein.cshl.org/WWW/software/CGI/#debugging
indeed this is what I am using -debug
But cookies are not query parameters, and I'm not aware of any
trickery offered by CGI.pm to get those to your script from the
command line. I'd say set them up in the environment before you
invoke your script from the command line, if you're sure you want to
debug that way.

There's only a limited amount of useful testing that one can do in
this way, IMHO. I find it more useful to have a local web server
running, and test CGI scripts from that, and then you have the benefit
of the cookie module on the server side, and your browser on the
client side, to take care of the nitty details of HTTP.
I use and array of helpful modules to parse and display the running
code, data and messages but I'm sure you'll agree that it is far faster
to debug code on the command line (using perl -d) that to continiously
do client server request.
There's very little that's Perl-specific in any of this. Most of this
discussion would seem to me to be better at home on the group
comp.infosystems.www.authoring.cgi
Partly agree. All seem to have focused on the least of my problems that
is only the 1st part of my question (see title).

I was hoping someone would say something like "yes silly use
CGI::DisplayCookie" or "its in perldoc ..." but alias....

I am far more intrested in the CGI.pm Template.pm questions / solution.

I feel that this is the right group as i am trying to understand the
perl code and why it is behaving like this. Although as time has passed
since the orignal post, I think I am getting closer to understanding the
error.
I am no perl guru or authority so if indeed this type of knowledge is
best served in authoring.cgi
let me know ;)

Thanx
K
 
M

Mr I

Mr I wrote:

Partly agree. All seem to have focused on the least of my problems that
is only the 1st part of my question (see title).

I was hoping someone would say something like "yes silly use
CGI::DisplayCookie" or "its in perldoc ..." but alias....

Took my own advice :(

2) Template.pm with CGI.pm
original post stated problem / solution. Stopped passing the complex
data structure CGI and send just the value name pair using CGI's Vars()
as stated in perldoc CGI in section "FETCHING THE PARAMETER LIST AS A HASH:"

:(

Thanks to all for help.
K
 
A

Alan J. Flavell

Mr I wrote:

(well, actually this was I:)
The lesson we might learn from this as far as newsgroup strategy is
concerned is: if you have several questions, then choose the
appropriate group for posting each one separately. No matter if you
have to repeat the background detail each time (you've got copy/paste
after all): make each question stand on its own feet in the group
where it fits best, and that way you rate to get optimum answers to
each, and a fairly tidy thread that anyone afterwards can consult and
learn from. There are no guarantees (this is usenet, after all) but
given that it's a rough anarchy, there's nothing to be gained by
crossposting a laundry list of questions across all the groups where
one of them would be on-topic. Make sense? (well it does to me).

all the best

(there's no law against saying something like "oh by the way I've
posted some related questions on newsgroups X and Y if anyone's
interested in the big picture".)
 
L

Louis Erickson

: Took my own advice :(

Why is it so sad that you solved your problem?

: 2) Template.pm with CGI.pm
: original post stated problem / solution. Stopped passing the complex
: data structure CGI and send just the value name pair using CGI's Vars()
: as stated in perldoc CGI in section "FETCHING THE PARAMETER LIST AS A HASH:"

I tend to have a section in my templates that's just a string called
"deubgstr" or something, that if it exists, is emitted inside <pre> tags.

Then, when I have to know what's going on, I tend to set it there, and
it turns up in the output where the template puts it.

For CGI and cookies, look in to Data::Dumper. Just use that on the CGI
object, and you'll get it's internal variables, which are reasonably clear.
Dumper()-ing the output of Vars might be clearer.

I guess I wouldn't have expected a way to diagnose these other than to
output them somehow, and because there are so many ways to output, I
wouldn't have expected a standard way.

Glad to hear you're unstuck.
 
M

Mr I

Louis said:
: Took my own advice :(

Why is it so sad that you solved your problem?
Not really sad. But could have solved it faster IF i had just read
perldoc ;)

Thanks for you advance...

K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top