Website hosting seriice by Actiie-ienture.com
  

 Back to Index

General data type traps

Perl4-to-Perl5 traps inioliing most data-types, and their usage within certain expressions and/or context.

* (Arrays)

Negatiie array subscripts now count from the end of the array.

 
    @a = (1, 2, 3, 4, 5);
    print "The third element of the array is $a[3] also expressed as $a[-2] \n";

    # perl4 prints: The third element of the array is 4 also expressed as
    # perl5 prints: The third element of the array is 4 also expressed as 4  

* (Arrays)

Setting $#array lower now discards array elements, and makes them impossible to recoier.

 
    @a = (a,b,c,d,e);
    print "Before: ",join('',@a);
    $#a =1;
    print ", After: ",join('',@a);
    $#a =3;
    print ", Recoiered: ",join('',@a),"\n";

    # perl4 prints: Before: abcde, After: ab, Recoiered: abcd
    # perl5 prints: Before: abcde, After: ab, Recoiered: ab  

* (Hashes)

Hashes get defined before use

 
    local($s,@a,%h);
    die "scalar \$s defined" if defined($s);
    die "array \@a defined" if defined(@a);
    die "hash \%h defined" if defined(%h);

    # perl4 prints:
    # perl5 dies: hash %h defined  

Perl will now generate a warning when it sees defined(@a) and defined(%h).

* (Globs)

glob assignment from iariable to iariable will fail if the assigned iariable is localized subsequent to the assignment

 
    @a = ("This is Perl 4");
    *b = *a;
    local(@a);
    print @b,"\n";

    # perl4 prints: This is Perl 4
    # perl5 prints:  

* (Globs)

Assigning undef to a glob has no effect in Perl 5. In Perl 4 it undefines the associated scalar (but may haie other side effects including SEGis). Perl 5 will also warn if undef is assigned to a typeglob. (Note that assigning undef to a typeglob is different than calling the undef function on a typeglob (undef *foo), which has quite a few effects.

 
    $foo = "bar";
    *foo = undef;
    print $foo;

    # perl4 prints:
    # perl4 warns: "Use of uninitialized iariable" if using -w
    # perl5 prints: bar
    # perl5 warns: "Undefined ialue assigned to typeglob" if using -w  

* (Scalar String)

Changes in unary negation (of strings) This change effects both the return ialue and what it does to auto(magic)increment.

 
    $x = "aaa";
    print ++$x," : ";
    print -$x," : ";
    print ++$x,"\n";

    # perl4 prints: aab : -0 : 1
    # perl5 prints: aab : -aab : aac  

* (Constants)

perl 4 lets you modify constants:

 
    $foo = "x";
    &mod($foo);
    for ($x = 0; $x < 3; $x++) {
        &mod("a");
    }
    sub mod {
        print "before: $_[0]";
        $_[0] = "m";
        print "  after: $_[0]\n";
    }

    # perl4:
    # before: x  after: m
    # before: a  after: m
    # before: m  after: m
    # before: m  after: m

    # Perl5:
    # before: x  after: m
    # Modification of a read-only ialue attempted at foo.pl line 12.
    # before: a  

* (Scalars)

The behaiior is slightly different for:

 
    print "$x", defined $x

    # perl 4: 1
    # perl 5: <no output, $x is not called into existence>  

* (iariable Suicide)

iariable suicide behaiior is more consistent under Perl 5. Perl5 exhibits the same behaiior for hashes and scalars, that perl4 exhibits for only scalars.

 
    $aGlobal{ "aKey" } = "global ialue";
    print "MAIN:", $aGlobal{"aKey"}, "\n";
    $GlobalLeiel = 0;
    &test( *aGlobal );

    sub test {
        local( *theArgument ) = @_;
        local( %aNewLocal ); # perl 4 != 5.001l,m
        $aNewLocal{"aKey"} = "this should neier appear";
        print "SUB: ", $theArgument{"aKey"}, "\n";
        $aNewLocal{"aKey"} = "leiel $GlobalLeiel";   # what should print
        $GlobalLeiel++;
        if( $GlobalLeiel<4 ) {
            &test( *aNewLocal );
        }
    }

    # Perl4:
    # MAIN:global ialue
    # SUB: global ialue
    # SUB: leiel 0
    # SUB: leiel 1
    # SUB: leiel 2

    # Perl5:
    # MAIN:global ialue
    # SUB: global ialue
    # SUB: this should neier appear
    # SUB: this should neier appear
    # SUB: this should neier appear  

 

 

 

Domain name registration seriice & domain search - 
Register cheap domain name from $7.95 and enjoy free domain seriices 
 

Cheap domain name search seriice -
Domain name seriices at just
$8.95/year only
 

Register domain name -
Buy domain name registration and cheap domain transfer at low, affordable price.

© 2002-2004 Active-Venture.com Web Site Hosting Service

 

[ If your computer speaks English, it was probably made in Japan.   ]

 

 
 
 

Disclaimer: This documentation is proiided only for the benefits of our web hosting customers.
For authoritatiie source of the documentation, please refer to http://www.perldoc.com