Problem with bless

Discussion in 'Perl Misc' started by Martin Kissner, Mar 9, 2006.

  1. 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))}}'
     
    Martin Kissner, Mar 9, 2006
    #1
    1. Advertising

  2. Martin Kissner

    Guest

    Martin Kissner <> 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
     
    , Mar 9, 2006
    #2
    1. Advertising

  3. wrote :
    > Martin Kissner <> 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))}}'
     
    Martin Kissner, Mar 9, 2006
    #3
  4. Martin Kissner

    Paul Lalli Guest

    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
     
    Paul Lalli, Mar 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bwv549
    Replies:
    3
    Views:
    142
    Joel VanderWerf
    Jan 29, 2010
  2. Joey Zhou
    Replies:
    7
    Views:
    113
    John Carter
    Mar 14, 2011
  3. Kim Jonguk

    question about 'bless'

    Kim Jonguk, Sep 8, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    97
    Anno Siegel
    Sep 8, 2003
  4. Michele Dondi
    Replies:
    8
    Views:
    104
    Michele Dondi
    Jul 3, 2004
  5. Martin Kissner

    Problem with bless

    Martin Kissner, Mar 9, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    118
    Martin Kissner
    Mar 9, 2006
Loading...

Share This Page