Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
A new episode in the series: "Yes, Perl can do!"

I recently had a discussion with a fellow perlmonger about a 3h video¹ about meta-programming in Python thanks to so called decorators. (something like advice in LISP)

So my reply was

Great, but what exactly are the benefits over attributes and type-glob manipulation in Perl?

His long reply can be summarized with something like

  • Attributes are very difficult to handle
  • "Nobody" understands them
  • no good tutorials
  • In Python decorators are first class objects with very short elegant code
  • glob manipulations in Perl are cryptic
  • Attributes are rarely used in Perl
  • While typical use cases in Python are
    • memoization,
    • debugging,
    • logging
    • CLI wrapping,
    • route-wrapper for web-applications
    • ...

So I accepted the challenge to prove him wrong:

Taking this (rather stupid) example from SO:

def makebold(fn): def wrapped(): return "<b>" + fn() + "</b>" return wrapped def makeitalic(fn): def wrapped(): return "<i>" + fn() + "</i>" return wrapped @makebold @makeitalic def hello(): return "hello world" print hello() ## returns <b><i>hello world</i></b>

I hacked this code

use strict; use warnings; use Data::Dumper qw'Dumper'; use Attribute::Handlers; use feature 'say'; sub wrap { my ($glob,$c_wrapper) = @_; no warnings 'redefine'; *$glob = $c_wrapper; } sub BOLD :ATTR { my ($pkg,$glob,$ref) = @_; wrap $glob => sub { "<b>" . $ref->() . "</b>" }; } sub ITALIC :ATTR { my ($pkg,$glob,$ref) = @_; wrap $glob => sub { "<i>" . $ref->() . "</i>"}; } sub hello :ITALIC :BOLD { return "hello world"; } say hello(); __END__ <b><i>hello World</i></b>

IMHO the Perl code is already better readable and can even be further improved with more syntactic sugar.

Not every decorator really needs to wrap code, abstracting it into a function wrap from an imported module seems reasonable (Python-folks always return the identical function-ref to handle this)

My friend was impressed, but maybe he wasn't critical enough.

Here my questions :

  • Did I miss something?
  • Does Attribute::Handlers have traps to be avoided?
  • Why do we ignore attributes?
  • Why don't we adapt (i.e. steal) good use cases from Python or LISP

Actually I wanted to meditate more about the issue and refine the code before posting, but I'm quite busy at the moment and the risk to forget this task in a drawer is quite high.

So following the release often paradigm I just posted my raw results...

At least I hope you have now good arguments, if someone claims Python was superior because of decorators!

Cheers Rolf

( addicted to the Perl Programming Language)

¹) OMG 8-|


In reply to RFC: Simulating Python's @decorators in Perl with Attributes by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found