Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Problem with bless

Reply
Thread Tools

Problem with bless

 
 
Martin Kissner
Guest
Posts: n/a
 
      03-09-2006
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";

--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      03-09-2006
Martin Kissner <(E-Mail Removed)> wrote:

> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      03-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote :
> Martin Kissner <(E-Mail Removed)> wrote:
>
>> 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.


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

Best regards
Martin

--
perl -e '$S=[[73,116,114,115,31,96],[108,109,114,102,99,112],
[29,77,98,111,105,29],[100,93,95,103,97,110]];
for(0..3){for$s(0..5){print(chr($S->[$_]->[$s]+$_+1))}}'
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      03-09-2006
Martin Kissner wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bless roger's gnuplot as official? bwv549 Ruby 3 01-29-2010 07:37 PM
RIP Teddy Kennedy - & God Bless the USA Bucky Breeder Computer Support 2 08-27-2009 06:40 PM
God Bless The Alabama Crimson Tide CtrlĄ/AltĄ/DelĄ Computer Support 18 09-24-2006 11:41 AM
Problem with bless Martin Kissner Perl Misc 3 03-09-2006 08:21 PM
Bless me Father =?ISO-8859-1?Q?Brian_H=B9=A9?= Computer Support 1 05-24-2004 09:44 PM



Advertisments