http://www.perlmonks.org?node_id=741684

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

Hello August Monks and Monkesses!

I've got an app with a subroutine, init_gui(), that is called through an object:

$ui->init_gui

When I add the line:

use subs 'init_gui';

to the Graphical package where init_gui() is defined, my app dies with undefined subroutine &...Graphical::init_gui called in line 895.

A test using an identical object model does not exhibit this behavior, nor does 'use subs' cause every method to fail.

What parts of perl or of my app, should I be examining to better understand what is going on? AFAIK there is no source filtering where I can lay the blame.

Thanks for reading.

Replies are listed 'Best First'.
Re: 'use subs' causes method to vanish
by zwon (Abbot) on Feb 05, 2009 at 20:43 UTC

    use subs rewrites existing symbols. Place use subs before init_gui definition. There's no sense to use subs after function is declared.

    Update: BTW, I don't think you should use subs at all in this application.

      You're right, I don't need (and from this result won't be using) use subs for an object method. I only encountered this because I used a script to use subs for all subs, to be able to begin to get rid of parentheses for non-object subs.

      I can sort out the methods from the subs and solve the problem, and I'd like to understand perl internals better, too. Hence this posting.

        I only encountered this because I used a script to use subs for all subs, to be able to begin to get rid of parentheses for non-object subs.

        As you've seen, that's a bad idea. If you're rigorous about the order in which Perl compiles your code, you can get away with it... but I like to avoid code which relies on a well-defined order of appearance in the compilation process, as it seems fragile.

Re: 'use subs' causes method to vanish
by linuxer (Curate) on Feb 05, 2009 at 20:42 UTC

    Having read the perldoc subs I wonder, if it is intented for usage in OO style. Though I cannot explain what happens internally.