module-management http://www.e-learn.cn/tag/module-management zh-hans Is it okay to use modules from within subroutines? http://www.e-learn.cn/topic/2709447 <span>Is it okay to use modules from within subroutines?</span> <span><span lang="" about="/user/112" typeof="schema:Person" property="schema:name" datatype="">回眸只為那壹抹淺笑</span></span> <span>2019-12-21 05:06:45</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Recently I start playing with OO Perl and I've been creating quite a bunch of new objects for a new project that I'm working on. As I'm unfamilliar with any best practice regarding OO Perl and we're kind in a tight rush to get it done :P</p> <p>I'm putting a lot of this kind of code into each of my function:</p> <pre><code>sub funcx{ use ObjectX; # i don't declare this on top of the pm file # but inside the function itself my $obj = new ObjectX; } </code></pre> <p>I was wondering if this will cause any negative impact versus putting on the <code>use Object</code> line on top of the Perl modules outside of any function scope.</p> <p>I was doing this so that I feel it's cleaner in case I need to shift the function around.</p> <p>And the other thing that I have noticed is that when I try to run a test.pl script on the unix server itself which test my objects, it slow as heck. But when the same code are run through CGI which is connected to an apache server, the web page doesn't load as slowly.</p> <br /><h3>回答1:</h3><br /><h2>Where to put use?</h2> <p><code>use</code> occurs at compile time, so it doesn't matter where you put it. At least from a purely pragmatic, 'will it work', point of view. Because it happens at compile time <code>use</code> will always be executed, even if you put it in a conditional. Never do this: <code>if( $foo eq 'foo' ) { use SomeModule }</code></p> <p>In my experience, it is best to put all your use statements at the top of the file. It makes it easy to see what is being loaded and what your dependencies are.</p> <hr /><p><strong>Update:</strong> </p> <p>As brian d foy points out, things compiled before the <code>use</code> statement will not be affected by it. So, the location can matter. For a typical module, location does not matter, however, if it does things that affect compilation (for example it imports functions that have prototypes), the location could matter.</p> <p>Also, Chas Owens points out that it can affect compilation. Modules that are designed to alter compilation are called pragmas. Pragmas are, by convention, given names in all lower-case. These effects apply only within the scope where the module is used. Chas uses the <code>integer</code> pragma as an example in his answer. You can also disable a pragma or module over a limited scope with the keyword <code>no</code>.</p> <pre><code>use strict; use warnings; my $foo; print $foo; # Generates a warning { no warnings 'unitialized`; # turn off warnings for working with uninitialized values. print $foo; # No warning here } print $foo; # Generates a warning </code></pre> <hr /><h2>Indirect object syntax</h2> <p>In your example code you have <code>my $obj = new ObjectX;</code>. This is called indirect object syntax, and it is best avoided as it can lead to obscure bugs. It is better to use this form:</p> <pre><code>my $obj = ObjectX-&gt;new; </code></pre> <h2>Why is your test script slow on the server?</h2> <p>There is no way to tell with the info you have provided.</p> <p>But the easy way to find out is to profile your code and see where the time is being consumed. NYTProf is another popular profiling tool you may want to check out.</p> <h2>Best practices</h2> <p>Check out Perl Best Practices, and the quick reference card. This page has a nice run down of Damian Conway's OOP advice from PBP.</p> <p>Also, you may wish to consider using Moose. If the long script startup time is acceptable in your usage, then Moose is a huge win.</p> <br /><br /><br /><h3>回答2:</h3><br /><h1>question 1</h1> <p>It depends on what the module does. If it has lexical effects, then it will only affect the scope it is used in:</p> <pre><code>my $x; { use integer; $x = 5/2; #$x is now 2 } my $y = 5/2; #$y is now 2.5 </code></pre> <p>If it is a normal module then it makes no difference where you use it, but it is common to use all of those modules at the top of the program.</p> <h1>question 2</h1> <p>Things that can affect the speed of a program between machines</p> <ol><li>speed of the processor</li> <li>version of modules installed (some modules have XS versions that are much faster)</li> <li>version of Perl</li> <li>number of entries in PERL5LIB</li> <li>speed of the drive</li> </ol><br /><br /><br /><h3>回答3:</h3><br /><p>daotoad and Chas. Owens already answered the part of your question pertaining to the position of use statements. Let me remark on something else here:</p> <blockquote> <p>I was doing this so that I feel it's cleaner in case I need to shift the function around.</p> </blockquote> <p>Personally, I find it much cleaner to have all the used modules in one place at the top of the file. You won't have to search for use statements to see what other modules are being used and a quick glance will tell you what is being used and even what is not being used.</p> <p>Regarding your performance problem: with Apache and mod_perl the Perl interpreter will have to parse and compile your used modules only once. The next time the script is run, execution should be much faster. On the command line, however, a second run doesn't get this benefit.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/1457796/is-it-okay-to-use-modules-from-within-subroutines</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/perl" hreflang="zh-hans">perl</a></div> <div class="field--item"><a href="/tag/oop" hreflang="zh-hans">oop</a></div> <div class="field--item"><a href="/tag/module-management" hreflang="zh-hans">module-management</a></div> </div> </div> Fri, 20 Dec 2019 21:06:45 +0000 回眸只為那壹抹淺笑 2709447 at http://www.e-learn.cn ZF2 load service config from module http://www.e-learn.cn/topic/1111702 <span>ZF2 load service config from module</span> <span><span lang="" about="/user/34" typeof="schema:Person" property="schema:name" datatype="">爱⌒轻易说出口</span></span> <span>2019-12-02 15:19:36</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I am still struggling in instantiating a service from a ZF2 module outside of Zend Framework (in a blank .php). </p> <p><strong>I want to achieve:</strong></p> <p>Instantiate + invoke a ZF2 service method from outside ZF by the use of the ServiceManager and possibly DI.</p> <p><strong>What I have now:</strong> (<strong>UPDATED 4/10/2013</strong>)</p> <p>Following up on the comments below I have done more research,particularly:</p> <ul><li>The quick guide<br /> http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html</li> <li>RTD (Databases and models) http:<br /> //zf2.readthedocs.org/en/latest/user-guide/database-and-models.html</li> <li>Modules presentation (Very helpful) http://www.youtube.com/watch?v=Vp7y65rnN98#t=1200</li> <li>Module source on github - https: //github.com/juriansluiman/SlmMail</li> </ul><p>I've opted to trim out all the DI and ModuleManager things and try to autoload (works fine now) and instantiate (does not) a service.</p> <p><strong>1 - Autoload the requested classes using a Classmap and instantiate servicemanager in a stand-alone .PHP file</strong></p> <p> </p><pre><code>// Autoload ZF and ProductImage module via classmap Zend\Loader\AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' =&gt; array( 'autoregister_zf' =&gt; TRUE, ), 'Zend\Loader\ClassMapAutoloader' =&gt; array( '/home/frequency/domains/scrftcdn/public_html/ft/shop/php/zendframework/module/ProductImage/autoload_classmap.php', ) ) ) // Hard-coded servicemanager configuration (will come from $module-&gt;getConfig once this works) $smc = new \Zend\ServiceManager\Config( array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'ProductImage\Model\ProductImage' =&gt; 'ProductImage\Factory\ProductImageFactory', ) ), ) ); // Instantiate the service manager $sm = new \Zend\ServiceManager\ServiceManager($smc); //Load the service via the service manager $service = $sm-&gt;get('ProductImage\Model\ProductImage'); // &lt;throws exception die(); </code></pre> <p><strong>2 - The exception</strong> </p> <pre><code> [error] [client 192.168.6.52] PHP Fatal error: Uncaught exception 'Zend\\ServiceManager\\Exception\\ServiceNotFoundException' with message 'Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for ProductImage\\Model\\ProductImage' in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php:495 Stack trace:\n#0 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/manage-product-images/functions.inc.php(48): Zend\\ServiceManager\\ServiceManager-&gt;get('ProductImage\\Mo...') #1 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.inc.php(14): require_once('/home/frequency...')\n #2 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.php(14): require_once('/home/frequency...')\n #3 /home/frequency/domains/wpfreqad/public_html/wp-settings.php(293): include('/home/frequency...')\n #4 /home/frequency/domains/wpfreqad/public_html/wp-config.php(90): require_once('/home/frequency...')\n #5 /home/frequency/domains/wpfreqad/public_html/wp-load.php(29): require_onc in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 495 </code></pre> <p><strong>3 - ProductImage\autoload_classmap.php</strong></p> <pre><code> &lt;?php // Generated by ZF2's ./bin/classmap_generator.php return array( 'ProductImageTest\Service\ProductImageServiceTest' =&gt; __DIR__ . '/test/ProductImageTest/Service/ProductImageServiceTest.php', 'ProductImage\Module' =&gt; __DIR__ . '/Module.php', 'ProductImage\Factory\ProductImageFactory' =&gt; __DIR__ . '/src/ProductImage/Factory/ProductImageFactory.php', 'ProductImage\Model\ProductImage' =&gt; __DIR__ . '/src/ProductImage/Model/ProductImage.php', ); </code></pre> <p><strong>4 - ProductImage\Module.php</strong></p> <pre><code>class Module implements \Zend\ModuleManager\Feature\ConfigProviderInterface { /* Invoked by Module Manager */ public function getConfig() { return include __DIR__ . '/config/module.config.php'; } } </code></pre> <p><strong>5 - ProductImage\config\module.config.php</strong></p> <pre><code>&lt;?php return array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'ProductImage\Model\ProductImage' =&gt; 'ProductImage\Factory\ProductImageFactory', ), ), ); </code></pre> <p>I hope that's the right approach and not too far off the right way..</p> <br /><h3>回答1:</h3><br /><p>I've finally found a solution. Jurian's hints to using the actual application have put me on the right track! :)</p> <p><strong>1 - /zendframework/config/application.config.php.</strong></p> <p>Everything is default, just make sure the module is added. I commented the 'application' module as I don't see any use for it (as of now). I also had to change the path to the config files from <code>'./module'</code> to <code>__DIR__ . '../module'</code> as it was looking in the wrong directory (took me a while to find that one).</p> <pre><code>&lt;?php return array( // ... 'modules' =&gt; array( 'ProductImage', /* ProductImage module */ // 'Application', ), // ... 'module_listener_options' =&gt; array( 'module_paths' =&gt; array( __DIR__ . '/../module', __DIR__ . '/../vendor', ), </code></pre> <p><strong>2 - configuration</strong></p> <p>make sure the modules are configured right, and also that ZF2 Path is set up correctly. In my case, run through the quick start on RTD (http://zf2.readthedocs.org/en/latest/ref/installation.html). I had the <code>ZF2_PATH</code> exception and change the <code>httpd.conf</code> via WHM.</p> <p><strong>3 - Read more on RTD</strong></p> <p>In particular on how you can bootstrap the application: http://zf2.readthedocs.org/en/latest/modules/zend.mvc.intro.html#zend-mvc-intro</p> <p>Which after very little debugging produced me the following code to access a neatly configured $sm instance.</p> <pre><code>//wherever the ZF2 application skeleton is, include the autoloader require_once '/home/path/to/the/ZF2/application/directory/init_autoloader.php'; use Zend\Loader\AutoloaderFactory; use Zend\Mvc\Application; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\ServiceManager; // setup autoloader AutoloaderFactory::factory(); // get application stack configuration $configuration = include '/home/path/to/the/ZF2/application/directory/config/application.config.php'; //var_export($configuration); // The init() method does something very similar with the previous example. $app = Application::init($configuration); $sm = $app-&gt;getServiceManager(); $pi = $sm-&gt;get('ProductImage\Service\ProductImageService'); var_export($pi); die(); </code></pre> <p>I do not like the fact that the configuration needs to be specified in addition to the init_autoloader path. I avoid this implementation from being copied and pasted all over the place, I am considering integrating the <code>$sm</code> instantiation into the <code>init_autoloader.php</code> in the future so that the path of the configuration file does not have to be specified whenever a <code>ProductImage</code> service needs to be invoked.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/19154300/zf2-load-service-config-from-module</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/php" hreflang="zh-hans">php</a></div> <div class="field--item"><a href="/tag/dependency-injection" hreflang="zh-hans">dependency-injection</a></div> <div class="field--item"><a href="/tag/zend-framework2" hreflang="zh-hans">zend-framework2</a></div> <div class="field--item"><a href="/tag/servicemanager" hreflang="zh-hans">servicemanager</a></div> <div class="field--item"><a href="/tag/module-management" hreflang="zh-hans">module-management</a></div> </div> </div> Mon, 02 Dec 2019 07:19:36 +0000 爱⌒轻易说出口 1111702 at http://www.e-learn.cn ZF2 load service config from module http://www.e-learn.cn/topic/1074612 <span>ZF2 load service config from module</span> <span><span lang="" about="/user/72" typeof="schema:Person" property="schema:name" datatype="">本小妞迷上赌</span></span> <span>2019-12-02 09:25:21</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><div class="alert alert-danger" role="alert"> <p>I am still struggling in instantiating a service from a ZF2 module outside of Zend Framework (in a blank .php). </p> <p><strong>I want to achieve:</strong></p> <p>Instantiate + invoke a ZF2 service method from outside ZF by the use of the ServiceManager and possibly DI.</p> <p><strong>What I have now:</strong> (<strong>UPDATED 4/10/2013</strong>)</p> <p>Following up on the comments below I have done more research,particularly:</p> <ul><li>The quick guide<br /><a href="http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html" rel="nofollow">http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html</a></li> <li>RTD (Databases and models) http:<br /> //zf2.readthedocs.org/en/latest/user-guide/database-and-models.html</li> <li>Modules presentation (Very helpful) <a href="http://www.youtube.com/watch?v=Vp7y65rnN98#t=1200" rel="nofollow">http://www.youtube.com/watch?v=Vp7y65rnN98#t=1200</a></li> <li>Module source on github - https: //github.com/juriansluiman/SlmMail</li> </ul><p>I've opted to trim out all the DI and ModuleManager things and try to autoload (works fine now) and instantiate (does not) a service.</p> <p><strong>1 - Autoload the requested classes using a Classmap and instantiate servicemanager in a stand-alone .PHP file</strong></p> <p> </p><pre><code>// Autoload ZF and ProductImage module via classmap Zend\Loader\AutoloaderFactory::factory(array( 'Zend\Loader\StandardAutoloader' =&gt; array( 'autoregister_zf' =&gt; TRUE, ), 'Zend\Loader\ClassMapAutoloader' =&gt; array( '/home/frequency/domains/scrftcdn/public_html/ft/shop/php/zendframework/module/ProductImage/autoload_classmap.php', ) ) ) // Hard-coded servicemanager configuration (will come from $module-&gt;getConfig once this works) $smc = new \Zend\ServiceManager\Config( array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'ProductImage\Model\ProductImage' =&gt; 'ProductImage\Factory\ProductImageFactory', ) ), ) ); // Instantiate the service manager $sm = new \Zend\ServiceManager\ServiceManager($smc); //Load the service via the service manager $service = $sm-&gt;get('ProductImage\Model\ProductImage'); // &lt;throws exception die(); </code></pre> <p><strong>2 - The exception</strong> </p> <pre><code> [error] [client 192.168.6.52] PHP Fatal error: Uncaught exception 'Zend\\ServiceManager\\Exception\\ServiceNotFoundException' with message 'Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for ProductImage\\Model\\ProductImage' in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php:495 Stack trace:\n#0 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/manage-product-images/functions.inc.php(48): Zend\\ServiceManager\\ServiceManager-&gt;get('ProductImage\\Mo...') #1 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.inc.php(14): require_once('/home/frequency...')\n #2 /home/frequency/domains/wpfreqad/public_html/wp-content/themes/frequency/functions.php(14): require_once('/home/frequency...')\n #3 /home/frequency/domains/wpfreqad/public_html/wp-settings.php(293): include('/home/frequency...')\n #4 /home/frequency/domains/wpfreqad/public_html/wp-config.php(90): require_once('/home/frequency...')\n #5 /home/frequency/domains/wpfreqad/public_html/wp-load.php(29): require_onc in /usr/lib/zendframework/library/Zend/ServiceManager/ServiceManager.php on line 495 </code></pre> <p><strong>3 - ProductImage\autoload_classmap.php</strong></p> <pre><code> &lt;?php // Generated by ZF2's ./bin/classmap_generator.php return array( 'ProductImageTest\Service\ProductImageServiceTest' =&gt; __DIR__ . '/test/ProductImageTest/Service/ProductImageServiceTest.php', 'ProductImage\Module' =&gt; __DIR__ . '/Module.php', 'ProductImage\Factory\ProductImageFactory' =&gt; __DIR__ . '/src/ProductImage/Factory/ProductImageFactory.php', 'ProductImage\Model\ProductImage' =&gt; __DIR__ . '/src/ProductImage/Model/ProductImage.php', ); </code></pre> <p><strong>4 - ProductImage\Module.php</strong></p> <pre><code>class Module implements \Zend\ModuleManager\Feature\ConfigProviderInterface { /* Invoked by Module Manager */ public function getConfig() { return include __DIR__ . '/config/module.config.php'; } } </code></pre> <p><strong>5 - ProductImage\config\module.config.php</strong></p> <pre><code>&lt;?php return array( 'service_manager' =&gt; array( 'factories' =&gt; array( 'ProductImage\Model\ProductImage' =&gt; 'ProductImage\Factory\ProductImageFactory', ), ), ); </code></pre> <p>I hope that's the right approach and not too far off the right way..</p> </div><div class="panel panel-info"><div class="panel-heading"></div><div class="panel-body"> <p>I've finally found a solution. Jurian's hints to using the actual application have put me on the right track! :)</p> <p><strong>1 - /zendframework/config/application.config.php.</strong></p> <p>Everything is default, just make sure the module is added. I commented the 'application' module as I don't see any use for it (as of now). I also had to change the path to the config files from <code>'./module'</code> to <code>__DIR__ . '../module'</code> as it was looking in the wrong directory (took me a while to find that one).</p> <pre><code>&lt;?php return array( // ... 'modules' =&gt; array( 'ProductImage', /* ProductImage module */ // 'Application', ), // ... 'module_listener_options' =&gt; array( 'module_paths' =&gt; array( __DIR__ . '/../module', __DIR__ . '/../vendor', ), </code></pre> <p><strong>2 - configuration</strong></p> <p>make sure the modules are configured right, and also that ZF2 Path is set up correctly. In my case, run through the quick start on RTD (<a href="http://zf2.readthedocs.org/en/latest/ref/installation.html" rel="nofollow">http://zf2.readthedocs.org/en/latest/ref/installation.html</a>). I had the <code>ZF2_PATH</code> exception and change the <code>httpd.conf</code> via WHM.</p> <p><strong>3 - Read more on RTD</strong></p> <p>In particular on how you can bootstrap the application: <a href="http://zf2.readthedocs.org/en/latest/modules/zend.mvc.intro.html#zend-mvc-intro" rel="nofollow">http://zf2.readthedocs.org/en/latest/modules/zend.mvc.intro.html#zend-mvc-intro</a></p> <p>Which after very little debugging produced me the following code to access a neatly configured $sm instance.</p> <pre><code>//wherever the ZF2 application skeleton is, include the autoloader require_once '/home/path/to/the/ZF2/application/directory/init_autoloader.php'; use Zend\Loader\AutoloaderFactory; use Zend\Mvc\Application; use Zend\Mvc\Service\ServiceManagerConfig; use Zend\ServiceManager\ServiceManager; // setup autoloader AutoloaderFactory::factory(); // get application stack configuration $configuration = include '/home/path/to/the/ZF2/application/directory/config/application.config.php'; //var_export($configuration); // The init() method does something very similar with the previous example. $app = Application::init($configuration); $sm = $app-&gt;getServiceManager(); $pi = $sm-&gt;get('ProductImage\Service\ProductImageService'); var_export($pi); die(); </code></pre> <p>I do not like the fact that the configuration needs to be specified in addition to the init_autoloader path. I avoid this implementation from being copied and pasted all over the place, I am considering integrating the <code>$sm</code> instantiation into the <code>init_autoloader.php</code> in the future so that the path of the configuration file does not have to be specified whenever a <code>ProductImage</code> service needs to be invoked.</p> </div></div><div class="alert alert-warning" role="alert"><p>来源:<code>https://stackoverflow.com/questions/19154300/zf2-load-service-config-from-module</code></p></div></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/php" hreflang="zh-hans">php</a></div> <div class="field--item"><a href="/tag/dependency-injection" hreflang="zh-hans">dependency-injection</a></div> <div class="field--item"><a href="/tag/zend-framework2" hreflang="zh-hans">zend-framework2</a></div> <div class="field--item"><a href="/tag/servicemanager" hreflang="zh-hans">servicemanager</a></div> <div class="field--item"><a href="/tag/module-management" hreflang="zh-hans">module-management</a></div> </div> </div> Mon, 02 Dec 2019 01:25:21 +0000 本小妞迷上赌 1074612 at http://www.e-learn.cn