Greg London wrote:
> To do that in a class, you have to have a lexical container
> or put it in a separate file.
> 
> {
>  package increment;
>  sub new{ return bless({Name=>shift(@_),count=>shift(@_)}), 'increment'); }
>  sub inc{ my $obj=shift(@_); $obj->{count}++;
>     print "incremented ".($obj->{Name})." to ".($obj->{count}."\n";
>  }
> }

Your outermost level of curlies is not required. Simply:

package Class;
...

package main;
...

will do the job if you want to do it all in one file.

(There are some cases where the lexical isolation is desired or
necessary, such as if you want to insure lexical vars declared in Class
aren't accessible in main.)


> I've done quite a few "closure constructors" like this myself:
> 
> sub constructor {
>   my($name,$count)=@_;
>   my $closure=sub{
>      $count++;
>      print "incremented $name to $count\n";
>   };
>   return $closure;
> }
> 
> This sort of thing returns a variable that acts like an "object"
> as far as I'm concerned. And it has zero linguistic overhead.

I wouldn't say it acts like an object, but I'd agree it is more suited
to the job, if that's all you are looking to do.

In my opinion, it wouldn't be the best fit for the problem posed in
Federico's code.


> I gotta say, the Moose.pm documentation started in second gear
> and stripped my clutch.

:-) I'm sure someone on the list can recommends a good video/slideshare
presentation that provides a more accessible introduction to Moose.


> My default, for almost anything that doesn't
> have a specific language requirement is to use perl.

Ditto.

 -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