Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Wrong idioms (digression)

by Intrepid (Deacon)
on Mar 31, 2013 at 19:05 UTC ( #1026376=note: print w/replies, xml ) Need Help??

in reply to Re: Wrong idioms
in thread Wrong idioms

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026376]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-04-22 07:49 GMT
Find Nodes?
    Voting Booth?