... you are pretending to not understand it. Presumably. ... if you've ever worked in a production environment with code more than five minutes old. Say 20+ years.

As I said, I was trying to present arguments for "the Perl way" of looking at things. I intended my replies to be friendly, and if you took offense at the few light jabs I made, I apologize. If, as you seem to be saying, you have 20+ years of C experience, then I know I could learn from that. I'm just offering my 20+ years of Perl experience here. As you can probably imagine I'm not a big fan of insults, and that's not what I'm trying to do here, but I am a fan of correctness, and unless I misunderstood you, I do think you're wrong in a couple of points.

You can try to pretend being explicit about what a return value of something must be is bad, but if you say that you can't sit there and argue that having literally no idea is more informed. That is worse than wrong. ... With explicit return values, there is no confusion unless you are deliberately being foolish.

Sorry but I think you are mistaken about what I said and meant. Let me be more direct: defined is documented to return "a Boolean value", nothing more. Testing a boolean with if( BOOL eq "" ) to check for false values is incorrect. As per Truth and Falsehood, which I linked to earlier, there are three basic "false" values in Perl: 0, "", and undef. The test would fail on the first case because "0" eq "" is false. (Update: More on the comparison operators here.)

The way to explicitly check for "false" in Perl is if( not EXPR ) or the other variants I showed. The way to check for "true" is if( EXPR ), or, if you want to be really explicit about it, which was how I understood your argument, then use if( !!(EXPR) ) (see also "Bang bang" in perlsecret).

Not quite, in Perl it could also be "0 but true" (e.g. sysseek), or in theory any other "true" value.
Sysseek does not return a conditional, it returns a position or undef. ... Ironically, mentioning sysseek is important, they couldn't do if( ! sysseek() ){...} because the failure state, undef, and a success state, 0, both become a truthful value after not so they ended up using an exception to get around a lousy logic style.

Are you talking about C or Perl here? Because in Perl, you can do if( !sysseek() ) to test for failure, that is the point of the "0 but true" return value - when evaluated as a number, it is 0, and in a boolean context, it is true. So the only false value sysseek should return is undef on failure, and it shouldn't normally throw an exception either. Note that the string "0 but true" is special-cased in Perl to not warn when it is used as a number, this is described in fcntl and ioctl. Another value that is sometimes used as a "numerically zero but true in a boolean context" value is the string "0E0".

sysseek($fh,0,0) returned "0 but true" sysseek($fh,42,0) returned 42 sysseek($fh,-1,0) returned undef undef: bool=false, str= "", num=0 (warns: uninit x 2) "": bool=false, str= "", num=0 (warns: numeric) 0: bool=false, str= "0", num=0 "0": bool=false, str= "0", num=0 1: bool=true , str= "1", num=1 "1": bool=true , str= "1", num=1 "foo": bool=true , str= "foo", num=0 (warns: numeric) Perl's true: bool=true , str= "1", num=1 Perl's false: bool=false, str= "", num=0 "0 but true": bool=true , str="0 but true", num=0 "3 and true": bool=true , str="3 and true", num=3 (warns: numeric) "0E0": bool=true , str= "0E0", num=0 0E0: bool=false, str= "0", num=0 1E0: bool=true , str= "1", num=1 "1E0": bool=true , str= "1E0", num=1
use warnings; use strict; use File::Temp qw/tempfile/; use Data::Dumper; sub pp { Data::Dumper->new([@_])->Useqq(1)->Terse(1) ->Indent(0)->Dump } my ($fh) = tempfile(UNLINK=>1); print $fh "Perl"x12; for my $pos (0, 42, -1) { my $rv = sysseek($fh,$pos,0); print "sysseek(\$fh,$pos,0) returned ",pp($rv),"\n"; } print "\n"; my @vals = ( 'undef' => undef, # warns as number and string '""' => "", # warns as number '0' => 0, '"0"' => "0", '1' => 1, '"1"' => "1", '"foo"' => "foo", # warns as number "Perl's true" => !0, "Perl's false" => !1, '"0 but true"' => "0 but true", # doesn't warn as number (!) '"3 and true"' => "3 and true", # warns as number '"0E0"' => "0E0", '0E0' => 0E0, '1E0' => 1E0, '"1E0"' => "1E0", ); while (@vals) { my ($desc, $val) = (shift @vals, shift @vals); #no warnings qw/ uninitialized numeric /; printf "%13s: bool=%-5s, str=%12s, num=%s%s\n", do { my %w; local $SIG{__WARN__} = sub { my $w = shift; if ($w=~/numeric/) { $w{numeric}++ } elsif ($w=~/uninitialized/) { $w{uninit}++ } else { warn $w } }; ($desc, $val?'true':'false', '"'.$val.'"', 0+$val, keys(%w) ? " (warns: ".join(',',map {$w{$_}>1?"$_ x $w{$_}":$_} sort keys %w).")" : '' ); }; }

pryrt also wrote a good post in a similar vein here.

Defined itself not returning the value on success or undef on not defined is probably the core reason we can't have sensible returns.

Sorry, I don't understand this point.

In C there are more than one return values on failures, and there are functions which return status values or pointers.

Right, which is why I said "you don't check that with somefunc()==-1". What I meant is that if a function is documented to return a negative value on failure, I'd first use if( somefunc()<0 ), or, if I needed to inspect it closer, int rv = somefunc(); if ( rv<0 ) { handle_error(rv); } or something along those lines.

Additionally, even if you use defined, you have to know in advance whether or not your function can return a defined-but-false value. ... And documentation is nice, except if you've ever worked in a production environment with code more than five minutes old.

I completely agree that poorly maintained documentation is a problem. Fortunately, the Perl documentation for the most part doesn't suffer from that problem and can be taken seriously.

Update 2019-08-17: Updated the link to "Truth and Falsehood".


In reply to Re^7: Regex result being defined when it shouldn't be(?) by haukex
in thread Regex result being defined when it shouldn't be(?) by chenhonkhonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.