Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Wrong idioms

by Lotus1 (Chaplain)
on Mar 29, 2013 at 23:16 UTC ( #1026206=note: print w/ replies, xml ) Need Help??


in reply to Wrong idioms

++ for mentioning the -n Bash operator, I wasn't aware of the string testing operators in Bash. I was shocked to find out that Bash uses == for strings and -eq for integers.

I found a stackoverflow node that deals with the empty string issue and the best answer for Perl seemed to be to use the length function.

return unless length $str;


Comment on Re: Wrong idioms
Select or Download Code
Re^2: Wrong idioms (digression)
by Intrepid (Deacon) on Mar 31, 2013 at 19:05 UTC

    Lotus1 wrote

    I found a stackoverflow node that deals with the empty string issue and the best answer for Perl seemed to be to use the length function.

    IMHO, probably so! And FWIW, Perl is not the only programming language in which some obvious problems pertaining to "truth" might need to be thought about carefully. We've discussed Bash, and here's another one: VimL or Vim script (AKA “ex”).

    In VimL it is complicated where Bash makes it easy. To test for both defined-ness and non-null-ness / non-zero-ness, there are some hoops to jump through.

    Vim will complain if we try to do anything with a variable identifier that has not been defined yet. So I found myself having to write a function that does this, since I was rewriting the same code so often in my .vimrc:

    # Vim script language function! EnNZ(kipper) if !exists(a:kipper) | return 0 | endif exec 'let arg_ref = '.a:kipper if ( strlen(arg_ref) || arg_ref != 0 ) return 1 endi return 0 endfunction

    First a test must be run to determine if the referenced variable even exists. Then if it does we need to "extract" the reference (ok, "dereference" if you like). Then we can finally test the data actually in the variable, using "length" (but in VimL we spell it strlen() as if we were C programmers), and also accounting for a numeric value by following that test with a numerical comparison. The end result is that we've worked out whether this variable is approximately "true" or "false" in a Perlish sense.

    This is not a Perl topic ... but improvements, critiques welcomed (I know there are Vimmers hiding in plain sight around here).

    Oh, and BTW: anyone who went Ewwww when they perceived the design principles of VimL revealed in that code: you wouldn't be the first ;-)

    Intrepid Mar 31, 2013 at 18:26 UTC
    Examine what is said, not who speaks.
    Love the truth but pardon error.
    Silence betokens consent.
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2014-12-27 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls