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


In reply to Can't call method "url" on an undefined value? But it appears defined! by mjfrazer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.