unexpected differences in perl 5.000 
Author Message
 unexpected differences in perl 5.000

Fellow Perl user!

Below are two chunks of perl programs that differ in a unexpected way in
perl5.00 on AIX from their behavior in Per4.036. I could not find hints in the perl5
documentation, i hope that i have not missed some essential document, which
points out these differences.

In the first example

    s/SO(MET)HING/&func(\1)/e

inserts "SCALAR(0x200e48f8)" or the like into the substituted expression, whereas
$1 works as i would expect. The following perl script

========================================================
sub subs {

    print STDERR "   receive <$_>\n";
    s/-/ /g;
    print STDERR "   will return <$_>\n";
    return $_;

Quote:
}

 $test = '---(---)---';

 print STDERR "input <$test>\n";
 ($_ = $test) =~ s/(\([^\)]*\))/&subs(\1)/e;
 print STDERR "output 1 <$_>\n";
 ($_ = $test) =~ s/(\([^\)]*\))/&subs($&)/e;
 print STDERR "output 2 <$_>\n";
 ($_ = $test) =~ s/(\([^\)]*\))/&subs($1)/e;
 print STDERR "output 3 <$_>\n";
========================================================
outputs
========================================================
input <---(---)--->
   receive <SCALAR(0x200e48f8)>
   will return <SCALAR(0x200e48f8)>
output 1 <---SCALAR(0x200e48f8)--->
   receive <(---)>
   will return <(   )>
output 2 <---(   )--->
   receive <(---)>
   will return <(   )>
output 3 <---(   )--->
========================================================
where output 2 to output 3 are correct.

The second problem came from $[: the script below
========================================================
sub test1 { print "test1: min = $[, max = $#a\n"; }
$[ = 1;
sub test2 { print "test2: min = $[, max = $#a\n"; }

$a[1] = 1;
$a[2] = 2;
&test1;
&test2;
========================================================
outputs
========================================================
test1: min = 0, max = 1
test2: min = 1, max = 2
========================================================
whereas i was expecting min = 1 and max = 2 for test1 and test2.

needless to say, in perl 4.036 \1 and $1 worked identical in s///e,
the results for test1 and test2 were identical.

Other than that, i am very impressed by perl5.

Gustaf Neumann
--

Postdoctoral/Visiting Scientist    Tel: (914) 784 7086
IBM T.J.Watson Research Center, P.O.Box 704
Yorktown Heights, New York 10598



Thu, 01 May 1997 09:49:46 GMT  
 unexpected differences in perl 5.000
: Fellow Perl user!
:
: Below are two chunks of perl programs that differ in a unexpected way in
: perl5.00 on AIX from their behavior in Per4.036. I could not find hints
: in the perl5 documentation, i hope that i have not missed some essential
: document, which points out these differences.
:
: In the first example
:
:     s/SO(MET)HING/&func(\1)/e
:
: inserts "SCALAR(0x200e48f8)" or the like into the substituted expression,
: whereas $1 works as i would expect.

Use of \1 was already discouraged in Perl 4's manpage:

     (Outside of the pattern, always use $ instead of \ in front of the
     digit.  The scope  of  $<digit> (and  $`,  $&  and  $')  extends
     to the end of the enclosing BLOCK or eval string, or to  the
     next  pattern  match  with subexpressions.   The \<digit> notation
     sometimes works outside the current pattern, but should not be
     relied upon.)

I suspect I'll leave this one "busted", since backslash now has a real
meaning in ordinary code.  Maybe the user really *does* want a reference
to integer 1.  Perhaps a -w warning is in order though.  Oddly enough,
without the /e modifier, you would get a warning:

    \1 better written as $1 at - line 1.

: The second problem came from $[: the script below
: ========================================================
: sub test1 { print "test1: min = $[, max = $#a\n"; }
: $[ = 1;
: sub test2 { print "test2: min = $[, max = $#a\n"; }
:
: $a[1] = 1;
: $a[2] = 2;
: &test1;
: &test2;
: ========================================================
: outputs
: ========================================================
: test1: min = 0, max = 1
: test2: min = 1, max = 2
: ========================================================
: whereas i was expecting min = 1 and max = 2 for test1 and test2.

In Perl 5, setting $[ is now treated as a lexically scoped compiler
directive.  This will break (intentionally) any code that relies on the
dynamic evaluation of $[, such as that above.  If you want a file to
use an arraybase of 1, I'd recommend setting $[ = 1 right up at the
top, and then don't touch $[ again.

The point of this change was to make it so that one module's setting
of $[ doesn't louse up the semantics of some other module.  There were
a lot of Perl 4 library routines that had to put

    local($[) = 0;

on entrance to every subroutine.  This practice I deemed too ugly to live.

: Other than that, i am very impressed by perl5.

Well, thanks.  Many brain-years have gone into Perl 5, some of them mine.

Larry



Tue, 06 May 1997 04:19:20 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. perl 5.000 v. 5.001 difference

2. PERL 5.000 bug report (FORMATs from 4.036 to 5.000)

3. Unexpected behavior of Perl classes

4. Perl 5.000 failed make test on NeXT

5. Building Perl 5.000 on NEXTSTEP/HP

6. Perl 5.000, DLD, Linux

7. Perl 5.000 on Pyramid MIS/T

8. linked list class in Perl 5.000

9. reporting build success Re: ANNOUNCE: tkperl5alpha5, the Tk extension to Perl 5.000

10. ERROR Compiling Perl 5.000 on Solaris 2.3

11. Perl 5.000 for Linux??

12. perl 5.000 defect: core dump

 

 
Powered by phpBB® Forum Software