A question about grep


M

Martin Lee

I read Intermediate Perl today, and I found a piece of code that is:

use HTTP::SimpleLinkChecker qw(check_link);
my @good_links = grep {
check_link( $_ );
! $HTTP::SimpleLinkChecker::ERROR;
} @links;

and I modify this code , like this:

use HTTP::SimpleLinkChecker qw(check_link);
@links = ("http://www.google.com", "http://www.facebook.com", "http://mcnvakdlectter.com")
my @good_links = grep {
check_link( $_ );
! $HTTP::SimpleLinkChecker::ERROR;
} @links;
print "$_\n" for @good_links;

the "mcnvakdlectter.com" is a wrong url, but I get this when I execute this code:

http://www.google.com
http://www.facebook.com
http://mcnvakdlectter.com

I refer to the usage of HTTP::SimpleLinkChecker, this module can get the HTTP response code, I test with http://mcnvakdlectter.com and it return 500, but I
don't know why this url is in @good_links , it's a broken websit!!

PS:
Could you tell me the mean of "! $HTTP::SimpleLinkChecker::ERROR;" in the last code? :)

Thank you very much!
 
Ad

Advertisements

R

Rainer Weikusat

Martijn Lievaart said:
]

PS:
Could you tell me the mean of "! $HTTP::SimpleLinkChecker::ERROR;" in
the last code? :)

That variable is documented as a string, '!' is not. 'Not' some string is
not well documented, but seems to return an empty string if the string is
not empty and 1 if the string is empty.

! is documented as 'returns the logical negation of its right operand'
and perlsyn(1) states that

Truth and Falsehood
The number 0, the strings '0' and '', the empty list "()", and
"undef" are all false in a boolean context. All other values are
true. Negation of a true value by "!" or "not" returns a special
false value. When evaluated as a string it is treated as '', but
as a number, it is treated as 0.

How's that "not well documented"?
 
P

Peter J. Holzer

! is documented as 'returns the logical negation of its right operand'
and perlsyn(1) states that

Truth and Falsehood
The number 0, the strings '0' and '', the empty list "()", and
"undef" are all false in a boolean context. All other values are
true. Negation of a true value by "!" or "not" returns a special
false value. When evaluated as a string it is treated as '', but
as a number, it is treated as 0.

How's that "not well documented"?

It doesn't document what negation of a false value returns. (although I
doubt that this is what Martijn meant, since it isn't specific to
strings.)

It is also in the wrong place. The return values of operators should be
documented in perlop. The special false value should probably be
documented in perldata and maybe true/false values in general, too
(although that needs to be at least mentioned/referenced in perlsyn,
too).

hp
 
M

Martin Lee

The last can be even be boiled down to:



print "$_\n" for grep defined(check_link($_)), @links;



But that is a matter of taste, when learning Perl, it's better not to be

overly smart and terse.

Thank you, I agree with you, and I think your synx is cool!
 
R

Rainer Weikusat

Peter J. Holzer said:
It doesn't document what negation of a false value returns. (although I
doubt that this is what Martijn meant, since it isn't specific to
strings.)

The claim was "'Not' some string' is not well documented". The
documentation states that 'not' performs logical negation, quotes all
examples of string counting as 'false values' and states that any other
string counts as a 'true value'. That's not qualtiatively different from
the way other 'kinds' of values (eg, numbers) are treated in this
respect. Hence, it doesn't make sense to single out strings in this
respect and I, too, doubt that the original statement was supposed to be
a general "Perl is not well documented" (or event just "the ! operator
is not well documented") lament ...
 
R

Rainer Weikusat

Martijn Lievaart said:
Martijn Lievaart said:
]

PS:
Could you tell me the mean of "! $HTTP::SimpleLinkChecker::ERROR;" in
the last code? :)

That variable is documented as a string, '!' is not. 'Not' some string
is not well documented, but seems to return an empty string if the
string is not empty and 1 if the string is empty.

! is documented as 'returns the logical negation of its right operand'
and perlsyn(1) states that

Truth and Falsehood
The number 0, the strings '0' and '', the empty list "()", and
"undef" are all false in a boolean context. All other values are
true. Negation of a true value by "!" or "not" returns a special
false value. When evaluated as a string it is treated as '', but
as a number, it is treated as 0.

How's that "not well documented"?

It did not occur to me to look in perlsyn. How is negation 'syntax'??

IMO, this paragraph is misplaced and rather belongs to perlop (and it
should also, should document the result when negating a false value,
which happens to be a 'special truth value' whose numerical value is 1
and whose string value is '1'). But 'returns the logical negation of its
right operand' isn't terribly expressive on its own for other types,
either, eg, why shouldn't "the logical negation of 1" be "-1"?
 
Ad

Advertisements

T

Tim McDaniel

perlsyn:
Truth and Falsehood
The number 0, the strings '0' and '', the empty list "()", and
"undef" are all false in a boolean context. All other values are
true. Negation of a true value by "!" or "not" returns a special
false value. When evaluated as a string it is treated as '', but
as a number, it is treated as 0.

