[Moscow.pm] Почему код так работает?

Vladimir V. Perepelitsa inthrax на gmail.com
Пт Май 30 07:57:53 PDT 2008


On Friday 30 May 2008 18:57:14 Andrew Shitov wrote:
> А как это воспроизвести?
>
> Linux:
>
> main: SCALAR(0x8149868) at 1.pl line 5.
> func1: SCALAR(0x8149868) at 1.pl line 8.
> func2: SCALAR(0x8149868) at 1.pl line 10.
> func2: SCALAR(0x8149868) at 1.pl line 10.
> func2: SCALAR(0x8149868) at 1.pl line 10.
> func2: SCALAR(0x8149868) at 1.pl line 10.
> func2: SCALAR(0x8149868) at 1.pl line 10.
> func2: SCALAR(0x8149868) at 1.pl line 10.

хм...

$ perl
use strict;
use warnings;

my $test = 10;
warn 'main: ', \$test;

my $func1 = sub {
     warn 'func1: ', \$test;
     sub func2 {
         warn 'func2: ', \$test;
     }
};

$func1->();
func2();
func2();
func2();
func2();
func2();
func2();
main: SCALAR(0x815b834) at - line 5.
func1: SCALAR(0x815b834) at - line 8.
func2: SCALAR(0x819a000) at - line 10.
func2: SCALAR(0x8199fdc) at - line 10.
func2: SCALAR(0x819a000) at - line 10.
func2: SCALAR(0x8199fdc) at - line 10.
func2: SCALAR(0x819a000) at - line 10.
func2: SCALAR(0x8199fdc) at - line 10.

------
$ perl -V
Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
  Platform:
    osname=linux, osvers=2.6.24-tuxonice-r2-undef, 
archname=i686-linux-thread-multi
    uname='linux undef 2.6.24-tuxonice-r2-undef #7 smp mon mar 24 22:01:54 msk 
2008 i686 intel(r) celeron(r) cpu 2.50ghz genuineintel gnulinux '
    
config_args='-des -Darchname=i686-linux-thread -Dcccdlflags=-fPIC -Dccdlflags=-rdynamic -Dcc=i686-pc-linux-gnu-gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth=  -Doptimize=-O2 -march=pentium4 -fomit-frame-pointer -pipe -Duselargefiles -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dinstallman1dir=/usr/share/man/man1 -Dinstallman3dir=/usr/share/man/man3 -Dman1ext=1 -Dman3ext=3pm -Dinc_version_list=5.8.0 
5.8.0/i686-linux-thread-multi 5.8.2 5.8.2/i686-linux-thread-multi 5.8.4 
5.8.4/i686-linux-thread-multi 5.8.5 5.8.5/i686-linux-thread-multi 5.8.6 
5.8.6/i686-linux-thread-multi 5.8.7 
5.8.7/i686-linux-thread-multi  -Dcf_by=Gentoo -Ud_csh -Dusenm -Dusethreads -Ui_ndbm -Ui_gdbm -Di_db'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define 
usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='i686-pc-linux-gnu-gcc', ccflags 
='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -Wdeclaration-after-statement -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
    optimize='-O2 -march=pentium4 -fomit-frame-pointer -pipe',
    
cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -fno-strict-aliasing -pipe -Wdeclaration-after-statement -I/usr/include/gdbm'
    ccversion='', gccversion='4.2.3 (Gentoo 4.2.3 p1.0)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', 
lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='i686-pc-linux-gnu-gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lpthread -lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
    perllibs=-lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.7.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.7'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'


Characteristics of this binary (from libperl):
  Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT
                        PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_ITHREADS
                        USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
  Built under linux
  Compiled at May 12 2008 12:16:56
  @INC:
    /etc/perl
    /usr/lib/perl5/vendor_perl/5.8.8/i686-linux-thread-multi
    /usr/lib/perl5/vendor_perl/5.8.8
    /usr/lib/perl5/vendor_perl
    /usr/lib/perl5/site_perl/5.8.8/i686-linux-thread-multi
    /usr/lib/perl5/site_perl/5.8.8
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/5.8.8/i686-linux-thread-multi
    /usr/lib/perl5/5.8.8
    /usr/local/lib/site_perl



-- 
Vladimir Perepelitsa aka Mons Anderson
<inthrax на gmail.com> / #99779956


Подробная информация о списке рассылки Moscow-pm