E
edward.nigma
This appears to be an example shown on many diffrent articles for
creating global values available to all packages in a Perl script...
looks good except I have one alternation to it that I am not quite able
to figure out...
This assumes that your packages are being loaded from remote files with
the use command...
BEGIN CODE -->
script.pl:
----------------
use vars qw($q);
use CGI;
use lib qw(.);
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
use My:oc qw($q); # Ditto
$q = new CGI;
My::HTML:rintmyheader();
My/HTML.pm
----------------
package My::HTML;
use strict;
BEGIN {
use Exporter ();
@My::HTML::ISA = qw(Exporter);
@My::HTML::EXPORT = qw();
@My::HTML::EXPORT_OK = qw($q);
}
use vars qw($q);
use My:oc qw($q);
sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q->header();
My:oc:rinttitle('Guide');
}
1;
My/Doc.pm
----------------
package My:oc;
use strict;
BEGIN {
use Exporter ();
@My:oc::ISA = qw(Exporter);
@My:oc::EXPORT = qw();
@My:oc::EXPORT_OK = qw($q);
}
use vars qw($q);
sub printtitle{
my $title = shift || 'None';
print $q->h1($title);
}
1;
END CODE -->
What if your packages are being established within the same file? Such
as the following.
Now you do not have the opportunity to include this line, which I
belive is necessary for this to function properly.
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
BEGIN CODE -->
script.pl
-------
use strict;
$hello = 1;
TESTING->showtest; # output 1
TESTING2->showtest; # output 2
print $hello; # output 3
package TESTING;
use strict;
sub showtest {
print $hello;
}
package TESTING2;
use strict;
sub showtest {
$hello++;
print $hello;
$hello++;
}
END CODE -->
Thank you! This may be a pretty simple solution, but I am new to object
oriented perl.
creating global values available to all packages in a Perl script...
looks good except I have one alternation to it that I am not quite able
to figure out...
This assumes that your packages are being loaded from remote files with
the use command...
BEGIN CODE -->
script.pl:
----------------
use vars qw($q);
use CGI;
use lib qw(.);
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
use My:oc qw($q); # Ditto
$q = new CGI;
My::HTML:rintmyheader();
My/HTML.pm
----------------
package My::HTML;
use strict;
BEGIN {
use Exporter ();
@My::HTML::ISA = qw(Exporter);
@My::HTML::EXPORT = qw();
@My::HTML::EXPORT_OK = qw($q);
}
use vars qw($q);
use My:oc qw($q);
sub printmyheader{
# Whatever you want to do with $q... e.g.
print $q->header();
My:oc:rinttitle('Guide');
}
1;
My/Doc.pm
----------------
package My:oc;
use strict;
BEGIN {
use Exporter ();
@My:oc::ISA = qw(Exporter);
@My:oc::EXPORT = qw();
@My:oc::EXPORT_OK = qw($q);
}
use vars qw($q);
sub printtitle{
my $title = shift || 'None';
print $q->h1($title);
}
1;
END CODE -->
What if your packages are being established within the same file? Such
as the following.
Now you do not have the opportunity to include this line, which I
belive is necessary for this to function properly.
use My::HTML qw($q); # My/HTML.pm is in the same dir as script.pl
BEGIN CODE -->
script.pl
-------
use strict;
$hello = 1;
TESTING->showtest; # output 1
TESTING2->showtest; # output 2
print $hello; # output 3
package TESTING;
use strict;
sub showtest {
print $hello;
}
package TESTING2;
use strict;
sub showtest {
$hello++;
print $hello;
$hello++;
}
END CODE -->
Thank you! This may be a pretty simple solution, but I am new to object
oriented perl.