Gossamer Forum
Home : Products : Gossamer Links : Discussions :

sub init Plugin hook and $DB value seems not to work

Quote Reply
sub init Plugin hook and $DB value seems not to work
Hi,

I am stuck with a little problem. I wanted to load some values with a Plugin at the beginnig of every call.
So I use a POST Plugin hook with init. As far as I can see $DB is defined in Links.pm -> sub init

Code:
sub init {
# --------------------------------------------------------------------
# This function initializes Gossamer Links, and must be called before any other
# action is performed.
#
my $path = shift || '.';

# If under mod_perl, reset our environments.
if (PERSIST) {
GT::SQL->reset_env if $INC{'GT/SQL.pm'};
GT::CGI->reset_env();
}

# Get our config object.
$CFG = Links::Config->new($path);
my $debug = $CFG->{debug_level} || $DEBUG;

# Set our tmp directory to store files.
$ENV{GT_TMPDIR} = $CFG->{admin_root_path} . '/tmp';
$IN = GT::CGI->new();
$PLG = GT::Plugins->new(directory => "$CFG->{admin_root_path}/Plugins", debug => $debug);

if ($DB and not ref $DB eq 'GT::Delay') { # $DB is already a GT::SQL object
$DB = GT::SQL->new({
def_path => $CFG->{admin_root_path} . '/defs',
cache => 1,
debug => $debug
});
}
else {
$DB = GT::Delay(
'GT::SQL', 'HASH',
{
def_path => $CFG->{admin_root_path} . '/defs',
cache => 1,
debug => $debug
}
);
}

(%STASH, $GLOBALS, $LANGUAGE, $USER) = ();

$PLG->dispatch('init', sub {});
}

The last two lines were switched to make %STASH available.

I have some code to check for the $DB value in my Plugin script:

Code:
sub init {
my (@args) = @_;
use Data::Dumper;
print $IN->header();
print Dumper($DB);

$DB is empty:
Code:
$VAR1 = bless( {}, 'GT::Delay' );

It would be nice to get some thoughts on how to approach this issue.

Thanks

n||i||k||o
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Hi,

So you are trying to add a hook onto the "init" function? You probably need to run as a POST if I'm understanding you correctly - otherwise it won't have been initialized.

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] sub init Plugin hook and $DB value seems not to work In reply to
Hi Andy,

Thanks for you reply. POST is what I did. STASH and everything works fine, the $DB is missing and I am a little stuck here.

Cheers

n||i||k||o
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Mmm not sure how much help I can be. I know I had issues trying to create my own global functions in here for one of my sites. Everything else was created fine, but even after exporting them they still didn't exist / I got errors in my modules. Was annoying to say the least!

Sorry I can't be more help Angelic

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Hi,

How do you mean $DB is empty while it's clearly not!

el noe wrote:
Code:
$VAR1 = bless( {}, 'GT::Delay' );

What you get looks like normal GT::Delay object to me.

Cheers,
Boris

Facebook, Twitter and Google+ Auth for GLinks and GCommunity | reCAPTCHA for GLinks | Free GLinks Plugins

Last edited by:

eupos: Aug 13, 2014, 7:30 AM
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
HI,

Have you setup database and glinks' tables? And is it working with glinks' normal functions?

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [eupos] sub init Plugin hook and $DB value seems not to work In reply to
Thanks for all answers,

@tandat: yes, everything is up and running

@eupos: you are right, I did not really look at GT::Delay yet. It just seemed wrong to me that I could not see the appropriate content in my opinion by printing Dumper($DB).

Does anyone have more experience with GT::Delay?

I access two core Databases from every Links installation e.g. with:

Code:
my $supplierDB = GT::SQL->new({
def_path => '/path/to/cgi-bin/supplier/admin/defs',
cache => 1
});

Would it be better to use the following instead:

Code:
if ($supplierDB and not ref $supplierDB eq 'GT::Delay') { # $supplierDB is already a GT::SQL object
$supplierDB = GT::SQL->new({
def_path => '/path/to/cgi-bin/supplier/admin/defs',
cache => 1,
debug => $debug
});
}
else {
$supplierDB = GT::Delay(
'GT::SQL', 'HASH',
{
def_path => '/path/to/cgi-bin/supplier/admin/defs',
cache => 1,
debug => $debug
}
);
}

I would appreciate any comments on GT::Delay

Cheers

n||i||k||o

Last edited by:

el noe: Aug 13, 2014, 11:19 PM
Quote Reply
Re: [Andy] sub init Plugin hook and $DB value seems not to work In reply to
Andy wrote:
Mmm not sure how much help I can be. I know I had issues trying to create my own global functions in here for one of my sites. Everything else was created fine, but even after exporting them they still didn't exist / I got errors in my modules. Was annoying to say the least!

Hi Andy,

I guess you have seen it for you participated in this post but just in case that not: I think that is the hint to the problem you mentioned:

http://www.gossamer-threads.com/...ere_P310647/#p310647

Cheers

n||i||k||o

Last edited by:

el noe: Aug 13, 2014, 11:20 PM
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Hi,

Yeah, the problem I had was actually different. Basically, if you have a script that hasn't done any kind of query (count, select, etc), and then try and setup a GT::SQL::Condition->new(), you get an error. According to GT, the only way around it - is to do a very small/simple SQL count(), so that it gets initalized correctly (this only really affects scripts where you don't do Links::user_init(), as that obviously does its own queries)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] sub init Plugin hook and $DB value seems not to work In reply to
Hi Andy,

I will keep that in mind, I guess I had that problem before without knowing, so I started with GT::SQL->new.
Will keep that in mind.

Cheers

n||i||k||o
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Yeah, I spent hours digging into the module, to try and debug why it wasn't working. Luckily, Adrian came to my rescue Angelic

Cheers

Andy

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [el noe] sub init Plugin hook and $DB value seems not to work In reply to
Hi niko,

What I meant about $DB is since you are trying to dump the object immediately after it's initialisation the result you are getting is perfectly normal. I just assumed you took a peek in GT::Delay. It's fairly simple module if you look at it, but anyway here is the description from the module itself.

Quote:
This module provides a simple way to handle delayed module loading in a
fairly generic way. Your object will only be a very lightweight
GT::Delay object until you call a method on it, at which point the
desired module will be loaded, your object will be changed into an
object of the desired type.

Hope this explains now the result you are getting and how this module works.

Cheers,
Boris

Facebook, Twitter and Google+ Auth for GLinks and GCommunity | reCAPTCHA for GLinks | Free GLinks Plugins