Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to break Class::MakeMethods by using the wrong variable name

by beable (Friar)
on Aug 10, 2004 at 04:59 UTC ( #381463=perlmeditation: print w/replies, xml ) Need Help??

I was using Class::MakeMethods and I got a strange error about "Can't use a string as a HASH ref". Here's how to do it:

#!/usr/bin/perl package BreakCmm; use strict; use warnings; use Class::MakeMethods::Standard::Hash ( 'new' => 'new', 'scalar' => 'sleep', ); my $bc = BreakCmm->new; $bc->sleep(5); print "sleep = ", $bc->sleep, "\n"; sleep($bc->sleep); __END__ Output: Can't use string ("5") as a HASH ref while "strict refs" in use at /us +r/local/share/perl/5.8.2/Class/MakeMethods/Standard/Hash.pm line 179.

This error is caused because Class::MakeMethods made a class accessor method called sleep. When I tried to call the Perl builtin function sleep, this accessor method was called, with the time to sleep as the object to call the accessor on, which produced the strange error.

To avoid or fix the error, don't call your scalars by the same name as a builtin function that you intend to call. This could also be a problem with inheritance, so it's probably best to not call any of the object's attributes by the same name as a function which might be called, even if you don't intend doing so yourself. Here's the example program with the sleep attribute renamed to rest:

#!/usr/bin/perl package BreakCmm; use strict; use warnings; use Class::MakeMethods::Standard::Hash ( 'new' => 'new', 'scalar' => 'rest', ); my $bc = BreakCmm->new; $bc->rest(5); print "rest = ", $bc->rest, "\n"; sleep($bc->rest); __END__

Replies are listed 'Best First'.
Re: How to break Class::MakeMethods by using the wrong variable name
by ysth (Canon) on Aug 10, 2004 at 05:33 UTC
    To avoid or fix the error, don't call your scalars by the same name as a builtin function that you intend to call.
    Two alternatives: use the :method attribute on your method. (Class::MakeMethods should probably be doing this for you anyway.) Then non-method calls will call the builtin operator, not the method sub. Or qualify the non-method call with CORE::.
    $ perl -we'sub sleep:method { print "in sleep method" } sleep 5; print + "done"' done $ perl -we'sub sleep { print "in sleep method" } CORE::sleep 5; print +"done"' done
Re: How to break Class::MakeMethods by using the wrong variable name
by gaal (Parson) on Aug 10, 2004 at 05:50 UTC
    You can get to Perl's sleep by calling CORE::sleep within your module. Note that the masking effect you were bitten by was local to your module's namespace; outside of it, the error would not have occurred.
Re: How to break Class::MakeMethods by using the wrong variable name
by simonm (Vicar) on Aug 11, 2004 at 01:52 UTC
    While I'm always pleased to see people making use of my Class::MakeMethods module, do note that this problem isn't particularly specific to that distribution: any time your subroutine names conflict with Perl built-ins there's a small chance of this type of confusion. (As gaal points out, once you switch package scopes this problem goes away.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-03-21 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?