NAME

Regexp::Bind - Bind variables to captured buffers

SYNOPSIS

use Regexp::Bind qw(
                    bind
                    global_bind
                    global_arraybind
                   );

$record = bind($string, $regexp, @fields);
$record = global_bind($string, $regexp, @fields);
@record = global_arraybind($string, $regexp, @fields);

DESCRIPTION

This module is an extension to perl's native regexp function. It binds matched variables to an anonymous hash or named variables. You can view it as a tiny and petite template extraction system.

Three functions are exported. They bind the given field names to captured contents, and return an anonymous hash to the fields.

use Data::Dumper;

# match the first occurrence
$record = bind($string, $regexp, qw(field_1 field_2 field_3));
print Dumper $record;

# global matching
while($record = global_bind($string, $regexp, qw(field_1 field_2 field_3));
  print Dumper $record;
}

# do global matching and store matched parts in @record 
@record = global_arraybind($string, $regexp, qw(field_1 field_2 field_3));
print Dumper $_ foreach @record;

To use named variable binding, please set $Regexp::Bind::USE_NAMED_VAR to non-undef, and then matched parts will be binded to named variables while using bind() and global_bind(). It is not supported for global_arraybind().

$Regexp::Bind::USE_NAMED_VAR = 1;
bind($string, $regexp, qw(field_1 field_2 field_3));
print "$field_1 $field_2 $field_3\n";

while( global_bind($string, $regexp, qw(field_1 field_2 field_3) ){
  print "$field_1 $field_2 $field_3\n";
}

See also test.pl for an example.

For a similar functionality, see Regexp::Fields. But I don't really like that style messing up with regexp itself, so I built up this module.

COPYRIGHT

Copyright (C) 2004 by Yung-chung Lin (a.k.a. xern) <xern@cpan.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself