Everybody seems to be happy with the presence of defined-or (//) operator in Perl 5.10, including me. As I often write:
my $default = 0;
my $input_number = $ARGV[0];
my $number = defined $input_number ? $input_number : $default;
Now I can write:
my $number = $input_number // $default;
Lacking of chances to download and install Perl 5.10 and test it myself (which can be read as 'lazy' :-) ), I wonder if any monks have experiences on using this one of new-long-waited-features in sequential. Take a look at this example:
my $default_theme = get_default_theme();
my $global_config = read_config('/path/to/config_file');
my $theme_config = read_config($global_config->{theme_file});
my $current_mode = get_current_mode();
my $theme = defined $global_config->{modes}{$current_mode}{theme} ?
$global_config->{modes}{$current_mode}{theme} :
defined $theme_config->{current_theme} ?
$theme_config->{current_theme} :
defined $default_theme ?
$default_theme :
'none';
With defined-or operator, can I simply write
my $theme = $global_config->{modes}{$current_mode}{theme} //
$theme_config->{current_theme} //
$default_theme //
'none';
or, should I use some parens to delimit the (inner) expressions such as,
my $theme = (
(
$global_config->{modes}{$current_mode}{theme}
//
$theme_config->{current_theme}
)
//
$default_theme
)
//
'none';
Is there any other alternative? Is this possible at all?
Thanks all for sharing your experiences.
Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!
-
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.