2008年3月28日金曜日

Substitute regexp for -v option

When you check the running processes with ps command and select the target processes only with grep command, it is not a cool solution to do like following.
ps -ef | grep hoge | grep -v grep

Doing like this gives the same result.
ps -ef | grep [h]oge

Because regexp like /[h]oge/ matches "*hoge*" and does not match "[h]hoge" that is the argument of grep command.

2008年3月18日火曜日

Length of array

Length of array in Perl.

use Perl6::Say;
my @array = qw( hoge foo var );
say @array + 0;
say $#array + 1;
say scalar(@array);

2008年3月17日月曜日

Making subroutine alias with typeglob

If you assign an anonymous subroutine to lexical variable $ref, you need to use &$ref to call the subroutine. It's not cool. You can use like calling a usual subroutine if you make aliases with typeglob.
my $target = 'abcbac#ok#dddse';
my $regexp = '#(.+?)#';
*regexp_p = sub { $_[0] =~ m/$_[1]/; defined $1 and print $1; };
regexp_p($target, $regexp);

The name of typeblog that is now using become available after setting it undefined.
undef *regexp_p;

If you want to make an alias of the existing usual subroutine, do like this.
sub regexp_p_sub {
my $target = shift;
my $regexp = shift;
$target =~ m/${regexp}/;
defined $1 and print $1;
}
*regexp_p = \®exp_p_sub;
regexp_p($target, $regexp);

Apparently this sort of thing is not popular, but I was taught so much by the codes of the subroutines to create accessors in Class::Accessor. (the subroutines : _mk_accessors, follow_best_practice)

http://search.cpan.org/src/KASEI/Class-Accessor-0.31/lib/Class/Accessor.pm

I take some sort of reference and modified the above sample.
sub make_glob {
no strict 'refs';
*{ $_[0] } = sub { $_[0] =~ m/$_[1]/; defined $1 and print $1; };
}
make_glob('regexp_p');
regexp_p($target, $regexp);

Pitfalls of idiom

my $input = 0;
my $flag = $input || 1;

The value of $flag is '1'. To be sure, it is not a surprising matter because '0' is evaluated as 'false' in Perl. This is a very famous pitfall of idim in Perl.

There's More Than One Way To Do It. I did it like this for now.
my $input = 0;
my $flag = defined $input ? $input : 1;

2008年3月14日金曜日

Generating accessors automatically in Perl

Class::Accessor::Fast is so useful CPAN module that creates accessors automatically.

In addition, sub classes inherited the accessors that generated in their parent classes. In the following example, an instance of class Foo can use the accessors of 'name' only. But that of class Foo::Var can use the accessors of 'name' and 'birthday'.
package Foo;
use base qw( Class::Accessor::Fast );
__PACKAGE__->mk_accessors( qw( name ) );

package Foo::Var;
use base qw/ Foo /;
__PACKAGE__->mk_accessors( qw( birthday ) );

If this is the case, you can use following accessors.
* getter
$self->get('KEY'), $self->KEY
* setter
$self->set('KEY', 'something new')

But you must use only get_KEY or set_KEY (like recommended in 'Perl Best Practices') if you added the following constraint.
__PACKAGE__->follow_best_practice;

This constraint is continued in the sub classes if you added in the parent classes.

You need to write this above 'mk_accessors' to get it to work properly.

2008年3月2日日曜日

Bloop in using vi ":;wp!"

Hi. The other day, I made a mistake when I was editing a text file with vi.
:;wp!

The file was overwritten with 1 byte text by the upper command. It needs a bit attention.