bless doesn't seem to work.

R

RyanMcCoskrie

I'm trying to piece to gether a adventure like[1] game.

This is something that I have copied almost straight from the man
page:
package room_t;
use exit_t;

sub new()
{
my $self = {};
my $class = shift;

$self->{NAME} = shift;
$self->{NUMBER}= shift;
@{$self->{EXITS}} = [];

return bless($self, $class);
}

The trouble starts here though:
$temp = room_t->new("Start", 0);
push(@world, $temp);
$world[0]->addExit("End", 1);

These three lines (in the buildWorld() subroutine) result in the error:
Can't call method "addExit" on unblessed reference at ./game.pl line 22.

Have I done something wrong? Or is this an interpreter issue?

[1]This may turn into "Indiana Jones and the recursive macro processor"
for the sake of silliness.
 
T

Tad J McClellan

This is something that I have copied almost straight from the man
^^^^^^
^^^^^^

As is usual with programming, the devil is in the details.


[ snip code ]

Have I done something wrong?


Not as far as we can tell, but the "almost" is worrisome.

This short and complete example that you can run (as is suggested
in the Posting Guidelines that are posted here frequently) works
just fine.

The problem is in some of the code that we have not seen...


------------------------
#!/usr/bin/perl
use warnings;
use strict;

package room_t;

sub new()
{
my $self = {};
my $class = shift;

$self->{NAME} = shift;
$self->{NUMBER}= shift;
@{$self->{EXITS}} = [];

return bless($self, $class);
}

sub addExit { warn "addExit() got called\n" }


package main;

my @world;
my $temp = room_t->new("Start", 0);
push(@world, $temp);
$world[0]->addExit("End", 1);
------------------------
 
R

RyanMcCoskrie

Tad said:
RyanMcCoskrie said:
This is something that I have copied almost straight from the man page
As is usual with programming, the devil is in the details.
[ snip code ]
Have I done something wrong?

Not as far as we can tell, but the "almost" is worrisome.
When I said almost I mean that I changed some names. I was working from
the example of the person class in the perltoot man page. The constructor
subroutine is near identical.
This short and complete example that you can run (as is suggested
in the Posting Guidelines that are posted here frequently) works
just fine.
[snip code out]
The problem is in some of the code that we have not seen...
Okay then. I've tried making what I have produced closer to the example and
it failed.
All of this code is Copyright of my self (Ryan McCoskrie) 2009 and is under
the GPL (available on the Free Software Foundation website www.fsf.org).


Note that I found a description for the type of programmer that I am.
A rabid prototyper. There are other incarnations of this program with
other parts implemented and their own sets of mysterious bugs.
This is the only one in perl though.

#########################
#From the file exit_t.pm#
#########################
package exit_t;
use strict;
sub new()
{
my $self = {};
my $class = shift;

my $self->{NAME} = shift;
my $self->{NUMBER} = shift;

bless ($self, $class);
return $self;
}
1;

#########################
#From the file room_t.pm#
#########################
package room_t;
use strict;
use exit_t;

sub new()
{
my $self = {};
my $class = shift;

$self->{NAME} = shift;
$self->{NUMBER}= shift;
@{$self->{EXITS}} = [];

return bless($self, $class);
}

sub addExit()
{
my $self = shift;
push(@{$self->{EXITS}}, exit_t->new(shift, shift));
}

1;

#######################
#From the file game.pl#
#######################

#!/usr/bin/perl

use warnings;
use strict;
use room_t;

package main;

sub buildWorld();
my @world = [];
my $room = undef;


buildWorld();
my $loop = 1;

while( $loop != 0 ){
printf("> ");
my $cmd = <STDIN>;
}


sub buildWorld()
{
my $temp = room_t->new("Start", 0);
push(@world, $temp);
$world[0]->addExit("End", 1);
$temp = room_t->new("End", 1);
push(@world, $temp);

$room = \$world[0];
}