should also, should document the result when negating a false value,
which happens to be a 'special truth value' whose numerical value is
1 and whose string value is '1').

I'm afraid I'm not following you. The negation of a true value does
indeed have a slight difference from both normal '' and 0:

$ perl -e 'use warnings; use strict; my $x = ""; print($x + 5, "\n"); print ("($x)\n");'
Argument "" isn't numeric in addition (+) at -e line 1.
5
()

$ perl -e 'use warnings; use strict; my $x = !1; print($x + 5, "\n"); print ("($x)\n");'
5
()

But 1 or '1' has no such specialness: you can perform arithmetic with
either without a warning, for example, and each converts to the other
in the appropriate circumstances. In what way is the value of !0 a
special value?
 
R

Rainer Weikusat

I'm afraid I'm not following you. The negation of a true value does
indeed have a slight difference from both normal '' and 0:

$ perl -e 'use warnings; use strict; my $x = ""; print($x + 5, "\n"); print ("($x)\n");'
Argument "" isn't numeric in addition (+) at -e line 1.
5
()

$ perl -e 'use warnings; use strict; my $x = !1; print($x + 5, "\n"); print ("($x)\n");'
5
()

But 1 or '1' has no such specialness: you can perform arithmetic with
either without a warning, for example, and each converts to the other
in the appropriate circumstances. In what way is the value of !0 a
special value?

In exactly the same way as the other:

[[email protected]]~/work#perl -MDevel::peek -e 'Dump(!0), Dump(!1)'
SV = PVNV(0x817a634) at 0x817851c
REFCNT = 2147483646
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x817b618 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x817a620) at 0x817850c
REFCNT = 2147483647
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x817a610 ""\0
CUR = 0
LEN = 4

In particular, it has all the *OK flags set from the start. Also, it is
"a special truth value" because there's an infinite set of other values
which are also regarded as 'true' but this particular one happens to be
used. As far as the documentaton goes, this

[[email protected]]~/work#perl -e 'use Scalar::Util "dualvar"; $a = dualvar(-9, "Wot?!?"); print("$a\n") if $a;'
Wot?!?

would just be as possible.
 
T

Tim McDaniel

[email protected] (Tim McDaniel) said:
I'm afraid I'm not following you. The negation of a true value does
indeed have a slight difference from both normal '' and 0:

$ perl -e 'use warnings; use strict; my $x = ""; print($x + 5, "\n"); print ("($x)\n");'
Argument "" isn't numeric in addition (+) at -e line 1.
5
()

$ perl -e 'use warnings; use strict; my $x = !1; print($x + 5, "\n"); print ("($x)\n");'
5
()

But 1 or '1' has no such specialness: you can perform arithmetic with
either without a warning, for example, and each converts to the other
in the appropriate circumstances. In what way is the value of !0 a
special value?

In exactly the same way as the other:

[[email protected]]~/work#perl -MDevel::peek -e 'Dump(!0), Dump(!1)'
SV = PVNV(0x817a634) at 0x817851c
REFCNT = 2147483646
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x817b618 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x817a620) at 0x817850c
REFCNT = 2147483647
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x817a610 ""\0
CUR = 0
LEN = 4

In particular, it has all the *OK flags set from the start.

I'm sorry, but I don't have any idea what that output might mean.
When I ran the same one-liners, but with $x = 1 the first time and !0
the second time, in both cases they output
6
(1)
without any messages, unlike the !1 case. So from the point of view
of the user, at least in this test there is nothing special about the
value produced by !0 -- it appears to act identically to 1 regardless
of what the perl internals happens to represent it. So, so far as I
can tell, there's nothing special about it.
Also, it is "a special truth value" because there's an infinite set
of other values which are also regarded as 'true' but this particular
one happens to be used.

I think that's a much weaker definition of "special". Yes, it could
choose an unbounded number of possible true values, and one of a few
possible false values, but there need not be anything further
unusual. In fact, for truth it does not choose a dualvar of -9 and
"Wot?!?", but an apparently ordinary 1. But !1 is special other than
being a particular fixed value.
 
E

Eric Pozharski

with said:
*SKIP*
I think that's a much weaker definition of "special". Yes, it could
choose an unbounded number of possible true values, and one of a few
possible false values, but there need not be anything further unusual.
In fact, for truth it does not choose a dualvar of -9 and "Wot?!?",
but an apparently ordinary 1.

There outght to be a module for fine tuning this.
But !1 is special other than being a particular fixed value.

That's why I don't read Rainer -- he always messes things. Probably
time will cure him and I'll enjoy his company too (probably, time will
cure me and Rainer won't be my problem anymore). Anyway:

% perl -MDevel::peek -wle 'Dump !1; Dump 0; Dump ""; Dump undef'
SV = PVNV(0x945d688) at 0x945b524
REFCNT = 2147483647
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x945d680 ""\0
CUR = 0
LEN = 12
SV = IV(0x9477f68) at 0x9477f6c
REFCNT = 1
FLAGS = (PADTMP,IOK,READONLY,pIOK)
IV = 0
SV = PV(0x945c760) at 0x947800c
REFCNT = 1
FLAGS = (PADTMP,POK,READONLY,pPOK)
PV = 0x947bfd8 ""\0
CUR = 0
LEN = 12
SV = NULL(0x0) at 0x945b510
REFCNT = 2147483608
FLAGS = (READONLY)

