Perl Error “Not a CODE reference …”

This Perl error has cost me a few hours of my life and is a nice example of trying to find an answer to a given problem in the internet without ever being successful. This investigation has made me read a lot of things I probably never wanted to know ( e.g. about utf8 encoding in Perl ) until I finally figured out the cause of my problem myself.

This error was caused by a simple syntax error and incorrectly coding access to an element in a hash. May be there is not such a thing like a “simple syntax” error in a powerful language like Perl, since even stupid code can have some meaning.

Anyhow, I was writing something like

   1: print OUT $post->("Content");

instead of

   1: print OUT $post->{"Content"};

Note the round brackets vs. the curly brackets !

So, if anyone every bumps into the very same strange error because of this syntax fault he or she hopefully finds my blog posting here pretty soon to fix this without wasting too much time.

Advertisements

31 Responses to “Perl Error “Not a CODE reference …””

  1. chromatic Says:

    If you find an error message like this again, read perldoc perldiag, or add “use diagnostics;” to your code. That’ll give you more information on what the error means. (In this case, it should give you enough information to solve it.)

  2. Jess Robinson Says:

    Hi Axel,

    Did you try looking in “perldoc perldiag” by any chance? (aka http://perldoc.perl.org/perldiag.html). Thats the doc that lists all the possible warnings perl itself will output.

    Jess

  3. amagard Says:

    Good hints, folks, thanks chromatic & Jess !
    The info I found in perldiag.html is certainly right but in this case not very detailed and probably I still would have not discovered right away what was wrong. But anyway, a good source of information for future perl errors like this.
    “use diagnostics” actually delivered more detailed info about the error. Excellent, chromatic, may be the best lesson I learned this week !

  4. MichaelR Says:

    I ran into the same problem today, with the same cause. Didn’t find this until after I’d fixed it.

    But now I know, and have some pain to hammer in the lesson, about `perldoc perldiag`.

    Fair trade.

  5. Kay Says:

    Thank you! This saved me hours of work! You’re great!

  6. Dan H. Says:

    Just wanted to say thanks. Came across this and had made the same typo in my code. Saved me hours of work.

  7. Adam Lounds Says:

    Same message but was bizarrely a result of using Apache2::Reload on a mod_perl setup. Yay for randomness!

  8. Bruce Says:

    Hi, I just had the same error but due to a very different syntax error. To instantiate an object, ObectjPackage->new instead I had ObjectPackage::new->()
    In hind site the problem is obvious. I want to live in hind site.

  9. Eugene Says:

    Got same error due to typo
    index($_, ‘UNIX_TIMESTAMP’ == 0)
    instead of
    index($_, ‘UNIX_TIMESTAMP’) == 0

  10. Sri Says:

    Hi all,

    I had the same error msg and scratched my head for a while. I added “use diagnostics” after reading this page and the error is gone. I didnt fix anything in my code. I’m really worried that its going to comeback to bite me. So, would like comments in case anyone knows other situations that can lead to this error.

    I was getting this error at a completely unrelated line of code compared to where my perl script is executing at that moment. My project involves calling C API from a perl script AND also an embedded perl interpreter in my C library to invoke callbacks defined in the same script. The code from a callback starts executing and Perl throws this error at another completely unrelated part of the code. The same callback code works fine in other locations/scripts.

  11. Mark Says:

    You just save me a few hours, my friend. Many thanks! I ran into this working with MySQL, getting values out from a fetchrow_hashref() call.

  12. 2010 in review « Axel’s Travelog Says:

    […] out your blog ?Planing by the hour in MS ProjectPerl Coding Error: do not overwrite your hash !Perl Error “Not a CODE reference …”Alexander […]

  13. Michael Says:

    Another happy customer- thanks for being at the top of the google search for “not a Code reference”!!

  14. James Says:

    I encountered this problem in a logging routine I was writing. Turns out I didn’t make the same error as above, instead I had the following:

    printf $self->{log_fh} (“[%s] %s\n”, $self->timestamp(), $message);

    This looks fine but Perl interprets it as accessing &$self->{log_fh}->(parameters); . This could possibly be avoided by wrapping the log_fh in brackets, IIRC.

  15. D Says:

    THank you man, you saved me some hours of my life 🙂

  16. Thomas Says:

    Same error with still a little different cause. Using $q from CGI->new I wrote in one place:

    $q->(‘name’) instead of $q->param(‘name’)

    Took me about 15 minutes of wondering and debugging to see it…

  17. Matthias Says:

    “So, if anyone every bumps into the very same strange error because of this syntax fault he or she hopefully finds my blog posting here pretty soon to fix this without wasting too much time.”

    I did, cheers bro!

  18. Ashwin Says:

    I did stumble into your blog after struggling with a syntax error for an hour. I had used () instead of {}. It is so bugging. Your post was helpful 🙂

  19. amagard Says:

    Thanks for all the comments and nice feedback, folks. One root cause to this problem might be that the curly bracket looks pretty similar to the regular bracket, especially if you have a high resolution monitor and your eyes are not as good anymore as they had been 20 years ago. Thus it becomes simply hard to spot the error.
    I am just speaking of myself, of course 😉

  20. Kyla Davis Says:

    Solved my problem today!! Thanks for sharing.

  21. Victor Says:

    You solved my problem too! I love the internet!

  22. rcanzlovar Says:

    another person saved hours of head scratching because, yes, old eyes can sometimes have a hard time telling parens from curly braces. In my case, it was a combination of that and trying to change code from one flavor to another. Thanks…

  23. rcanzlovar Says:

    You’ve saved another person hours of headscratching and googling. Indeed, parens and curly braces can look very similar depending on the monitor, the font, etc. Old eyes indeed….

  24. spice Says:

    Thanks for sharing. And thanks for the feedback with ‘use diagnostics’ – nice how one can always learn new things each single day.

  25. Mimi Says:

    thanks for posting!

  26. Raju Says:

    This helped me a lot to resolve the issue immediately thanks a lot

  27. Morgan Says:

    Thanks for your post. I had the exact same error and you saved me some head scratching.

  28. Sheldon Says:

    Thanks. I knew it was that line, and suspected the parentheses, but being new to perl and seeing the reference on the internet as being parentheses, I used parentheses. Thanks again.

  29. Paul Rogers Says:

    Many thanks bro, saved me hours too!! Something so simple as curly v straight brackets and couldn’t see it for the trees 😉

  30. Hema Says:

    Thanku soo much


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: