Gossamer Forum
Quote Reply
Hook Arguments
Could someone explain to me in very simple terms how to access tag values out of the hook @args?

For example:

I want to retreive the CategoryID as a scaler (integer), which is avaliable in a lot of templates, including browser_info.html.

my (@args) = @_; <- all hooks seem to have this in as default, why not $args???

my $args = shift;

my $cat_id = $args->{CategoryID}; ???

No matter what I try, including $tags = shift; I get an error about value not being a hashref and equalling "1".Crazy


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
Not all arguments contain a hashref....you have to play about to see what you get...also changing the hooks from pre/post, first/last can affect the args. First thing I normally do is print out the args to see what I have.
Quote Reply
Re: [Paul] Hook Arguments In reply to
Thanks Paul.... it seems that not all hooks should have @args, but rather something else.Crazy

I will print @args in my hook and have a look!



Edit: well, the sum total of the args for modify_category is: 1

Frown


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 4, 2002, 1:32 PM
Quote Reply
Re: [Ian] Hook Arguments In reply to
>>it seems that not all hooks should have @args, but rather something else.<<

Thats what I first though but Alex explained that some hooks have multiple arguments so using:

my @args = @_;

....covers them all whereas:

my $hashref = shift;

....may be skipping the 2nd arg onwards.

You can still obviously access the hashref using the first method with:

%{$args[0]}

...or for specific keys:

$args[0]->{key}

Last edited by:

Paul: Jun 4, 2002, 1:32 PM
Quote Reply
Re: [Paul] Hook Arguments In reply to
Quote:
%{$args[0]}




That's a little piece of magic that might help!



Edit: so $args[0]->{CategoryID} would return the value for categoryID if it existed in the peice of code that called it? I think am still a little confused.

Edit 2: when I look through the hooks.cfg, I notice that this particular hook that is troubling me takes a HASH in, and a SCALER out. Is this something that would affect how I find my value (CategoryID in this case) out of @args?