1;
 
J

J. Gleixner

RyanMcCoskrie said:
Tad said:
RyanMcCoskrie said:
This is something that I have copied almost straight from the man page
As is usual with programming, the devil is in the details.
[ snip code ]
Can't call method "addExit" on unblessed reference at ./game.pl line 22.
Have I done something wrong?
Not as far as we can tell, but the "almost" is worrisome.
When I said almost I mean that I changed some names. I was working from
the example of the person class in the perltoot man page. The constructor
subroutine is near identical.
This short and complete example that you can run (as is suggested
in the Posting Guidelines that are posted here frequently) works
just fine.
[snip code out]
The problem is in some of the code that we have not seen...
Okay then. I've tried making what I have produced closer to the example and
it failed.

So your first thought is to post the code so someone else
can fix it for you?
All of this code is Copyright of my self (Ryan McCoskrie) 2009 and is under
the GPL (available on the Free Software Foundation website www.fsf.org).

That made me laugh so hard my spleen fell out... I don't think you
have much to worry about.
Note that I found a description for the type of programmer that I am.
A rabid prototyper.

rabid : affected with rabies.

Maybe that explains why your code is so poor. You might
want to get that checked.

There are other incarnations of this program with
other parts implemented and their own sets of mysterious bugs.

blah.. blah..
This is the only one in perl though.

oh.. I'm pretty sure there are more..
#########################
#From the file exit_t.pm#
#########################
package exit_t;
use strict;
sub new()
{
my $self = {};
my $class = shift;

Typically, you'd order it so the parameters are set
first, then define $self.
my $self->{NAME} = shift;
my $self->{NUMBER} = shift;

Really?? Well. I guess that's 'near identical'....
bless ($self, $class);
return $self;
}
1;

#########################
#From the file room_t.pm#
#########################
package room_t;
use strict;
use exit_t;

sub new()
{
my $self = {};
my $class = shift;

$self->{NAME} = shift;
$self->{NUMBER}= shift;
@{$self->{EXITS}} = [];

return bless($self, $class);
}

sub addExit()
{
my $self = shift;
push(@{$self->{EXITS}}, exit_t->new(shift, shift));
}

1;

#######################
#From the file game.pl#
#######################

#!/usr/bin/perl

use warnings;
use strict;
use room_t;

package main;

sub buildWorld(); Why?

my @world = [];

This is where the problem lies. Don't do that.
$world[0] is now undef.. later, when you push( @world..)
you're pushing into [1].. not [0].

my @world;
my $room = undef;

my $room;
buildWorld();
my $loop = 1;

while( $loop != 0 ){
printf("> ");

perldoc -f print
my $cmd = <STDIN>;
}


sub buildWorld()
{
my $temp = room_t->new("Start", 0);
push(@world, $temp);
$world[0]->addExit("End", 1);
$temp = room_t->new("End", 1);
push(@world, $temp);

$room = \$world[0]; Why return a reference to it?
}

1;
hu?
 
T

Tad J McClellan

RyanMcCoskrie said:
my @world = [];


This does not do what you think it does.

After that code, @world contains one element (an anon array ref).

I think you expected @world to contain zero elements...

You want simply:

my @world;

there.

my $temp = room_t->new("Start", 0);
push(@world, $temp);


Now @world has 2 elements in it.

$world[0]->addExit("End", 1);


But you access the 1st element from way up in the code.

The value from $temp is in $world[1]
 
S

sln

Quoth (e-mail address removed):
I'm trying to piece to gether a adventure like[1] game.

This is something that I have copied almost straight from the man
page:
package room_t;
use exit_t;

[snip the stuff he has no idea of what your talking about]

Fairly complex verbage for a beginner wouldn't you say.
The question is, what are YOU trying to prove?

-sln
 
R

RyanMcCoskrie

J. Gleixner said:
RyanMcCoskrie said:
Tad said:
This is something that I have copied almost straight from the man page

