Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Side effects of comparison on numeric string's type #475

Closed
p5pRT opened this issue Sep 4, 1999 · 2 comments
Closed

Side effects of comparison on numeric string's type #475

p5pRT opened this issue Sep 4, 1999 · 2 comments

Comments

@p5pRT
Copy link

p5pRT commented Sep 4, 1999

Migrated from rt.perl.org#1316 (status was 'resolved')

Searchable as RT1316$

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 1999

From merriam@world.std.com

Hello again,

1. Thanks for the answer to "$a+++$b" issue. All it needs is
documentation. Thanks again.

2. It's cool that ++ works on serial number strings. There is
a wierd type switching here. If I make an implicit numeric conversion
for use in an expression, the state of the variable is changed and
causes
headaches at the next ++ operator. Here's an example,

$x = "0000"; # ok, a string.
print "x = $x "; # "0000"
$x++;
print "x = $x "; # "0001"
$x++;
print "x = $x "; # "0002"
$y = $x > 20; # side effect here...
print "x = $x "; # "0002" still.
$x++; # trigger to effect here...
print "x = $x "; # "3"
$x++;
print "x = $x "; # "4"
print "\nOutput was 0000 0001 0002 0002 3 4\n\n";

and the variants.

$x = "0000"; # ok, a string.
print $x;
$y = ($x) > 20; # side effect here...
print "x = $x ";
$x++; # trigger to effect here...
print "x = $x ";
print "\nOutput was 0000 0000 1\n\n";

$x = "0000"; # ok, a string.
print "x = $x ";
$z = $x;
$y = $z > 20; # Of course, $x doesn't change.
print "x = $x ";
$x++;
print "x = $x ";
print "\nOutput was 0000 0000 0001\n\n";

Why? It doesn't seem like desirable behavior.

Thank you again for your patience,

Charles Merriam
merriam@​world.std.com
408 773 8824


Site configuration information for perl 5.00503​:

Summary of my perl5 (5.0 patchlevel 5 subversion 03) configuration​:
  Platform​:
  osname=MSWin32, osvers=4.0, archname=MSWin32-x86-object
  uname=''
  hint=recommended, useposix=true, d_sigaction=undef
  usethreads=undef useperlio=undef d_sfio=undef
  Compiler​:
  cc='cl.exe', optimize='-O2 -MD -DNDEBUG -TP -GX', gccversion=
  cppflags='-DWIN32'
  ccflags ='-O2 -MD -DNDEBUG -TP -GX -DWIN32 -D_CONSOLE -DNO_STRICT
-DHAVE_DES_FCRYPT -DPERL_OBJECT'
  stdchar='char', d_stdstdio=define, usevfork=false
  intsize=4, longsize=4, ptrsize=4, doublesize=8
  d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10
  alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries​:
  ld='link', ldflags ='-nologo -nodefaultlib -release -machine​:x86'
  libpth="C​:\Perl\lib\core"
  libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib
comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib
netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib version.lib
odbc32.lib odbccp32.lib PerlCRT.lib
  libc=C​:\Perl\lib\CORE\PerlCRT.lib, so=dll, useshrplib=yes,
libperl=perlcore.lib
  Dynamic Linking​:
  dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
  cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release
-machine​:x86'

Locally applied patches​:
  ACTIVEPERL_LOCAL_PATCHES_ENTRY


@​INC for perl 5.00503​:
  C​:/Perl/lib
  C​:/Perl/site/lib
  .


Environment for perl 5.00503​:
  HOME (unset)
  LANG (unset)
  LANGUAGE (unset)
  LD_LIBRARY_PATH (unset)
  LOGDIR (unset)

PATH=C​:\PERL\BIN;C​:\WINDOWS;C​:\WINDOWS;C​:\WINDOWS\COMMAND;C​:\VISUALCAFE\BIN;C​:\VISUALCAFE\JAVA\BIN

  PERL_BADLANG (unset)
  SHELL (unset)

@p5pRT
Copy link
Author

p5pRT commented Sep 4, 1999

From [Unknown Contact. See original ticket]

On Sat, 04 Sep 1999 11​:23​:53 -0700, C M <merriam@​world.std.com> wrote​:

2. It's cool that ++ works on serial number strings. There is
a wierd type switching here. If I make an implicit numeric conversion
for use in an expression, the state of the variable is changed and
causes
headaches at the next ++ operator. Here's an example,

That's documented behavior (see perlop.pod)​:

| The auto-increment operator has a little extra builtin magic to it. If
| you increment a variable that is numeric, or that has ever been used in
| a numeric context, you get a normal increment. If, however, the
| variable has been used in only string contexts since it was set, and has
| a value that is not null and matches the pattern `/^[a-zA-Z]*[0-9]*$/',
| the increment is done as a string, preserving each character within its
| range, with carry​:

$x = "0000"; # ok, a string.
print "x = $x "; # "0000"
$x++;
print "x = $x "; # "0001"
$x++;
print "x = $x "; # "0002"
$y = $x > 20; # side effect here...

Yes, using $x in numeric context. Maybe you want

  $y = $x gt "0020"; # no side effect here...

print "x = $x "; # "0002" still.
$x++; # trigger to effect here...
print "x = $x "; # "3"
$x++;
print "x = $x "; # "4"
print "\nOutput was 0000 0001 0002 0002 3 4\n\n";

and the variants.

[snip]

Why? It doesn't seem like desirable behavior.

I would agree. I don't like this behavior much, but it has been in Perl
for a very long time and cannot be changed. This is something one must
simply accept and move on. Besides, there are people who find that the
usefulness of the feature does compensate for it's negative attributes
(action at a distance, reliance on internal caching behavior etc.).

-Jan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant