Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Sequential defined-or operators

by ikegami (Pope)
on Jan 14, 2008 at 06:04 UTC ( #662263=note: print w/ replies, xml ) Need Help??


in reply to Sequential defined-or operators

The second last snippet is much more readable than the last. An alternative is

my $theme = $global_config->{modes}{$current_mode}{theme} // $theme_config->{current_theme} // $default_theme // 'none';


Comment on Re: Sequential defined-or operators
Download Code
Re^2: Sequential defined-or operators
by naikonta (Curate) on Jan 14, 2008 at 09:38 UTC
    It's wonderful, ikegami! My worries disappear right away.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      Just be careful with precedence. Absent prototypes overriding the default,

      my $value = $hash{$arg} // func $arg // 'default';
      means
      my $value = $hash{$arg} // func($arg // 'default');

      Putting parens around the low precedence bit solves the problem.

      my $value = $hash{$arg} // (func $arg) // 'default';

      Of course, with functions, you can also do

      my $value = $hash{$arg} // func($arg) // 'default';
        Yes, I'm aware about this issue. I wrote related matter sometime ago. So I believe it will be perfectly fine to write:
        my $value = $hash{$key} // func $arg // 'default';
        knowing that func is a unary operator.

        Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re^2: Sequential defined-or operators
by WoodyWeaver (Scribe) on Jan 14, 2008 at 16:44 UTC
    WWCD? (What would Conway Do?)

    ...Break long lines before an operator. That approach ensures that each line of the continued expression will start with an operator, which is unusual in Perl code. That way, as the reader's eye scans down the left margin of the code, its immediately obvious that an indented line is merely the continuation of the previous line, because it starts with an operator.

    The indenting of the second and subsequent lines of the expression is also critical...they should be indented to the starting column of the expression to which they belong.

    Hence, ikegami. (Although Conway likes to put the terminal semi-colon on a line by itself, to signal completion of the multi-line formula.)
      That approach ensures that each line of the continued expression will start with an operator
      Another reason I support this style is the starting operators can be made inline with the assignment operator where the line begins. I don't think it's so much unusual in Perl code, at least, it's not strange for me.

      However, I still much prefer (and get used) to break long lines after the operator because the adoption of \ (backslash) usage to break long lines in Unix/Linux convention. Still, both styles sign line continuation as we don't see the statement terminator (;) until the last line of the group.


      Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://662263]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-07-10 20:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (215 votes), past polls