As is usual with programming, the devil is in the details.
[ snip code ]

Can't call method "addExit" on unblessed reference at ./game.pl line
22.
Have I done something wrong?
Not as far as we can tell, but the "almost" is worrisome.
When I said almost I mean that I changed some names. I was working from
the example of the person class in the perltoot man page. The constructor
subroutine is near identical.
This short and complete example that you can run (as is suggested
in the Posting Guidelines that are posted here frequently) works
just fine.
[snip code out]
The problem is in some of the code that we have not seen...
Okay then. I've tried making what I have produced closer to the example
and it failed.

So your first thought is to post the code so someone else
can fix it for you?
All of this code is Copyright of my self (Ryan McCoskrie) 2009 and is
under the GPL (available on the Free Software Foundation website
www.fsf.org).

That made me laugh so hard my spleen fell out... I don't think you
have much to worry about.
Note that I found a description for the type of programmer that I am.
A rabid prototyper.

rabid : affected with rabies.

Maybe that explains why your code is so poor. You might
want to get that checked.

There are other incarnations of this program with
other parts implemented and their own sets of mysterious bugs.

blah.. blah..
This is the only one in perl though.

oh.. I'm pretty sure there are more..
#########################
#From the file exit_t.pm#
#########################
package exit_t;
use strict;
sub new()
{
my $self = {};
my $class = shift;

Typically, you'd order it so the parameters are set
first, then define $self.
my $self->{NAME} = shift;
my $self->{NUMBER} = shift;

Really?? Well. I guess that's 'near identical'....
bless ($self, $class);
return $self;
}
1;

#########################
#From the file room_t.pm#
#########################
package room_t;
use strict;
use exit_t;

sub new()
{
my $self = {};
my $class = shift;

$self->{NAME} = shift;
$self->{NUMBER}= shift;
@{$self->{EXITS}} = [];

return bless($self, $class);
}

sub addExit()
{
my $self = shift;
push(@{$self->{EXITS}}, exit_t->new(shift, shift));
}

1;

#######################
#From the file game.pl#
#######################

#!/usr/bin/perl

use warnings;
use strict;
use room_t;

package main;

sub buildWorld(); Why?

my @world = [];

This is where the problem lies. Don't do that.
$world[0] is now undef.. later, when you push( @world..)
you're pushing into [1].. not [0].

my @world;
my $room = undef;

my $room;
buildWorld();
my $loop = 1;

while( $loop != 0 ){
printf("> ");

perldoc -f print
my $cmd = <STDIN>;
}


sub buildWorld()
{
my $temp = room_t->new("Start", 0);
push(@world, $temp);
$world[0]->addExit("End", 1);
$temp = room_t->new("End", 1);
push(@world, $temp);

$room = \$world[0]; Why return a reference to it?
}

1;
hu?

I know, I'm terrible.
The thing is that I'm probably the most literal person you'll ever meet face
to face or on the net.
There is almost nothing but literal language processing going on in my head.
Bad at multitasking by male standards, Spock without the socio-emotional
insights, can't make sense of depth-perception, bamboozled by three figure
arithmetic kind of a thing.
That's why I like computers despite the fact that I'm useless at doing
anything with them. The best way to deal with the machine is through a text
based interface with a language that doesn't have anything to do with body
language, tone of voice or implications of statements.
 
S

sln

I know, I'm terrible.
The thing is that I'm probably the most literal person you'll ever meet face
to face or on the net.
There is almost nothing but literal language processing going on in my head.
Bad at multitasking by male standards, Spock without the socio-emotional
insights, can't make sense of depth-perception, bamboozled by three figure
arithmetic kind of a thing.
That's why I like computers despite the fact that I'm useless at doing
anything with them. The best way to deal with the machine is through a text
based interface with a language that doesn't have anything to do with body
language, tone of voice or implications of statements.

Toss in the towell, ur done...

-sln
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top