Shawn Corey am Montag, 16. Januar 2006 04.12:
[...]
> > Ok, it would be interesting to look deeper into the mess of different
> > variables all named with the same name $q, exported across the modules,
> > overwritten by several imports...
> >
> > What do you want to achieve with your code? It looks really strange (hm,
> > at least to me).
> >
> > joe
>
> All the variables $q in the packages have been shunted aside into the
> deep, dark bit bucket of oblivion.
>
> As I said before:
>
>    $main::q = \*My::HTML::q;
>    $main::q = \*My::Doc::q;
>
> $My::HTML::q and $My::Doc::q no longer exist; they are aliases to
> $main::q. In the modules, $q no longer exists; it is an alias for
> $main::q. Whenever you say $q in the modules, you really mean $main::q.
> The modules do not import anything; they export any changes to $main::q;
> via the phrase '$q'.

Hi again Shawn,

I have a question concerning the code presented in the OP. I repeat it for 
better overview:

==== Version 1 ====
  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::Doc  qw($q); # Ditto
  $q = new CGI;

  My::HTML::printmyheader();  
  
  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::Doc  qw($q);
  sub printmyheader{
    # Whatever you want to do with $q... e.g.
    print $q->header();
    My::Doc::printtitle('Guide');
  }
  1;  
  
  My/Doc.pm
  ----------------
  package My::Doc;
  use strict;

  BEGIN {
    use Exporter ();
    @My::Doc::ISA         = qw(Exporter);
    @My::Doc::EXPORT      = qw();
    @My::Doc::EXPORT_OK   = qw($q);
  }
  use vars qw($q);
  sub printtitle{
    my $title = shift || 'None';
    print $q->h1($title);
  }
  1;
==== END Version 1====

The code demonstrates the usage of the use vars pragma and the Exporter.

However, my personal feeling ist that in a bigger project it is eventually bad 
style to use globals this way?!?

Do you agree? Or do I  - again - overlook something?

See the equivalent code below as an alternative:

==== Version 2 ====
  script.pl:
  ----------------
  use CGI;
  use lib qw(.); 
  use My::HTML;
  my $q = new CGI;

  My::HTML::printmyheader($q);  
  
  My/HTML.pm
  ----------------
  package My::HTML;
  use strict;

  use My::Doc;
  sub printmyheader{
   my $q=shift;
   ### DO CHECKS HERE
   # Whatever you want to do with $q... e.g.
   print $q->header();
   My::Doc::printtitle($q, 'Guide');
  }
  1;  
  
  My/Doc.pm
  ----------------
  package My::Doc;
  use strict;

  sub printtitle{
    my $q=shift;
    ### DO CHECKS HERE
    my $title = shift || 'None';
    print $q->h1($title);
  }
  1;
==== END Version 2====

This version does the same, is shorter, is easier to understand for some 
people, does not require Exporter, and does not need use'ing My::Doc in the 
main script (it's more modular).

Does this 2nd version lack any features/magic present in the 1st?
(Apart from demonstrating use vars / Exporter of course)

Any comments are very appreciated!

greetings 
joe

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to