Do you see difference now? Then back to '!0':

% perl -MDevel::peek -wle 'Dump !0'
SV = PVNV(0x8ebb6a0) at 0x8eb9538
REFCNT = 2147483644
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x8ebc570 "1"\0
CUR = 1
LEN = 12

There's kind-of-special-perl-truth (IIRC, I've doen once something like
$count += !!$parsed; obviously '!0 + 0 == 42' would be totaly
unwelcome). However, either TRUTH or FALSE are made special inside
negation op (what renders negation op some kind of magic blender).
 
R

Rainer Weikusat

Rainer Weikusat said:
[email protected] (Tim McDaniel) said:
should also, should document the result when negating a false value,
which happens to be a 'special truth value' whose numerical value is
1 and whose string value is '1').

I'm afraid I'm not following you. The negation of a true value does
indeed have a slight difference from both normal '' and 0:

$ perl -e 'use warnings; use strict; my $x = ""; print($x + 5, "\n"); print ("($x)\n");'
Argument "" isn't numeric in addition (+) at -e line 1.
5
()

$ perl -e 'use warnings; use strict; my $x = !1; print($x + 5, "\n"); print ("($x)\n");'
5
()

But 1 or '1' has no such specialness: you can perform arithmetic with
either without a warning, for example, and each converts to the other
in the appropriate circumstances. In what way is the value of !0 a
special value?

In exactly the same way as the other:

[[email protected]]~/work#perl -MDevel::peek -e 'Dump(!0), Dump(!1)'
SV = PVNV(0x817a634) at 0x817851c
REFCNT = 2147483646
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 1
NV = 1
PV = 0x817b618 "1"\0
CUR = 1
LEN = 4
SV = PVNV(0x817a620) at 0x817850c
REFCNT = 2147483647
FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
IV = 0
NV = 0
PV = 0x817a610 ""\0
CUR = 0
LEN = 4

In particular, it has all the *OK flags set from the start.

I'm sorry, but I don't have any idea what that output might mean.
[...]

So from the point of view of the user, at least in this test there is
nothing special about the value produced by !0 -- it appears to act
identically to 1 regardless of what the perl internals happens to
represent it. So, so far as I can tell, there's nothing special about
it.

AFAICT, there's nothing special about the 'special false value', either,
it behaves just like any other empty string:

[[email protected]]~#perl -e '$v = ""; print $v + 3; $v = !"a"; print $a + 3;'
33[[email protected]]~#

....

The difference is this here (irrelevant parts deleted):

FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)

This signifies a multivalue object which is 'ok to use as integer'
(IOK), 'ok to use as floating point number' (NOK) and 'ok to use as a
string' (POK) [AFAIK, the p-prefix flag mean "privately OK ..." but I
didn't examine the source to determine what this exactly means]. This is
a property ordinary Perl scalars don't usually have, cf

[[email protected]]~#perl -MDevel::peek -e '$v = ""; Dump($v); $v += 0; Dump($v)'
SV = PV(0x81796d8) at 0x8195c80
REFCNT = 1
FLAGS = (POK,pPOK)
PV = 0x8190770 ""\0
CUR = 0
LEN = 4
SV = PVNV(0x817a9a4) at 0x8195c80
REFCNT = 1
FLAGS = (NOK,pNOK)
IV = 0
NV = 0
PV = 0x8190770 ""\0
CUR = 0
LEN = 4

and a side effect of that is that using such a 'special boolean scalar'
won't trigger warnings someone put into the type-conversion code. Such
warnings wouldn't happen for the 'special truth value' case, anyway, but
it is nevertheless constructed in the same way as the other.
I think that's a much weaker definition of "special".

It's another definition.
Yes, it could choose an unbounded number of possible true values, and
one of a few possible false values, but there need not be anything
further unusual. In fact, for truth it does not choose a dualvar of -9 and
"Wot?!?", but an apparently ordinary 1. But !1 is special other than
being a particular fixed value.

Insofar 'external appearance' goes, it's not really different from any
other Perl-scalar which doesn't require a string-to-number conversion
for a non-numeric string value anymore:

[[email protected]]~#perl -we '$v = "bla"; { no warnings; $v += 0; }; $v += 0;'
[[email protected]]~#

AFAIK, after the embedded block, $v can be used as number as anyone
might see fit, without ever triggering a warning again (provided its
value doesn't change).
 
Ad

Advertisements

R

Rainer Weikusat

[...]
That's why I don't read Rainer -- he always messes things. Probably
time will cure him and I'll enjoy his company too (probably, time will
cure me and Rainer won't be my problem anymore).

Neither the inbred assumption that whatever one doesn't understand MUST
surely be wrong (and a sign of a deranged mind) nor the habit to make
uncalled for nasty remarks about others for no particular reason does
usually become better with (even) old(er) age.

But good luck trying!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top