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

comment on

( [id://3333]=superdoc: 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__

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-29 09:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found