cr8josh has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a case where by adding or removing a simple print statement, the results of my program change. My program is using OLE to set colors of certain text in Visio. Here is the code, I am using Perl 5.8.3

while ($text =~ /($pattern)/g) { my $startindex = $-[0]; my $endindex = $+[0]; my $capture = $1; print "$startindex $endindex\n"; ####REQUIRED OR WON'T WORK CO +RRECTLY! my $vsoChars = $VShape->Characters; $vsoChars->{Begin}=$startindex; $vsoChars->{End}=$endindex; $vsoChars->{Text}=$capture; $vsoChars->SetProperty('CharProps', 1, $color); }

I capture the start and end location of my found string into '$startindex' and '$endindex'. What I have found is that if I comment out the print statement, the {Begin} and {End} properties don't set correctly. With the print statement, everything works perfectly. Without it, it doesn't. I can't just print a carraige return or space or something else, I have to actually print the $startindex and $endindex values. I have verified that the data in $startindex and $endindex is the same (and correct) with and without the print statement. Has anyone ever seen anything like this?

Thanks in advance!

Replies are listed 'Best First'.
Re: print statement changes results!
by BrowserUk (Patriarch) on Jun 06, 2011 at 17:26 UTC

    My guess is that if you do:

    while( $text =~ /($pattern)/g ) { my $startindex = $-[0]; my $endindex = $+[0]; my $capture = $1; my $vsoChars = $VShape->Characters; $vsoChars->{Begin} = "$startindex"; $vsoChars->{End} = "$endindex"; $vsoChars->{Text} = $capture; $vsoChars->SetProperty( 'CharProps', 1, $color ); }

    or even:

    while( $text =~ /($pattern)/g ) { my $vsoChars = $VShape->Characters; $vsoChars->{Begin} = "$-[0]"; $vsoChars->{End} = "$+[0]"; $vsoChars->{Text} = $1; $vsoChars->SetProperty( 'CharProps', 1, $color ); }

    they will also work.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Both worked. Thank you!
        Sounds like some XS code is accessing the string buffer of the scalar whether the scalar holds a string or not. That code should be coercing the value of the scalar into a string if the scalar doesn't contain one.
        A reply falls below the community's threshold of quality. You may see it by logging in.
      So you think one has to use the stringified version of the value?

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        The values return from $-[0]/$+[0] are integers stored in the IV part of the scalar.

        Interpolating them in the print statement will have the side effect of forcing the PV slots to be populated with the stringified versions of those integers.

        If the XS code that is behind this VShape object is only looking at the PV slot, that would explain why the print statement has the effect of making the code work.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: print statement changes results!
by toolic (Bishop) on Jun 06, 2011 at 17:26 UTC

      How would buffering affect this?

        It was just a stab in the dark. Clearly, since the OP's problem has been solved, buffering has nothing to do with it. The OP presented a snippet of code with no input data or output data, and, in my opinion, a vague description of a mysterious print-related issue. I took an educated guess that the OP maybe wasn't showing us more print statements.

        I figured maybe trying to flush might at least eliminate that as a possibility. I'm just trying to help. And I'm here to learn.