[perl] use Smart::Comments - Bulknews::Subtech - subtech
use Smart::Commentsはいらないと思う - ヒルズで働く@robarioの技ログ
> ã°ãã¼ãã«ãã£ã«ã¿ã¼ã¿ããã«åããã¾ãä»çµã¿ã¯ç¡ãã§ããããã
ã¨ããããã§ãåãã¡ã¤ã«ã«èªåçã«ãuse Smart::Commentsããåãè¾¼ãã°ã§ããããªãã¨æã£ã¦ãã¡ããã¡ããªã¹ã¯ãªãããã§ã£ã¡ããã¦ã¿ãã
å
ã«è¨ã£ã¦ããã¾ããããããªãã¨ããããããªãç´ ç´ã«ãuse Smart::Commentsãããæ¹ãè¯ãã¨æãã¾ããã¿ã¤ãã«ã¯ãã¿ã§ãã
ãuse Smart::Commentsããç¡ãFoo.pm
package Foo; sub run { ### This is Foo... } 1;
ãå¼ã³åºãmain.pl
use Foo; Foo->run;
ãå®è¡ããã¨ãã«ã-MGlobalSmartComments=Fooãæå®ãã¦å®è¡ããã¨
% perl -MGlobalSmartComments=Foo main.pl ### This is Foo...
Foo.pmã§Smart::Commentsãæå¹ã«ãªãã¾ãã
GlobalSmartComments.pmã¯è¦ã¦ã®éããç²¾ä¸æ¯ã®åæï½
perldoc -mlã§ã½ã¼ã¹ã®å ´æãç¹å®ãã¦ã/tmp以ä¸ã«ãuse Smart::Commentsããæ¿å
¥ããªããã³ãã¼ãã¦ãã¾ããã³ãã¼ããå
ã@INCã®å
é ã«å
¥ãã¦ããã®ã§ããuse Smart::Commentsããä»ãã¦ããæ¹ã®ã¢ã¸ã¥ã¼ã«ãèªã¿è¾¼ã¾ããããã«ãªãã¾ãã
- é層æã£ã¦ãªãã®ã§ã::ããå«ãã¢ã¸ã¥ã¼ã«ãåãã¾ãããï¼ãã ã®ææãï¼
- perldocãå¼ã³åºãã¦ããã®ã§é ã
- ãuse Module ...;ãã®å¼æ°ãç¡è¦ããã¦ãã
- UNIXç³»ã³ãã³ãï¼cat,echoï¼ãå¿ è¦ï¼ãã ã®ææãï¼
ãã»ã»ã»æåã½ã¼ã¹ãã£ã«ã¿ã§ä½ã¨ããªãã¨æã£ã¦Filter::Simpleã使ã£ã¦ãã¾ãããã©ãããã¯ç¡çã ããåãã¡ã¤ã«ã«ãuse Smart::Commentsããå¿
è¦ãªãã§ãããããã
Filter::Simple使ããã«GlobalSmartComments::importã§å
¨é¨ããã°ã§ãã¾ãããã¨ãããã¨ã§å¾ã§ã¡ãã£ã¨ãã£ã¦ã¾ãããã£ã¦ã¿ã¾ããâuse Smart::Commentsãã½ã¼ã¹ã«æ¸ããã«æå¹ã«ããæ¹æ³
æ¬å½ã«ãã¿ã®ã¤ãããªã®ã§ãçªã£è¾¼ã¿ã¯ãææããã«ã»ã»ã»
package GlobalSmartComments; use strict; use warnings; use UNIVERSAL::require; use List::Util qw(first); my @modules; my $inc = '/tmp/SCG'; sub import { my $class = shift; @modules = @_; mkdir $inc; unshift @INC, $inc; } use Filter::Simple sub { s{(\s?use\s+([^;]+))}{ my $all = $1; my $args = $2; if (first {$args =~ /$_/} @modules) { 'BEGIN{' . __PACKAGE__ . "::use('$args');}"; } else { $all; } }egmsx; }; sub use { my $module = shift; my $src = `perldoc -ml $module`; chomp $src; my $dest = $inc . '/' . $module . '.pm'; `echo 'use Smart::Comments;\n' | cat - $src > $dest`; package main; $module->use; } 1;