[Petal] Re: improvement on the _code_with_line_numbers() method and how to writte my own modifiers?

Jean-Michel Hiver jhiver@mkdoc.com
Fri, 6 Sep 2002 15:11:35 +0100


[CC'ed to the Petal mailing list]

> i've changed the _code_with_line_numbers() method...padding the numbers with spaces looks
> better..look the new code:
> 
> sub _code_with_line_numbers
> {
>     my $self = shift;
>     my $code = $self->_code_disk_cached;
> 
>     # get lines of code
>     my @lines = split(/\n/, $code);
> 
>     # add line numbers
>     my $count = 0;
>     @lines = map {
>       my $cur_line = $_;
>       $count++;
> 
>       # space padding so the line numbers nicely line up with each other
>       my $line_num = sprintf("%" . length(scalar(@lines)) . "d", $count);
> 
>       # put line number and line back together
>       return "${line_num} ${cur_line}";
>     } @lines;
> 
>     return join("\n", @lines);
> }
> 
> could you add this 'improvement' in 0.65?

Looks good! Thanks a lot!


> oh, one question:
> 
> 1 - Could you explain to me how to writte my own modifiers? I need some help to do this,
> because i want to add some data verification to my app...
> 
> FUTURE MODIFIERS:
> 
> "is_whole:"      => 'Petal::Hash::Types::Is_Whole',
> "to_whole:"      => 'Petal::Hash::Types::To_Whole',
> "is_int:"        => 'Petal::Hash::Types::Is_Int',
> "to_int:"        => 'Petal::Hash::Types::To_Int',
> "is_decimal:"    => 'Petal::Hash::Types::Is_Decimal',
> "to_decimal:"    => 'Petal::Hash::Types::To_Decimal',
> "is_real:"       => 'Petal::Hash::Types::Is_Decimal',
> "to_real:"       => 'Petal::Hash::Types::To_Decimal',
> "is_float:"      => 'Petal::Hash::Types::Is_Float',
> "to_float:"      => 'Petal::Hash::Types::To_Float',
> "is_string:"     => 'Petal::Hash::Types::Is_String',
> "to_string:"     => 'Petal::Hash::Types::To_String',
> 
> AND THE CURRENT CODE:
> 
> sub is_whole
> {
>     return unless($_[0]);
>     return unless($_[0] =~ /^\d+$/);
>     return 1;
> }
> <SNIP />

You write a very simple module:

  package Petal::Hash::Types::Is_Whole;
  use strict;
  use warnings;

  sub process
  {
      my $class = shift;
      my $hash  = shift;
      my $value = $hash->FETCH (@_);
      return unless($_[0]);
      return unless($_[0] =~ /^\d+$/);
      return 1;
  }

  
  1;


  __END__


To make Petal aware of the new modifier you need to do the following at
the beginning of your program:

  $Petal::Hash::MODIFIERS->{'is_whole:'} = 'Petal::Hash::Types::Is_Whole';


My collegue Bruno suggested to me the other day a 'lightweight' modifier
syntax which would work as follow:

  $Petal::Hash::MODIFIERS->{'is_whole:'} = sub {
      my $hash  = shift;
      my $value = $hash->FETCH (@_);
      return unless($_[0]);
      return unless($_[0] =~ /^\d+$/);
      return 1;
  }


I find the idea very interesting (it saves writing modules for 'small'
modifiers), so it'll probably be implemented quite soon.
  
Implementing that pluggable modifiers system seems to have been
definitely a good move, I am amazed by the amount of ideas that you guys
come with :-)

I'll try to figure out how to implement a better modifier system which
would save the hassle of having to 'register' the modifiers in the
$Petal::Hash::MODIFIERS hash, it would allow you to put all these
modules on CPAN!

Cheers,
-- 
IT'S TIME FOR A DIFFERENT KIND OF WEB
================================================================
  Jean-Michel Hiver - Software Director
  jhiver@mkdoc.com
  +44 (0)114 255 8097
================================================================
                                      VISIT HTTP://WWW.MKDOC.COM