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

U

usenet

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!
 
J

Jürgen Exner

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

Jay Tilton

(e-mail address removed) 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.
 
U

usenet

They are not equal. Look closer at the time.

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!
 
X

xhoster

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top