Quote:
'modify_category' => [
{
'subroutine' => 'Links::Category::modify',
'hook_name' => 'modify_category',
'filename' => '(eval 4)',
'input_params' => [
'HASH'
],
'output_params' => [
'SCALAR'
],
'description' => '',
'line' => '8',
'path' => './Plugins'
}


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 4, 2002, 1:44 PM
Quote Reply
Re: [Ian] Hook Arguments In reply to
If you set a PRE hook on modify_category, you will get a hash passed to your function, so you can do

my $tags = shift;

As for the id of the category, you would get it with

$tags->{ID}

because the name of the field is ID and not CategoryID.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Hook Arguments In reply to
Thanks IvanSmile I am going to send you a pm, in a sec....


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
Just a quick followup on how argument parsing and hooks work in our plugin system:

In any of our products, we can wrap a subroutine call with GT::Plugin::dispatch function. By doing this, it makes end users able to do one of the following:

1. Run some custom code before the subroutine is called
2. Change the input arguments to the subroutine
3. Override the subroutine (i.e. don't run it)
4. Run some custom code after the subroutine.
5. Change the output of a subroutine

So wherever you see:

my $results = GT::Plugins->dispatch($CFG->{admin_root_path} . '/Plugins', 'hook_name', \&subroutine, $arg1, $arg2);

It means that it is going to run subroutine() function with $args as arguments. You are able to run code before or after it by placing a hook on hook_name.

For a PRE hook, you are passed in whatever the subroutine() was going to take, in the example above, you'd get $arg1, and $arg2. Your PRE hook can modify these or leave them alone. If you don't override the main function, it must return the same type of arguments so that subroutine() can run as normal (as the return of your hook is passed into subroutine()).

For a POST hook, you are passed in whatever subroutine() returned. You must take care to return something of the same type, as your hooks return value will be placed in $results.

Hope that helps clear up the logic a bit. If you can wrap your head around this, it makes creating hooks much easier. You can scan the code for GT::Plugins->dispatch() and see what exactly you are getting passed in, and the hook name, and what you need to return.

Cheers,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Jun 5, 2002, 10:40 AM
Quote Reply
Re: [Alex] Hook Arguments In reply to
Thank you very much Alex!

Let me ponder on this for bitSmile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Hook Arguments In reply to
So, lets say for example I want to trap the Link URL and Category the Links was in when it is deleted.

I would Need to know this information BEFORE it is actually removed, but not before the admin has a chance to change their mind and not delete it. The question here then is this a PRE or a POST hook requirement. I am going to assume PRE, as once post happens, you cannot access this information.

I then should find the code in a links moduale where the delete_category sub calls the PRE hook in link.pm...

sub delete {
# -------------------------------------------------------------------
# Deletes a link, but passes through the plugin system.
#
my ($self, $id) = @_;
$id or return $self->error ("BADARGS", "FATAL", "Usage: \$cat->delete( id_number ).");
ref $id or ($id = { ID => $id });

GT::Plugins->dispatch ( $CFG->{admin_root_path} . '/Plugins', 'delete_link', sub { return $self->_plg_delete (@_); }, $id );
}

In here I see $id is returned from the plug_in, and id is passed in. (link id I am assuming). I can grab the associated category id easily from the catlinks table so that is not a problem.

Is this the correct method for finding out the arguments for a hook then?

And to access this $id in a PRE hook (or a POST hook for that matter), what is the correct syntax? (i am still a little Perl-Newbie),

my $id = shift; ??

and if there were multiple arguments, I would have to access them in the order they were presented to the hook sub above like: my $arg1 = (@args[0]) and my $arg2 = (@args[1]) or somemthing? I need help with the extraction of the $args out of @args .

Thanks again.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
Hi,

Correct, you would use a PRE hook to capture the link just before it was deleted. The argument in is the link's id number, so your PRE hook would look like:

sub my_pre_hook {
my $id = shift;
... # do whatever you need to do here
return $id; # Must return the $id!
}

Make sure to return the $id, as the Links SQL delete will take whatever you return and delete that. So if you wanted a plugin hook that would delete the wrong link (don't ask me why), you could do:

sub my_pre_hook {
my $id = shift;
return $id + 1;
}

This would have the affect of when you try to delete link 15, it would actually delete link 16. =)

If you have multiple arguments in a PRE hook, use:

my ($arg1, $arg2) = @_;

and be sure to return them at the end:

return ($arg1, $arg2);

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Hook Arguments In reply to
Alex, your post just cemented the concept in!!! Thank you very much for that.

Watch out, I am even more plug-in dangerous nowTongue.


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Hook Arguments In reply to
Sorry, one last tiny questions:

If I have a POST hook for say delete_link already in my plugin.pm, and I am now adding a PRE hook for the same (delete_link), the wizard creates only one sub of the same name "sub delete_link {". Is this right?

How do I seperate/differentiate my PRE stuff from my POST stuff, if they are in the same sub??


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
The wizard will create a hook based on what you enter in the code section. You probably want to name your functions

Plugins::YourPlugin::pre_delete_link

Plugins::YourPlugin::post_delete_link

That will help clear up the confusion. You need a separate sub for a pre and a post hook.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Hook Arguments In reply to
Many thanks again Alex, that is very clear now.Smile


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
Oh, one other tip, due to some lack of forsight on our part, all other products are prefixed:

Plugins::Product::YourPlugin::function

so if you were doing a Gossamer Forum plugin, it would be:

Plugins::GForum::YourPlugin::function

Links SQL for historical reasons didn't get prefixed. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Hook Arguments In reply to
Ok,

So for now at least, I should leave out the ::LinksSQL:: . Thanks for the tip!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Alex] Hook Arguments In reply to
"historical reasons"...hehe....I like it :)
Quote Reply
Re: [Paul] Hook Arguments In reply to
I am not sure if your question mark was that bad. The beer swilling homer makes me thirsty everytime I read your posts!


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Ian] Hook Arguments In reply to
My favorite characters are Mr Burns and Chief Wiggum so you may just see one of them appear soon, I just haven't found a good image yet :)
Quote Reply
Re: [Paul] Hook Arguments In reply to
Mine would have to be the scottish gardener.... "Willy".


http://www.iuni.com/...tware/web/index.html
Links Plugins
Quote Reply
Re: [Paul] Hook Arguments In reply to
Why would $id be returning this: HASH(0x8511258)

my $id = shift; In the PRE delte_link hook?

Is this a perl variable type thing, that I have to change?

Edit: Like my $link_id = $id->{ID}; ??

Edit2: Yes, that seemed to workSmile Just ignore this post!


http://www.iuni.com/...tware/web/index.html
Links Plugins

Last edited by:

Ian: Jun 5, 2002, 3:00 PM
Quote Reply
Re: [Ian] Hook Arguments In reply to
That means the first argument is a hashref

If you do:

my $id = shift;

...then $id contains all the keys/values. To see them do:

print %$id;
Quote Reply
Re: [Paul] Hook Arguments In reply to
Most excellent, I was wondering that too!

Thanks Paul.


http://www.iuni.com/...tware/web/index.html
Links Plugins