Ain't ($foo eq 'abc') the same as ($foo =~ /^abc$/)? Ha - NOT ON MY BOX!!!

Discussion in 'Perl Misc' started by usenet@DavidFilmer.com, Jun 15, 2005.

  1. Guest

    I'm beginning to think my Perl has gone crazy with the way it uses
    modules. First, the strange situation that I posted on
    http://tinyurl.com/8zvet (sorry, I'm behind a corporate firewall and
    I'm stuck with Google:80 for Usenet).

    Now I'm doing a bit of database stuff. Consider this simple example:

    #!/usr/bin/perl -w
    use DBIx::Simple;
    use strict;
    my $dbh = DBIx::Simple -> connect("dbi:DB2:testdb2", 'userid', 'pw');
    my $sql = "select TS_LOAD from TESTDB2.SUPLMODL order by TS_LOAD";
    my $sql_timestamp = '2005-06-03 09.36.41.849402'; #ie, 26-byte DB2

    my @bar = $dbh -> query($sql) -> flat();
    foreach my $foo(@bar) {
    if ($foo eq $sql_timestamp) {
    print "'$foo'\n'$sql_timestamp'\n"; #for diagnostic
    }
    }

    The "if" condition will never evaluate true. However, if I change it:

    if ($foo =~ /^$sql_timestamp$/) {

    then it will work! The diagnostic "print" stmt says (as expected):
    '2005-06-03 09:36:41.849402'
    '2005-06-03 09.36.41.849402'

    THIS IS INSANE. The statement:
    if ($foo eq $sql_timestamp) {
    should be functionally IDENTICAL to
    if ($foo =~ /^$sql_timestamp$/) {
    RIGHT?????

    Perl seems to have gone completely crazy!

    Does anyone have ANY idea what could possibly be going on here? This is
    driving me nuts.

    I'm using Perl 5.8.4 built for AIX 5.1MR2. DBIx::Simple is 1.26, FWIW.

    Thanks for looking!
    , Jun 15, 2005
    #1
    1. Advertising

  2. wrote:
    > THIS IS INSANE. The statement:
    > if ($foo eq $sql_timestamp) {
    > should be functionally IDENTICAL to
    > if ($foo =~ /^$sql_timestamp$/) {
    > RIGHT?????


    Not if $sql_timestamps contains characters that have a special meaning in
    regular expressions.

    jue
    Jürgen Exner, Jun 15, 2005
    #2
    1. Advertising

  3. Jay Tilton Guest

    wrote:

    : #!/usr/bin/perl -w
    : use DBIx::Simple;
    : use strict;
    : my $dbh = DBIx::Simple -> connect("dbi:DB2:testdb2", 'userid', 'pw');
    : my $sql = "select TS_LOAD from TESTDB2.SUPLMODL order by TS_LOAD";
    : my $sql_timestamp = '2005-06-03 09.36.41.849402'; #ie, 26-byte DB2
    :
    : my @bar = $dbh -> query($sql) -> flat();
    : foreach my $foo(@bar) {
    : if ($foo eq $sql_timestamp) {
    : print "'$foo'\n'$sql_timestamp'\n"; #for diagnostic
    : }
    : }
    :
    : The "if" condition will never evaluate true. However, if I change it:
    :
    : if ($foo =~ /^$sql_timestamp$/) {
    :
    : then it will work! The diagnostic "print" stmt says (as expected):
    : '2005-06-03 09:36:41.849402'
    : '2005-06-03 09.36.41.849402'
    ^ ^
    ^ ^

    Those two strings are not identical. "$foo eq $sql_timestamp" will be
    false.

    But "." in $sql_timestamp, when used as a regex, is allowed to match the
    ":" characters in $foo .

    : THIS IS INSANE. The statement:
    : if ($foo eq $sql_timestamp) {
    : should be functionally IDENTICAL to
    : if ($foo =~ /^$sql_timestamp$/) {
    : RIGHT?????

    Nope.
    Jay Tilton, Jun 15, 2005
    #3
  4. Guest

    Re: Ain't ($foo eq 'abc') the same as ($foo =~ /^abc$/)? Ha - NOT ON MY BOX!!!

    >>> They are not equal. Look closer at the time.
    >>> '2005-06-03 09:36:41.849402'
    >>> '2005-06-03 09.36.41.849402'


    and

    >>> But "." in $sql_timestamp, when used as a regex, is allowed to match the ":" characters in $foo .


    Crap, You're right! And it just so happened the use of dots allowed it
    to match in the regex (what are the odds?)

    I feel stupid - I looked at that for TWO HOURS and didn't notice the
    difference in the strings. Always good to have a fresh set of eyes
    look at something! Thanks everyone!
    , Jun 15, 2005
    #4
  5. Guest

    Re: Ain't ($foo eq 'abc') the same as ($foo =~ /^abc$/)? Ha - NOT ON MY BOX!!!

    wrote:
    > >>> They are not equal. Look closer at the time.
    > >>> '2005-06-03 09:36:41.849402'
    > >>> '2005-06-03 09.36.41.849402'

    >
    > and
    >
    > >>> But "." in $sql_timestamp, when used as a regex, is allowed to match
    > >>> the ":" characters in $foo .

    >
    > Crap, You're right! And it just so happened the use of dots allowed it
    > to match in the regex (what are the odds?)
    >
    > I feel stupid - I looked at that for TWO HOURS and didn't notice the
    > difference in the strings. Always good to have a fresh set of eyes
    > look at something! Thanks everyone!


    For future reference, this is very handy in such situations:
    print "$_\n" foreach map ord $_, split //, $string;

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Jun 15, 2005
    #5
    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. Jiong Feng
    Replies:
    0
    Views:
    813
    Jiong Feng
    Nov 19, 2003
  2. Replies:
    3
    Views:
    382
    John Roth
    Jul 29, 2005
  3. Dave Kuhlman
    Replies:
    15
    Views:
    534
    Bruno Desthuilliers
    Mar 14, 2008
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    389
    JaredGrubb
    Sep 24, 2008
  5. Replies:
    4
    Views:
    140
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page