Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Right place to put common execute code in App::Cmd

by McA (Priest)
on Dec 10, 2012 at 15:06 UTC ( [id://1008112]=note: print w/replies, xml ) Need Help??


in reply to Re: Right place to put common execute code in App::Cmd
in thread Right place to put common execute code in App::Cmd

Hi tobyink,

thank you for answering that fast. I have to admit that I hoped it. ;-) (a ++ for that)

The first suggestion is not an option for me right now. The second approach is which I'm doing right now. But I wanted to avoid the call of before_execute in every execute method. I really hoped that there is another callback which I haven't found.

Once again. Thank you.

Best regards
McA

Replies are listed 'Best First'.
Re^3: Right place to put common execute code in App::Cmd
by tobyink (Canon) on Dec 10, 2012 at 16:43 UTC

    "I really hoped that there is another callback which I haven't found."

    The relevant code is App::Cmd::execute_command. As you can see, nothing happens between validate_args and execute that you could possibly hook onto.

    One alternative approach could be to subclass App::Cmd, overriding that method with your own implementation that adds extra hooks between validate_args and execute:

    { package My::App::Cmd; use base 'App::Cmd'; sub execute_command { my ($self, $cmd, $opt, @args) = @_; local our $active_cmd = $cmd; $cmd->validate_args($opt, \@args); $cmd->before_execute($opt, \@args); $cmd->execute($opt, \@args); } }

    Then use My::App::Cmd instead of App::Cmd.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found