Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Let’s see an example of how clean and self-describing such a thing looks in the language of your choice. Go on, astound me. :-)

I'm not likely to astound anyone. I write boring code, deliberately. It's easier to find bugs in boring code; code that's full of clever, subtle tricks causes all the problems, in my experience.

A simple series of append statements describes exactly what you want to do, but you seem to want to make life complicated, because you seem overly-concerned about the repetition of the variable name and the .= operator. I think that's only a real concern if you have to write such repeated expressions a lot in a given body of code, but if you really have to be concise (which is part of what I think you mean by "clean"), I still think there are simpler ways to write them.

Here's an even more concise way to write the main expression, using two helper functions. No wierd operator tricks are required, and this code could be written in just about any language, let alone a "language of choice". In some languages, using macros as opposed to helper functions might give a performance boost, but the principle remains the same...

# If the constant string "admin" changes, you can change it # in just one place... sub admin_link { my ($flag) = @_; if ($flag ) { return('admin'); } return(""); } # this can obviously be optimized as necessary... sub join_if_defined { my ( $join_string,@list) = @_; my @defined_elements; @defined_elements = grep( defined($_),@list); return join($join_string, @defined_elements ); } @sections = ( 'http://example.net/app', admin_link($is_admin), $subsite, $node, $id, $subnode ); $uri = join_if_defined("/", @sections);

I think that's reasonablely concise; it's certainly easier to read than a list containing phrases like "a list of one element consisting of <something> repeated not not <some expression> times".


In reply to Re^3: Secret Perl Operators: the boolean list squash operator, x!! by Anonymous Monk
in thread Secret Perl Operators: the boolean list squash operator, x!! by Aristotle

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 chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found