mjfrazer has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Back doing a project in perl after 10 years away and trying out this new-fangled OO packaging. I've only used packages previously to collect functions and static variables rather than actually declaring any objects. My first attempt is not working out so far.

This is a web service, index.pl is:
#!/usr/bin/perl use strict; use CGI; use Template; use SBL_Config; my $q = CGI->new; my $t = Template->new; my $config = SBL_Config->new; $config->check($q) || die $config->error; print $q->header(); $t->process('index.tt', {}) || die $t->error;
So, I'm just trying to pass the CGI object into my SBL_Config object to run the check method. My SBL_Config object is unable to access the methods in the CGI object. SBL_Config.pm is:
#!/usr/bin/perl package SBL_Config; use strict; use warnings; use diagnostics; use Data::Dumper; my $root_url = "/sbl"; sub new { my $self = {}; $self->{ROOT} = $root_url; $self->{ERROR} = undef; bless($self); return $self; } sub root { my $self = shift; if (@_) { die "SBL_Config::root is read-only"; } return $self->{ROOT}; } sub error { my $self = shift; if (@_) { die "SBL_Config::error is read-only"; } return $self->{ERROR}; } sub check { my ( $self, $q ) = @_; # my $self = shift; # my $q = shift; die "No CGI query passed to SBL_Config::check" if undef $q; print STDERR "q is " . (undef $q ? "undef" : "defined") . "\n"; print STDERR Dumper $q; my $loc; $loc = $q->url() || die "No URL from CGI"; $loc = $q->url(-relative=>1) || die "No relative URL from CGI"; if ($loc != $self->root ) { $self->{ERROR} = "Bad root URL"; retur +n 0; } return 1; }
Note I have tried both methods of assigning $self and $q in the check method.

In case it matters, index.tt is just:

<html><head>A title</head> <body>A body</body></html>
I get the following error in my apache error_log:
] q is defined ] $VAR1 = undef; ] Can't call method "url" on an undefined value at SBL_Config.pm line +42 (#1) ] (F) You used the syntax of a method call, but the slot filled by + the ] object reference or package name contains an undefined value. S +omething ] like this will reproduce the error: ] ] $BADREF = undef; ] process $BADREF 1,2,3; ] $BADREF->process(1,2,3); ] ] Uncaught exception from user code: ] \tCan't call method "url" on an undefined value at SBL_Config.pm lin +e 42. ] at SBL_Config.pm line 42 ] \tSBL_Config::check('SBL_Config=HASH(0x24bd2e8)', 'CGI=HASH(0x1dcdf4 +0)') called at /var/www/html/streetcar/index.pl line 12 ] Premature end of script headers: index.pl

How are my checks for undef passing, but the methods are not available and the object can not be accessed by Data::Dumper?

My perl is 5.10.1 (RHEL 6.4) which is a bit old, but should be new enough for this old stuff to work. The perltoot and perlboot perldocs with 5.10 indicate that all this should still work. But I cannot seem to grab onto what is wrong here.

Thanks for reading!
-mark

Replies are listed 'Best First'.
Re: Can't call method "url" on an undefined value? But it appears defined!
by Corion (Patriarch) on Jan 25, 2014 at 20:12 UTC

    The following code:

    undef $q ? "undef" : "defined"

    is unlikely to do what you intend it to do. undef explains it a bit more.

    Most likely, you wanted to use defined instead.

      That was indeed what got me. Thanks for your reply!
Re: Can't call method "url" on an undefined value? But it appears defined!
by AnomalousMonk (Archbishop) on Jan 25, 2014 at 21:35 UTC

    Further to Corion's reply above:

    The following code ... is unlikely to do what you intend it to do.

    Likewise the statement
        die "No CGI query passed to SBL_Config::check" if undef $q;
    which dies if the undefined value returned by undef after undefining  $q is true (it never will be). (Update: Furthermore,  $q is always undef-ed in this statement, thus rendering it useless — and then you try to use it!)

    Maybe you mean
        die ... unless defined $q;
    or
        die ... if ! defined $q;

      Thanks for taking the time to reply. Much appreciated.