Greg London wrote:
> ...replace your printline() function with a closure.
> Instead of doing this over and over:
> printline($var1,\$y,"text","font",$size);
> You could take $y and put it inside a lexical block...

To me this looks like an example of where we suffer by not having OO
truly baked into the language. It's a pretty natural fit for this
problem, but not the first thing people shout out, because we tend to
want to keep small scripts simple and procedural, and not bother with
the overhead of OO.

package Thermal::Printer;

sub new {
  ...
  if (!$args{context})
      my $surface = Cairo::ImageSurface->create(...);
      $args{context} = Cairo::Context->create($surface);
  }
  ...
}
...

package main;

  my $printer = Thermal::Printer->new(
    font => $font, # default
    size => $size, # default
  );
  ...
  $printer->write($text);
  $printer->font($other_font)->size($other_size)->write($more_text);
  ...

That particular example has font and size as object attributes, which
you can update to alter from their initial condition, and then they'll
persist with the new value until altered again. Alternatively you could do:

    $printer->write($text, font => $other_font, size => $other_size);

to override defaults for just that one write() call. Both options could
be implemented without conflict.

The OO approach gives you a cleaner syntax, and keeps all the internal
housekeeping details (context, position) hidden within the object.


>       my($text,$size,$font)=@_;

Federico mentioned dislike of the subroutine argument declaration in
Perl. How about:

http://search.cpan.org/~noirin/perl5i-v2.11.2/lib/perl5i.pm#Subroutine_and_Method_Signatures
 or:
http://search.cpan.org/~zefram/Devel-Declare-0.006011/lib/Devel/Declare.pm

 -Tom

-- 
Tom Metro
Venture Logic, Newton, MA, USA
"Enterprise solutions through open source."
Professional Profile: http://tmetro.venturelogic.com/

_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to