RFE: for "%%" becoming to "&&" as "//" is to "||"; the "defined(x) && expr" operator...

In looking at the 5.10 perl5op doc's,  I was thinking about the "//"
operator and how in an expression "a // b" it tests 'definedness' 
the left-hand-side and returns it if it is defined, otherwise, it returns
the right hand side.  The idea being that you might normally use
"||" but if the 'lhs' could be integer '0', it  wouldn't work as
desired.

I think it could be useful to provide an equivalent operator for 
'&&', say '%%', that tests 'defined(lhs)' and if true, returns the 
2nd part (right hand side) of the expression.

Just as you can chain expressions until one is true:  $a || $b || $c,
you can also chain expressions as long as truth is returned:
  $a && $b && $c;

With '//', one can continue or'ing expressions until one is defined:
  $a // $b // $c.

One could also chain expressions as long as they one is defined:

  $a %% $b %% "pass" || "fail;

or, return an 'alternate value' if defined, else return 'undef':

foreach ($a %% "value a = $a", $b %% "bswitch=$b") {
	print $_ // "";
}

I was thinking of its usefulness in switch processing (as '//' was
mentioned for being useful in providing default values).  I am
thinking one might want to create a 'switch string' if a variable
is true, otherwise return 'undef':

	$indentSpaces %% "--indent_spaces $indentSpaces" 

So if ENV{'indentSpaces'} was defined or one parses command line
switch that stores it's value in a variable, then you want to save
the variable contents to a config file, Or one wants to run run
and external program with copious and/or arcane options, one could
use formats:
   "$switchval %% "config_file_long_switch_name: $switchval\n"
   "$switchval %% "--arcane_long_option_name_one $switchval"  or

to generate values for for each.

   I can see an op like "%%" being just as useful as "//" just
in improving comprehension and shortening development time.  
While one could argue its non-necessity, one could argue that
including both '&&' and '||' in a language is unnecessary as one
can replace "$a && $b" with "!(!$a || !$b)".  But the code is certainly
uglier! :-)

   Maybe something like this is already in the works?  Or has this
discussion already been had someplace and I didn't see it? 

   Desirable?  Comments?  Please no jerking of the knees.  I bruise
easily! :-)

linda

0
perl
10/14/2009 8:25:49 PM
perl.perl5.porters 48287 articles. 1 followers. Follow

2 Replies
1154 Views

Similar Articles

[PageSpeed] 47
Get it on Google Play
Get it on Apple App Store

--0016e6d7e325e737300475ee561f
Content-Type: text/plain; charset=ISO-8859-1

Testing for undef twice is very unappealing to me, and that's what you're
going to have to do whenever you use %%.

Spelling it out looks cleaner to me:

    my @opts;
    push @opts, "value a=$a" if !defined($a);
    push @opts, "bswitch=$b" if !defined($b);

Ok, maybe not that much after cleaning up your for loop:

    my @opts = grep defined,
        $a %% "value a = $a",
        $b %% "bswitch=$b";

--0016e6d7e325e737300475ee561f--
0
ikegami
10/15/2009 12:27:07 AM
Eric Brine wrote:
> Testing for undef twice is very unappealing to me, and that's what 
> you're going to have to do whenever you use %%.
> 
> Spelling it out looks cleaner to me:
> 
> ��� my @opts;
> ��� push @opts, "value a=$a" if !defined($a);
> ��� push @opts, "bswitch=$b" if !defined($b);
> 
> Ok, maybe not that much after cleaning up your for loop:
> 
> ��� my @opts = grep defined,
> ������� $a %% "value a = $a",
> ������� $b %% "bswitch=$b";
----
	Thanks, I honestly appreciate the help.  When I'm writing code
from a 'raw idea', it's not very polished.  If my idea or script pans out,
and I want to update it, I make iterative changes -- cleaning it up over
time.  I'm was more trying to throw out ideas of usefulness than polished
code.  If I waited until I had all my polishing done, before I published or
emailed an idea, I'd never get anything sent.  

	As for testing for undefs twice, in the case I was thinking of, 
I'm pulling in a bunch possibly set environment vars and generating args 
for them.  Rather than doing much testing as I pull them in, It's easier to
push the whole bunch onto an array then test the array in a loop for any
pushed 'undef's.   Less typing for me while coding.    Not necessarily less
compute time, but compute time is cheaper for what I'm doing than 
the extra typing time.  

	If this was compute intensive code -- might be different, but previous
version was in shell code, so how compute intensive can it be?  :-)

linda


0
perl
10/15/2009 1:53:01 AM
Reply:

Similar Artilces:

