Strange behavior of Perl-5. Is it a bug? 
Author Message
 Strange behavior of Perl-5. Is it a bug?

I announced a strange behavior of Perl5.000 on SunOS last week, but nobody
replied. Perhaps, nobody recognized it before. I tried to strip my program
to get it very tight *and* retain the "strange" behavior. Here is it.

There are three files named test, unix.pl and mount.pl:

========File: test==========
#!/local/perl-5.0/bin/perl

$DFS= { '/m/' => { 'dfs' => { '/' => { 'process' => 'unix::process',
                                       'require' => 'unix.pl', }
                            },
                   'process' => 'mount::process',
                   'require' => 'mount.pl',
                 },
      };

sub loadDFS
{

    if( $dfs->{'require'} )
    {
        print "Loading DFS module: '$dfs->{'require'}'\n";
        require $dfs->{'require'};
    }

Quote:
}

print "Start\n";

{
    local( $dfs );

    $dfs= $DFS->{'/m/'};

    &loadDFS( $dfs );
    &{"$dfs->{'process'}"}( $dfs );

Quote:
}

print "Stop\n";

========File: mount.pl==========
package mount;

sub process
{

    print "mount::process: \$dfs='$dfs'\n";

    $mydfs= $dfs->{'dfs'}->{'/'};
    print "mount::process: \$mydfs='$mydfs'\n";
    &::loadDFS( $mydfs );

    &{"$mydfs->{'process'}"}( $mydfs );

Quote:
}

1;

========File: unix.pl==========
package unix;

sub process
{

    print "unix::process: \$dfs='$dfs'\n";

Quote:
}

1;

========END==========

Starting test outputs the following text on my machine:

Start
Loading DFS module: 'mount.pl'
mount::process: $dfs='HASH(0x918b0)'
mount::process: $mydfs='HASH(0x7f838)'
Loading DFS module: 'unix.pl'
unix::process: $dfs='unix::process: $dfs=''
Stop

The line before the last is very strange. It should be something like:

unix::process: $dfs='HASH(0x7f838)'

The reference to the HASH %$mount::mydfs is magically lost when &unix::process
is called. Most of the environment is needed to reproduce the behavior. Putting
all files together in one does produce the correct output.

Finally: Is it my fault or is it a bug in Perl5.000?

Ciao
Franz



Fri, 18 Jul 1997 22:10:34 GMT  
 Strange behavior of Perl-5. Is it a bug?

: I announced a strange behavior of Perl5.000 on SunOS last week, but nobody
: replied. Perhaps, nobody recognized it before. I tried to strip my program
: to get it very tight *and* retain the "strange" behavior. Here is it.
:
: There are three files named test, unix.pl and mount.pl:
:...
: Starting test outputs the following text on my machine:
:
: Start
: Loading DFS module: 'mount.pl'
: mount::process: $dfs='HASH(0x918b0)'
: mount::process: $mydfs='HASH(0x7f838)'
: Loading DFS module: 'unix.pl'
: unix::process: $dfs='unix::process: $dfs=''
: Stop
:
: The line before the last is very strange. It should be something like:
:
: unix::process: $dfs='HASH(0x7f838)'
:
: The reference to the HASH %$mount::mydfs is magically lost when &unix::process
: is called. Most of the environment is needed to reproduce the behavior. Putting
: all files together in one does produce the correct output.
:
: Finally: Is it my fault or is it a bug in Perl5.000?

It's a bug.  There's code in there to make eval look up lexically scoped
variables outside of the eval.  Apparently require is triggering the same
code, which is bogus, because the required code isn't in the lexical
scope of the "my".

But there's another bug here, in that the following prints "dynamic", when
it is fact printing the value of the lexical variable.  It would appear
that 5.000 silently turns the local into another "my" because $var was
already a lexical variable!  Oops.

    $var = "global\n";
    my $var = "lexical\n";
    local $var = "dynamic\n";
    print $var;
    print $::var;

I think trying to localize a lexical should probably produce a fatal error.
You can always explicitly qualify the localized global if you need to.
A case could be made for allowing the dynamic stack mechanism to be
applied to the lexical variable's value, but I think this is likely
to lead to much more confusion than it's worth.

Also, if you're inclined to think that the local declaration should hide
the lexical declaration, let me remind you that the local declaration
isn't in fact a declaration at all.  It introduces nothing into any
symbol table whatsoever.  (Unless it does so by accident because it's
the first mention.)

So I think immediate bebotherment is the best policy here.

Larry



Sat, 26 Jul 1997 03:30:07 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Some strange perl behaviors (bugs ?)

2. PERL 5 vs PERL 4 - strange socket behavior

3. Strange behavior in a Perl CGI script

4. Strange behavior using Perl's system() function to query SQL Server with osql

5. Win32::DDE::Client strange behavior, perl gives run-time error

6. Strange behavior in a Perl CGI script

7. Win32::DDE::Client strange behavior, perl run-time error

8. strange perl semop behavior

9. Strange behavior in do while loops in perl 5.00503

10. Strange behavior with Config and perl -e

11. strange typeglob behavior in Perl 5

12. Very strange behavior firing off ftp from a perl script

 

 
Powered by phpBB® Forum Software