A question about grep

Discussion in 'Perl Misc' started by Martin Lee, May 16, 2014.

  1. Martin Lee

    Martin Lee Guest

    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!
     
    Martin Lee, May 16, 2014
    #1
    1. Advertising

  2. Martijn Lievaart <> writes:
    > On Thu, 15 May 2014 23:27:36 -0700, Martin Lee wrote:


    [...]


    >> 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"?
     
    Rainer Weikusat, May 16, 2014
    #2
    1. Advertising

  3. On 2014-05-16 10:57, Rainer Weikusat <> wrote:
    > Martijn Lievaart <> writes:
    >> '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 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


    --
    _ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
    |_|_) | | Man feilt solange an seinen Text um, bis
    | | | | die Satzbestandteile des Satzes nicht mehr
    __/ | http://www.hjp.at/ | zusammenpaƟt. -- Ralph Babel
     
    Peter J. Holzer, May 17, 2014
    #3
  4. Martin Lee

    Martin Lee Guest


    > 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!
     
    Martin Lee, May 17, 2014
    #4
  5. "Peter J. Holzer" <> writes:
    > On 2014-05-16 10:57, Rainer Weikusat <> wrote:
    >> Martijn Lievaart <> writes:
    >>> '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 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 ...
     
    Rainer Weikusat, May 18, 2014
    #5
  6. Martijn Lievaart <> writes:
    > On Fri, 16 May 2014 11:57:11 +0100, Rainer Weikusat wrote:
    >> Martijn Lievaart <> writes:
    >>> On Thu, 15 May 2014 23:27:36 -0700, Martin Lee wrote:

    >>
    >> [...]
    >>
    >>>> 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"?
     
    Rainer Weikusat, May 18, 2014
    #6
  7. Martin Lee

    Tim McDaniel Guest

    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.

    In article <>,
    Rainer Weikusat <> wrote:
    >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?

    --
    Tim McDaniel,
     
    Tim McDaniel, May 18, 2014
    #7
  8. (Tim McDaniel) writes:
    > In article <>,
    > Rainer Weikusat <> wrote:
    >>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:

    [rw@sable]~/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

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

    would just be as possible.
     
    Rainer Weikusat, May 19, 2014
    #8
  9. Martin Lee

    Tim McDaniel Guest

    In article <>,
    Rainer Weikusat <> wrote:
    > (Tim McDaniel) writes:
    >> In article <>,
    >> Rainer Weikusat <> wrote:
    >>>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:
    >
    >[rw@sable]~/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.

    --
    Tim McDaniel,
     
    Tim McDaniel, May 20, 2014
    #9
  10. with <llerkc$o7f$> Tim McDaniel wrote:
    > In article <>,
    > Rainer Weikusat <> wrote:


    *SKIP*
    >>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.


    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).

    --
    Torvalds' goal for Linux is very simple: World Domination
    Stallman's goal for GNU is even simpler: Freedom
     
    Eric Pozharski, May 21, 2014
    #10
  11. (Tim McDaniel) writes:
    > Rainer Weikusat <> wrote:
    >> (Tim McDaniel) writes:
    >>> In article <>,
    >>> Rainer Weikusat <> wrote:
    >>>>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:
    >>
    >>[rw@sable]~/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:

    [rw@sable]~#perl -e '$v = ""; print $v + 3; $v = !"a"; print $a + 3;'
    33[rw@sable]~#

    ....

    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

    [rw@sable]~#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.

    >>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".


    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:

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

    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).
     
    Rainer Weikusat, May 21, 2014
    #11
  12. Eric Pozharski <> writes:

    [...]

    > 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!
     
    Rainer Weikusat, May 21, 2014
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,496
    danpres2k
    Aug 25, 2003
  2. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    464
    John Jardine
    Apr 13, 2004
  3. Replies:
    6
    Views:
    316
    Larry Bates
    Mar 12, 2007
  4. NetComrade

    array question (grep -v on array)

    NetComrade, Jul 28, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    130
    Brian McCauley
    Jul 28, 2003
  5. googler

    Question on grep and reading from file

    googler, Aug 8, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    127
Loading...

Share This Page