[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Re: [oma-python] an almost-switch statement



On Thu, 10 Jun 2004 00:03:45 -0500, Jay Hannah <jay@jays.net> wrote:
> 
> 
> [For the record, thehaas drug me into a Perl discussion on this list. I
> tried not to mention Perl at all... -grin-]

Sorry about that.  

But, hey, we can all learn together, right? 
> 
> On Jun 1, 2004, at 4:19 PM, thehaas@binary.net wrote:
> > I've used Perl quite a bit. Never used anonymous subroutines because
> > the syntax is ugly.
> 
> It is? From example:
> 
> http://www.python.org/doc/current/tut/
> node6.html#SECTION006750000000000000000
> 
> Python:
> 
> $ python
>  >>> def make_incrementor(n):
> ...     return lambda x: x + n
> ...
>  >>> f = make_incrementor(42)
>  >>> f(0)
> 42
>  >>> f(1)
> 43
> 

That's a horrible example of a lambda.  I use it for one-liners that I
need that it doesn't make sense to declare another function for.  I
also usually combine with with the map function.

Here is an example of real working code that decodes a list of Unicode
strings into latin-1:
   goodProcRef = map(lambda x: x.decode("latin-1"), goodProcRef)

But these days most people are leaving lambda's behind and using
generators for a lot of there stuff.

http://c2.com/cgi/wiki?GeneratorsInPython
http://www-106.ibm.com/developerworks/linux/library/l-pythrd.html

> I couldn't figure out how to do *exactly* that in Perl. I'm not sure I
> understand when you'd want to. You can skip the def step in Perl (I
> don't know if you could skip the def step in Python if your wanted to)
> thusly:
> 
> $ perl -de 1
> DB<1> $f = sub { $_[0] + 42 }
> DB<2> p &$f(0)
> 42
> DB<3> p &$f(1)
> 43
> 

It's a bit different than the Python version above -- basically the same as
   DB = lambda x: x+42


> > OO in Perl is even uglier.
> 
> Indeed, it can be. I like Perl OO, but it certainly doesn't have native
> cascading public/private attribute inheritance. You can do it (and we
> do... a lot...), but it is ugly.

I don't mind using modules in Perl that give back object and using
their methods/attributes.  But it get's funky sometimes.  And I've
never made my own class in Perl, but I've never really needed to.

> > Will Perl 6 ever get out? ;)
> 
> At OSCON 2003 Larry Wall and Damian Conway were talking about 2007.
> Parrot it coming along, apparently...
>

Cool.  I didn't know there was anything close to a formal announcement 

And how much code breakage will there be after that? ;)

> j
> 
> P.S. Contrary to my last post, Perl has a "Switch" module I didn't know
> about:
> 
>     http://search.cpan.org/~rgarcia/Switch-2.10/Switch.pm
> 
> It's distributed in core Perl nowadays, so "use Switch" should be
> available by default.

One thing about Perl is that there is a module of everything and
anything.  If it's not in core, CPAN makes it an easy install.  
Python is getting better -- I can find libraries for just about
anything I want to do anymore, but it is lagging behind in the CPAN
part, which it too bad.

Thanks for the discussion!

-- mikeh (TheHaas)