"&" become "&" in QueryString when databinding.
I am trying to bind some data to form a QueryString in a custom button column in a custom datagrid. as shown below:         private void cell_DataBinding(object sender, EventArgs e)         {                  string URLconnector = (strUrl.IndexOf("?")>0)?@"&":@"?";                 ((Button)(cell.Controls[0])).Attributes.Add("onclick", @"window.showModalDialog('container.aspx?url=" + strUrl + &...

Distinguish "To:" from "Cc:" &/or "Bcc:" ? ????
When sending an email, is there any way for recipient to know that his address was in: 1. "To:" or 2. "Cc:" or 3. "Bcc:" field? Richard Owlett wrote: > When sending an email, is there any way for recipient to know > that his address was in: > 1. "To:" > or > 2. "Cc:" > or > 3. "Bcc:" > field? Yes. If it is in TO: or CC: it will show in the headers. Plainly visible. If it is in BCC: it will not show at all -- but then of course he'll...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

What is this for? "<label style=""display:none;"" for=""" & cboFieldType.ClientID.ToString & """>Type</label>"
I'm digging through the code of DNN and I found many ocurrences of this type of code: lblFieldType.Text = "<label style=""display:none;"" for=""" & cboFieldType.ClientID.ToString & """>Type</label>" What is that code for? The label is never displayed and I could not figure out what DDN uses it for. thank you This is for ADA compliance. The guidelines require that form elements have an associated label. This helps the text readers out with identifying the different parts of the form. BruceDynamic...

quotes, quotes, quotes...
I am getting this error and I know what is causing it, but I have no idea how to fix it, any help would be great. The script steps through the /var/log/messages file on a linux server and puts The entries into a mysql database. However when it gets to the 'hlt' line in the messages file it just barfs. The single quotes are freaking it out. I know about quotes but not how to use in this situation. Thanks, Paul Error: May 27 17:53:00 localhost kernel: Checking 'hlt' instruction... OK. <----- doesn't like this in the messages file DBD::mysql::st exec...

"Page Inherits" , "Namespace" , "Register TagPrefix" , & "Assembly"
Hi,         I'm just start learning web matrix, can anyone explain to me about the "Page Inherits" , "Namespace" , "Register TagPrefix" , & "Assembly"  Thanks you~~          Most of the information you are asking you can get definitions from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconPage.asp linkHope it helps!Sreedharhttp://www.w3coder.orgweblog http://weblogs.asp.net/skoganti Thanks a lot! :)...

.Replace("&amp;", "&") doesnt seem to work.
 protected void Button3_Click(object sender, EventArgs e)    {        ExportGridToExcel(GridView2, "cc");       }    public void ExportGridToExcel(GridView grdGridView, string fileName)    {        Response.Clear();        Response.AddHeader("content-disposition",        string.Format("attachment;filename={0}.csv", fileName));    &...

replace the "." with a ","
Oi.... I need to build a small programm in ASP.NET and chose to use C# for it.Now i got everything working but there's one little problem.the first textbox is a double. I need to make it so that when someone enters a "." then it gets replaced by a ","any ideas?Ghan  string blah = "4.2.2.2";blah = blah.Replace(".", ",");Ryan Ryan OlshanASPInsider | Microsoft MVP, ASP.NEThttp://ryanolshan.comHow to ask a question...

Replacing "\\" with "\"
Hi all I'm getting this value from a CheckBoxList control - a location of file, i have to remove "\\" and replace it with "\" and pass it to Query, how to do it, i tried with Replace, but coud'nt suceed. "\\\\Blaze10xp\\BLZ_SFS_07\\Sample Excel Files\\Excel Files\\report2.xls" thank's in advance - Prakash.C you tried Replace like this? string newstring = oldstring.Replace(@"\\",@"\");Plese, do not forget to click "Mark as Answer" on the post that helped you. Thanx!My blog: Scenes From A Developer Memory yes i tr...

double quote
hello there...  i tried everything of think but not working the way i wanted to be... not sure what i'm missing...i'm generating a <span> in code behind and then using in javascript.... here is what i'm doing code behind: int i=0string _keywordID = "keyword";string _name = row["visit_info_nm"].ToString().Trim(); String _getElementByID = String.Format("<span id='{0}' OnClick = \"document.getElementById('{1}').value='{2}';\">{3}</span><br>", i, _keywordID, _name, _name); here is what it generate : <span id='1' OnClick = \"document.getElementById('keyword')...

"-" not "_"
I wrote a SQL statement in the data tab. I wrote a bunch of alaises as example ' word-type ' but when I hit the layout tab it converts the "-" to "_". So now my field name is ' word_type '. Is there any way to prevent this? CardGunner Don' use a hypen ( - ).  It isn't a valid character for column names.   See http://searchsqlserver.techtarget.com/expert/KnowledgebaseAnswer/0,289625,sid87_gci1188931,00.html   Here's an excerpt about column names: Letters as defined in the Unicode Standard 2.0 Decimal numbers from either B...

"Me" is better than "You"
Yes I know, strings are frozen. But let me talk about it, I really can't get through the idea of a PC talkin to me. I consider my PC as an extension of myself, not a dumb companion who addresses Me as You. Yes there are times when I get angry with Him while I work and get wrong calculations etc.., but it really is my fault, Me using wrong istructions and eventually wanting to find someone else to blame, but it's Me. And yes, I consider Thunderbird my mail program, reading my mail on my PC as Me. So I personally like to have Me in the header bar as a compact address ...

"To" and "From" missing
When I print emails, the words "To" and "From" are blank, even though the "To" name and "From name (addresser, addressee) do show up. This is not a problem for other users on my system. Suggestions In mailbox right click, view. On the message window, right click and choose print options. Make sure print header is checked. -- Barry Merchant NSC Volunteer SysOp *** no email unless requested please!! *** > In mailbox right click, view. On the message window, right click and > choose print options. Make sure prin...

Web resources about - RFE: for "%%" becoming to "&&" as "//" is to "||"; the "defined(x) && expr" operator... - perl.perl5.porters

Resources last updated: 12/15/2015 3:29:54 AM