Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Can't bless

by choroba (Cardinal)
on Mar 21, 2021 at 15:51 UTC ( [id://11130037]=note: print w/replies, xml ) Need Help??


in reply to Can't bless

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]

Replies are listed 'Best First'.
Re^2: Can't bless
by BernieC (Pilgrim) on Mar 21, 2021 at 17:45 UTC
    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

        What they meant is that it's not clear from bless's docs that the second argument should be a string. Keep in mind that Perl does accept unquoted identifiers in a number of locations.

        Seeking work! You can reach me at ikegami@adaelis.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130037]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2024-04-19 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found