Gossamer Forum
Quote Reply
Custom.pm problem
I use the following code, but I get error when I call this sub from a global.
Error: Global symbol "$CFG" requires explicit package name at /.../admin/Links/Custom.pm line 37.

Code:
package Links::Custom::SiteHTML;
# ==================================================================
use strict;
use Links qw/$IN $CFG $GLOBALS/;
use GT::Dumper;
print "\$CFG: " . GT::Dumper::Dumper(\$CFG) . "<br>\n\n";


Interesting, as $CFG should be loaded by "use Links".
Did I miss something?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Custom.pm problem In reply to
Because "use Links;" causes Links.pm to be loaded at compile time in a normal script but your global is an anonymous subroutine that is compiled later on. You will need to use:

Code:
require Links;
import Links qw/$IN $CFG $GLOBALS/;
Quote Reply
Re: [Wychwood] Custom.pm problem In reply to
Wychwood,

Good idea, however, I did not get better result.

Here is the whole test case:
Global contains the following:
'menu_left' => Links::Custom::SiteHTML::menu_template(),


Custom.pm:
Code:
package Links::Custom::SiteHTML;
# ==================================================================
use strict;
require Links;
import Links qw/$IN $CFG $GLOBALS $USER/;

use vars qw/$GRAND_TOTAL/;
use GT::Dumper;

print "Content-Type: text/html\n\n";
print "\$CFG: " . GT::Dumper::Dumper(\$CFG) . "<br>\n\n";


sub menu_template {
# --------------------------------------------------------
# This routine determines how the rate failure page will look like.
require Links;
import Links qw/$IN $CFG $GLOBALS $USER/;

use GT::Dumper;
print "\$CFG: " . GT::Dumper::Dumper(\$CFG) . "<br>\n\n";
}

Error:
Quote:
Global symbol "$CFG" requires explicit package name at /admin/Links/Custom.pm line 11.
BEGIN not safe after errors--compilation aborted at /admin/Links/Custom.pm line 20.
Compilation failed in require at /admin/Links.pm line 28.
BEGIN failed--compilation aborted at /admin/Links.pm line 28.
Compilation failed in require at page.cgi line 17.
BEGIN failed--compilation aborted at page.cgi line 17.
Premature end of script headers: page.cgi


Any idea?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Aug 26, 2007, 9:55 AM
Quote Reply
Re: [Wychwood] Custom.pm problem In reply to
I made a working solution, but I don't like this so much.
I had to pass the $IN, $CFG, $GLOBALS, $USER global variables to the called sub, which is not really logical...

Global:
Code:
'menu_left' => 'sub {
use Links::test;
Links::test::menu_template($IN, $CFG, $GLOBALS, $USER);
use GT::Dumper;
print "Content-Type: text/html\n\n";
print "\$CFG: " . GT::Dumper::Dumper(\$CFG) . "<br>\n\n";

}',


Links/test.pm:
Code:
package Links::test;
# ==================================================================
use strict;
use vars qw/$GRAND_TOTAL $IN $CFG/;
use GT::Dumper;


sub menu_template {
# --------------------------------------------------------
# This routine determines how the rate failure page will look like.

($IN, $CFG) = @_;
print "Content-Type: text/html\n\n";
print "\$CFG: " . GT::Dumper::Dumper(\$CFG) . "<br>\n\n";
}

This prints the $CFG correctly, but I still would like to have the globals exported to my module's namespace.


Any idea how to reach this?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Aug 26, 2007, 10:44 AM
Quote Reply
Re: [webmaster33] Custom.pm problem In reply to
One other way would be to fully qualify the package name, eg. $Links::CFG
Quote Reply
Re: [Wychwood] Custom.pm problem In reply to
Wychwood wrote:
One other way would be to fully qualify the package name, eg. $Links::CFG
Thanks, good idea, I will use that way.
However, this works only when called from a global, as $CFG is already loaded there.


But it will not work in the Custom.pm, as "use Links::Custom;" is called in Links.pm before any variables are loaded...
That's still a problem.

I could load the variables by causing repeated code from Links, but that would cause redundant code, which I would like to avoid.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Custom.pm problem In reply to
Why are you putting that code in the Links/Custom.pm anyways?

Adrian
Quote Reply
Re: [brewt] Custom.pm problem In reply to
Because it is suggested by GT, to put customized code there, as it is not overwritten when upgrades are done.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Custom.pm problem In reply to
No, the comment says:
Code:
# Description:
# By default, this file is empty, however it is here to allow installations
# to perform special operations required to make Gossamer Links load.
# For example, some installations might need a 'use lib' line to work
# properly.
If you want to put your own code into modules, create your own packages and stick them in admin/.

Adrian
Quote Reply
Re: [brewt] Custom.pm problem In reply to
brewt wrote:
If you want to put your own code into modules, create your own packages and stick them in admin/.

That's the other way I tried, and that's what I currently use. I created a W33 directory in Links, and using the Links::W33::Mymodule.


What do you mean by "stick them in admin"?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Custom.pm problem In reply to
I think Adrian means under admin folder just like current GLinks structure, so you can use W33::Mymodule::myfunc(); instead of Links::W33::Mymodule::myfunc();

Cheers,
Boris

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