Issues with subroutines and unbless references

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
Winfried Neessen [ Di, 22 Juni 2010 12:01 ] [ ID #2043515 ]

Re: Issues with subroutines and unbless references

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
> 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=

> derived object)"
>
> Here is a code example of what I've written:
>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D begin =
code snippet =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=2E..
> ## 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=

object holds all input headers. This object can then be used as tied hash o=
r
as described in L<APR::Table>.

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
input header collection. Hence the message.

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

Torsten Förtsch

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

Like fantasy? http://kabatinte.net
torsten.foertsch [ Di, 22 Juni 2010 12:40 ] [ ID #2043516 ]

RE: Issues with subroutines and unbless references

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<APR::Table>.
>
Thanks a bunch! That makes sense. Your solution worked for me.


Winni
Winfried Neessen [ Di, 22 Juni 2010 12:59 ] [ ID #2043517 ]
Webserver » gmane.comp.apache.mod-perl » Issues with subroutines and unbless references

Vorheriges Thema: Bucket Heap? Error?
Nächstes Thema: modperl book