Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here's a minimal test case that reliably reproduces a problem I've been having. As I understand perl, the three sets of print statements ought to produce the same output each time.

use strict; no warnings "numeric"; print "Reusing one variable:\n"; my $val = "ff"; print "(" . ($val & "") . ")\n"; $val = 7; print "(" . ($val & "") . ")\n"; $val = "ff"; print "(" . ($val & "") . ")\n"; print "\n"; print "Using multiple variables:\n"; my $v1 = "ff"; print "(" . ($v1 & "") . ")\n"; my $v2 = 7; print "(" . ($v2 & "") . ")\n"; my $v3 = "ff"; print "(" . ($v3 & "") . ")\n"; print "\n"; print "Using a sub:\n"; sub xx { my $arg = shift; print "(" . ($arg & "") . ")\n"; } xx("ff"); xx(7); xx("ff");

Since xx has no side-effects except for output, there should be no state maintained between multiple calls to it.

However, what this code actually does seems to be depend on what version I'm running it under:

  • Under 5.16.3 i386-openbsd and 5.14.2 x86_64-linux-gnu-thread-multi, it does what I'd expect, giving the output:
    Reusing one variable: () (0) () Using multiple variables: () (0) () Using a sub: () (0) ()
  • Under versions before 5.14, though, I get a different result (tested on v5.10.0 darwin-thread-multi-2level, 5.10.1 i486-linux-gnu-thread-multi and 5.12.4 darwin-thread-multi-2level, which are the versions I currently can get access to):
    Reusing one variable: () (0) () Using multiple variables: () (0) () Using a sub: () (0) (0)

The output of the last set of prints no longer matches the previous two sets, which suggests that there's some state being preserved between calls to the sub. It looks a bit like $arg's numeric status is somehow getting "stuck".

Putting a print inside the subroutine that prints $arg verifies that $arg is receiving the right value. Putting intermediate calls to other subroutines, or other code, in between the calls to xx doesn't seem to change anything, either.

I've trudged my way through the perldeltas between 5.12 and 5.14 and I couldn't see anything relevant, but then I don't really know what it'd come under, and I don't know much about the innards of perl, so it's possible I've completely missed something.

Can anyone shed some light on why this is happening?


In reply to Mystery state (possibly about whether an argument is numeric?) being maintained across calls to a sub by cheesestraws

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (7)
    As of 2018-05-27 16:44 GMT
    Find Nodes?
      Voting Booth?