Issues with subroutines and unbless references

Issues with subroutines and unbless references

am 22.06.2010 12:01:39 von Winfried Neessen

Hi,

I'm pretty new to mod_perl/Apache2::* and I'm already struggling around with
my first problem.

I am writing a script which later will be kind of proxy server for our self
written application.
I need to alter/manage some of the HTTP headers sent by the application. My
script is currently
not useable at all, though it already provides an error to me which I can't
get rid of.

I have a subroutine "handler" where I import the HTTP object from mod_perl
via: my $r = shift;
This works well and as expected. But if I wanna use that object later on in
a different
subroutine, I get the following error message everytime I try to use the $r
object in the
subroutine: "argument is not a blessed reference (expecting an APR::Table
derived object)"

Here is a code example of what I've written:

====================== begin code snippet =================================
## The main program handler // handler() {{{
sub handler
{

## Get HTTP Apache object
my $r = shift;

## Get a log object
my $log = get_logger( 'handler' );

## Log a start message
$log->info( __PACKAGE__ . ' successfully started.' );

## Get allowed headers as hashref
my $headers = getAllowedHeaders( $r );

## Exit gracefully
$r->content_type( 'text/html' );
return Apache2::Const::OK;


}
# }}}

## Extract sent HTTP headers and extract the allowed ones //
getAllowedHeaders() {{{
sub getAllowedHeaders
{

## Get Logger object
my $log = get_logger( 'getAllowedHeaders' );

## Get HTTP object from caller
my $r = shift;

## Define a temp. header hashref
my $header;

## Go through the list of allowed headers and check if the are
defined {{{
foreach my $head ( sort ALLOWED_HEADER )
{

## Check if the HTTP object has the corresponding header set
next unless defined( $r->headers_in( $head ) );

## Extract value from HTTP object
my $value = $r->headers_in( $head );

## Log a debug message
EXT_DEBUG && $log->debug( 'Found allowed HTTP header "' .
$head . ' with value: ' . $value );

## If the header is defined in the HTTP object, store it for
later usage
$header->{ $head } = $value;

}
# }}}

## Return the hashref
return $header;

}
# }}}
====================== end code snippet =================================

Whenever I call the "$r->headers_in( $head )" in the getAllowedHeaders
subroutine I
receive the error message about the unblessed reference.

Any hint about how to fix this issue would be greatly appreciated.


Thanks
Winni

Re: Issues with subroutines and unbless references

am 22.06.2010 12:40:20 von torsten.foertsch

On Tuesday 22 June 2010 12:01:39 Winfried Neessen wrote:
> I have a subroutine "handler" where I import the HTTP object from mod_per=
l=20
> via: my $r =3D shift;
> This works well and as expected. But if I wanna use that object later on
> in a different
> subroutine, I get the following error message everytime I try to use the
> $r object in the
> subroutine: "argument is not a blessed reference (expecting an APR::Table=
=20
> derived object)"
>=20
> Here is a code example of what I've written:
>=20
> ==================== == begin =
code snippet ==================== =
=============3D
..
> ## Extract value from HTTP object
> my $value =3D $r->headers_in( $head );

That's not the way to go. $r->headers_in returns an APR::Table object. This=
=20
object holds all input headers. This object can then be used as tied hash o=
r=20
as described in L.

In your case I'd do

$r->headers_in->{$head}

or

$r->headers_in->get($head)

The way you call headers_in means you want to set a new APR::Table object a=
s=20
input header collection. Hence the message.

..
> ==================== == end co=
de snippet ==================== =3D=
============

Torsten Förtsch

=2D-=20
Need professional modperl support? Hire me! (http://foertsch.name)

Like fantasy? http://kabatinte.net

RE: Issues with subroutines and unbless references

am 22.06.2010 12:59:04 von Winfried Neessen

Hi Torsten,

> That's not the way to go. $r->headers_in returns an APR::Table object.
> This
> object holds all input headers. This object can then be used as tied hash
> or
> as described in L.
>
Thanks a bunch! That makes sense. Your solution worked for me.


Winni