BernieC has asked for the wisdom of the Perl Monks concerning the following question:

I'm a bit perplexed -- I'm clearly making some totally dumb error, but I can't see it. When I perl-c this code:
use strict ; use warnings ; package PENDULUM ; sub new { shift @_ ; my $pend = {} ; bless $pend, PENDULUM ; return $pend ; }
I get:
perl -c packtest.pl Bareword "PENDULUM" not allowed while "strict subs" in use at packates +t.pl line 9. packtest.pl had compilation errors.
I can't see what I'm doing wrong but it must be something obvious/trivial... sigh

Replies are listed 'Best First'.
Re: Can't bless
by choroba (Cardinal) on Mar 21, 2021 at 15:51 UTC
    The second parameter to bless is a string, not a bareword. Most often, you don't specify the class name directly, but use the first parameter of the constructor:
    sub new { my ($class) = @_; return bless {}, $class }

    That's because when you inherit from the class, the constructor will work for the new class, as well, without a need to write a new one specifying the subclass.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Thanks. the perldoc doesn't really say that clearly.
        It's documented in perlobj to which bless links.

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        Hi

        Its has nothing to do with bless though, strict is protecting you from ambiguous code

        $ splain < 2 perl -c packtest.pl Bareword "PENDULUM" not allowed while "strict subs" in use at packates +t.pl line 9 (#1) (F) With "strict subs" in use, a bareword is only allowed as a subroutine identifier, in curly brackets or to the left of the "=> +" symbol. Perhaps you need to predeclare a subroutine? packtest.pl had compilation errors (#2) (F) The final summary message when a perl -c fails.

        ambiguous code (bareword PENDULUM)

        $ perl -le" print bless {}, PENDULUM; " PENDULUM=HASH(0x3f9adc) $ perl -le" sub PENDULUM { 666 } print bless {}, PENDULUM; " 666=HASH(0x3f9adc)

        See Ill-Advised Uses of Barewords in modern_perl_2016_a4.pdf page 167

Re: Can't bless
by GrandFather (Saint) on Mar 21, 2021 at 20:33 UTC

    choroba helped with the immediate problem, but maybe an example of common usage will help:

    use strict; use warnings; package Pendulum; sub new { my ($class, %params) = @_; # Do interesting parameter validation here my $self = bless \%params, $class; # and other set up here. At this point you can call derived class +methods # as part of setup if you want using $self->methodName(); return $self; } package main; my $pit = Pendulum->new(Hz => 0.5);

    Note in particular Pendulum->new(...) used to call the constructor

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      perlootut is a good start, in addition to the other docs mentioned herein. Actually I take that back, I may be misremembering how well done that was, but it doesn't even show a constructor using basic Perl.