Re: about the var's scope
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
Re: about the var's scope
I think the only difference between the two is Stat's code do the things of sharing vars across modules really. Under mod_perl,the situation is very different from common CGI environment,and the vars sharing sometimes is useful and needed. I hope I'm correct.If not,the criticism are welcome. -Original Message- From: John Doe [EMAIL PROTECTED] Sent: Jan 16, 2006 4:09 AM To: beginners@perl.org Subject: Re: about the var's scope 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 -- http://home.earthlink.net/~pangj/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
Jeff Pang wrote: I think the only difference between the two is Stat's code do the things of sharing vars across modules really. Under mod_perl,the situation is very different from common CGI environment,and the vars sharing sometimes is useful and needed. I hope I'm correct.If not,the criticism are welcome. The OP was asking about understanding the code, not what are the best practices. First of all, I would never call an object by a single letter. Something like $CGI_Obj would be better. And being a global, it starts with a capital. Yes, I would be more inclined to pass the object to the subroutines than use it as a global. I would use Exporter only to export subroutines, not variables because, as discussed, it is difficult to understand what is happening. I don't use mod_perl but my understanding it that it doesn't, or didn't, reset the globals to the default state (which is undef). Of course, setting all your variables to a known state before using them is consider good practice; so you should never encounter this problem ;) -- Just my 0.0002 million dollars worth, --- Shawn Probability is now one. Any problems that are left are your own. SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_ * Perl tutorials at http://perlmonks.org/?node=Tutorials * A searchable perldoc is available at http://perldoc.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
about the var's scope
Hello,lists, Seeing these code below please.I can't know why the var defined in the main script can been accessed in the modules that used by the main script?thanks. 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; -- http://home.earthlink.net/~pangj/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
Jeff Pang wrote: Hello,lists, Seeing these code below please.I can't know why the var defined in the main script can been accessed in the modules that used by the main script?thanks. From `perldoc vars`: While the vars pragma cannot duplicate the effect of package lexicals (total transparency outside of the package), it can act as an acceptable substitute by pre-declaring global symbols, ensuring their availability to the later-loaded routines. This means any variable declared by vars can be accessed outside of the file it is declared in. The second part of the understanding comes from Exporter. When it exports a thingy, it uses a typeglob to make the thingy in the package the same as in the 'use'ing module. In other words, for your My::HTML module, it does this: $main::q = \*My::HTML::q; For more details, see: perldoc vars perldoc -f our perldoc perlfunc (and search for 'our') perldoc Exporter perldoc perlop (and search for 'typeglob') perldoc perlsub (and search for 'typeglob') -- Just my 0.0002 million dollars worth, --- Shawn Probability is now one. Any problems that are left are your own. SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_ * Perl tutorials at http://perlmonks.org/?node=Tutorials * A searchable perldoc is available at http://perldoc.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. -Original Message- From: Shawn Corey [EMAIL PROTECTED] Sent: Jan 15, 2006 10:58 PM To: beginners@perl.org Subject: Re: about the var's scope Jeff Pang wrote: Hello,lists, Seeing these code below please.I can't know why the var defined in the main script can been accessed in the modules that used by the main script?thanks. From `perldoc vars`: While the vars pragma cannot duplicate the effect of package lexicals (total transparency outside of the package), it can act as an acceptable substitute by pre-declaring global symbols, ensuring their availability to the later-loaded routines. This means any variable declared by vars can be accessed outside of the file it is declared in. The second part of the understanding comes from Exporter. When it exports a thingy, it uses a typeglob to make the thingy in the package the same as in the 'use'ing module. In other words, for your My::HTML module, it does this: $main::q = \*My::HTML::q; For more details, see: perldoc vars perldoc -f our perldoc perlfunc (and search for 'our') perldoc Exporter perldoc perlop (and search for 'typeglob') perldoc perlsub (and search for 'typeglob') -- Just my 0.0002 million dollars worth, --- Shawn Probability is now one. Any problems that are left are your own. SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_ * Perl tutorials at http://perlmonks.org/?node=Tutorials * A searchable perldoc is available at http://perldoc.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- http://home.earthlink.net/~pangj/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
[reordered to bottom style posting] -Original Message- From: Shawn Corey [EMAIL PROTECTED] Sent: Jan 15, 2006 10:58 PM To: beginners@perl.org Subject: Re: about the var's scope Jeff Pang wrote: Hello,lists, Seeing these code below please.I can't know why the var defined in the main script can been accessed in the modules that used by the main script?thanks. From `perldoc vars`: While the vars pragma cannot duplicate the effect of package lexicals (total transparency outside of the package), it can act as an acceptable substitute by pre-declaring global symbols, ensuring their availability to the later-loaded routines. This means any variable declared by vars can be accessed outside of the file it is declared in. The second part of the understanding comes from Exporter. When it exports a thingy, it uses a typeglob to make the thingy in the package the same as in the 'use'ing module. In other words, for your My::HTML module, it does this: $main::q = \*My::HTML::q; For more details, see: perldoc vars perldoc -f our perldoc perlfunc (and search for 'our') perldoc Exporter perldoc perlop (and search for 'typeglob') perldoc perlsub (and search for 'typeglob') Just my 0.0002 million dollars worth, --- Shawn [...] Jeff Pang am Montag, 16. Januar 2006 01.59: Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
John, These codes are written by famous mod_perl expert Stas Berkman,do you know it? You can find them at: http://www.perl.com/pub/a/2002/04/23/mod_perl.html?page=1 maybe you don't know them,but it don't mean that it is impossible.Hm... -Original Message- From: John Doe [EMAIL PROTECTED] Sent: Jan 16, 2006 9:48 AM To: beginners@perl.org Subject: Re: about the var's scope [reordered to bottom style posting] -Original Message- From: Shawn Corey [EMAIL PROTECTED] Sent: Jan 15, 2006 10:58 PM To: beginners@perl.org Subject: Re: about the var's scope Jeff Pang wrote: Hello,lists, Seeing these code below please.I can't know why the var defined in the main script can been accessed in the modules that used by the main script?thanks. From `perldoc vars`: While the vars pragma cannot duplicate the effect of package lexicals (total transparency outside of the package), it can act as an acceptable substitute by pre-declaring global symbols, ensuring their availability to the later-loaded routines. This means any variable declared by vars can be accessed outside of the file it is declared in. The second part of the understanding comes from Exporter. When it exports a thingy, it uses a typeglob to make the thingy in the package the same as in the 'use'ing module. In other words, for your My::HTML module, it does this: $main::q = \*My::HTML::q; For more details, see: perldoc vars perldoc -f our perldoc perlfunc (and search for 'our') perldoc Exporter perldoc perlop (and search for 'typeglob') perldoc perlsub (and search for 'typeglob') Just my 0.0002 million dollars worth, --- Shawn [...] Jeff Pang am Montag, 16. Januar 2006 01.59: Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. 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 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- http://home.earthlink.net/~pangj/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
John Doe wrote: [reordered to bottom style posting] Jeff Pang am Montag, 16. Januar 2006 01.59: Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. 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'. -- Just my 0.0002 million dollars worth, --- Shawn Probability is now one. Any problems that are left are your own. SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_ * Perl tutorials at http://perlmonks.org/?node=Tutorials * A searchable perldoc is available at http://perldoc.perl.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
Shawn Corey am Montag, 16. Januar 2006 04.12: John Doe wrote: [reordered to bottom style posting] Jeff Pang am Montag, 16. Januar 2006 01.59: Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. 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. Shawn, Jeff, seems that it couldn't be more embarassing/awqward for me... (yes, I know who Stas Bekman is, Jeff) On the other side, I'm very happy having demonstrated the mess in my brain with the last post, since, at last, I begin to realize now something that I had the feeling not to fully understand during several years (sic!). I fear the result was an unnecessary amount of complexity in a lot of my code that could have been avoided. Have to check that in detail. 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'. Part of my confusion was (I think) that I didn't realize that, with $main::q = \*My::HTML::q, I can not only influence the main script by a module, but also in the other direction! (as the term 'alias' says...). Now, I see the code in the OP with different eyes! Thanks a *lot* to you two for giving me the chance to understand the use vars / Exporter issue better! Arggg... Have a nice time joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: about the var's scope
That's a good end for us all.Thanks for Shawn and John too. -Original Message- From: John Doe [EMAIL PROTECTED] Sent: Jan 16, 2006 2:09 PM To: beginners@perl.org Subject: Re: about the var's scope Shawn Corey am Montag, 16. Januar 2006 04.12: John Doe wrote: [reordered to bottom style posting] Jeff Pang am Montag, 16. Januar 2006 01.59: Thanks for Shawn.The main script can see the global var $q coming from module,since the main script import this symbol via 'use My::HTML qw($q)'.But the modules have no any importing behavior,why they can see the global var $q coming from main script?I'm really confused for that. 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. Shawn, Jeff, seems that it couldn't be more embarassing/awqward for me... (yes, I know who Stas Bekman is, Jeff) On the other side, I'm very happy having demonstrated the mess in my brain with the last post, since, at last, I begin to realize now something that I had the feeling not to fully understand during several years (sic!). I fear the result was an unnecessary amount of complexity in a lot of my code that could have been avoided. Have to check that in detail. 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'. Part of my confusion was (I think) that I didn't realize that, with $main::q = \*My::HTML::q, I can not only influence the main script by a module, but also in the other direction! (as the term 'alias' says...). Now, I see the code in the OP with different eyes! Thanks a *lot* to you two for giving me the chance to understand the use vars / Exporter issue better! Arggg... Have a nice time joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- http://home.earthlink.net/~pangj/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response