Problem with bless

M

Martin Kissner

Hello together,

I have read perldoc perlboot and tried to practice a little.

Now I have a problem with the (redueced) script below.
When I omit "use strict" i get the output of the final print statement
as expected.
With "use strict" I get the error:
Bareword "Sheep" not allowed while "strict subs" in use at
./sheep.pl line 23.
Execution of ./sheep.pl aborted due to compilation errors.

Line 23 is the second from last line.
I do not really know what the error means and I also do not know how to
change the script to make it work with "use strict".

Can anyone give me some explanation and/or point me to the applicable
section of the docs? perldoc -q "strict subs" was not helpful.

Thanks in advance and
Best regards
Martin

and here comes the code:

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

{ package Animal;
sub name {
my $instance = shift;
$instance->{Name}
}
sub color {
$_[0]->{Color}
}
}
{ package Sheep;
use vars qw(@ISA);
@ISA = qw(Animal);
sub sound { "baaah" };
sub default_color { "white" }
}

my $badsheep = bless { Name => "Evil", Color => "black" }, Sheep;
print "The sheep ",$badsheep->name," is ",$badsheep->color," and says ",
$badsheep->sound, "!\n";
 
X

xhoster

Martin Kissner said:
Can anyone give me some explanation and/or point me to the applicable
section of the docs? perldoc -q "strict subs" was not helpful.

"perldoc strict", search for "bareword". Paricularly note that
"quoted string is always ok" comment.

Xho
 
M

Martin Kissner

"perldoc strict", search for "bareword". Paricularly note that
"quoted string is always ok" comment.

Thank you (also to Paul Lalli who also pointed me to "perldoc strict" in
his post)
That helped.

Best regards
Martin
 
P

Paul Lalli

Martin said:
Now I have a problem with the (redueced) script below.
When I omit "use strict" i get the output of the final print statement
as expected.
With "use strict" I get the error:
Bareword "Sheep" not allowed while "strict subs" in use at
./sheep.pl line 23.
Execution of ./sheep.pl aborted due to compilation errors.

Line 23 is the second from last line.
I do not really know what the error means and I also do not know how to
change the script to make it work with "use strict".
my $badsheep = bless { Name => "Evil", Color => "black" }, Sheep;

Sheep is a bareword. That is, a sequence of
letters/numbers/underscores that is not a string or subroutine. That's
not allowed under strict.

my $badsheep = bless { Name => 'Evil', Color => 'black' }, 'Sheep';
and here comes the code:

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

Don't do that. Fix the error. Do not plug your ears and scream "la la
la I can't hear you!!".
{ package Animal;
sub name {
my $instance = shift;
$instance->{Name}
}
sub color {
$_[0]->{Color}
}
}
{ package Sheep;
use vars qw(@ISA);
@ISA = qw(Animal);
sub sound { "baaah" };
sub default_color { "white" }
}

my $badsheep = bless { Name => "Evil", Color => "black" }, Sheep;
print "The sheep ",$badsheep->name," is ",$badsheep->color," and says ",
$badsheep->sound, "!\n";

Note that this extraordinarily bizarre form. A class file itself
should never just out-right declare an object of the class. Objects
should be created and returned in constructors. The constructor would
get the name of the class, and you would bless into that class.

sub new {
my $class = shift;
my ($name, $color) = @_;
my $bad_sheep = bless { Name => $name, Color => $color }, $class;
print "The sheep $name is $color and says ", $bad_sheep->sound,
"\n";
return $bad_sheep;
}

Paul Lalli
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top