Perl / perl5

🐪 The Perl programming language
https://dev.perl.org/perl5/
Other
1.88k stars 530 forks source link

qw and x operator #763

Closed p5pRT closed 20 years ago

p5pRT commented 24 years ago

Migrated from rt.perl.org#1687 (status was 'resolved')

Searchable as RT1687$

p5pRT commented 24 years ago

From marcel.grunauer@lovely.net

I've come across a behavior of the qw() operator in conjunction with the x operator that I can't explain. I've checked perlop.

#!/usr/bin/perl -w use strict; my %test; @​test{qw/a b c/} = ('n') x 3; print "$_ => $test{$_}\n" foreach keys %test;

produces\, as expected​:

a => n b => n c => n

If I replace the hash slice line above with

@​test{qw/a b c/} = qw/n/ x 3;

it produces​:

Bareword "x" not allowed while "strict subs" in use at ... Unquoted string "x" may clash with future reserved word at ... syntax error at ...\, near "qw/n/ x " Number found where operator expected at ...\, near "x 3"   (Do you need to predeclare x?)

The following two work ok​:

@​test{qw/a b c/} = (qw/n/) x 3; @​test{qw/a b c/} = (split(' '\, q/n/)) x 3;

According to Tad McClellan (tadmc@​metronet.com)​:

Note also that you get the same messages if you replace the line above with just​:

x 3;

Looks like perl is taking "@​test{qw/a b c/} = qw/n/" as an entire statement\, and then resuming the parse...

According to Abigail (abigail@​delanet.com)​:

Based on [the repetition operator documentation]\, I would assume that

@​test\{qw/a b c/\} = qw/n/ x 3;

would lead to​:

%test = \(a => '111'\, b => undef\, c => undef\);

This is because the left operand of x will be evaluated in scalar context\, hence the split qw// does is done to @​_\, and the amount of produced fields is returned. This being 1\, leads to 1 x 3 which is '111'.


Site configuration information for perl 5.00502​:

Summary of my perl5 (5.0 patchlevel 5 subversion 02) configuration​:   Platform​:   osname=MSWin32\, osvers=4.0\, archname=MSWin32-x86-object   uname=''   hint=recommended\, useposix=true\, d_sigaction=undef   usethreads=undef useperlio=undef d_sfio=undef   Compiler​:   cc='cl.exe'\, optimize='-O2 -MD -DNDEBUG -TP -GX'\, gccversion=   cppflags='-DWIN32'   ccflags ='-O2 -MD -DNDEBUG -TP -GX -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DPERL_OBJECT'   stdchar='char'\, d_stdstdio=define\, usevfork=false   intsize=4\, longsize=4\, ptrsize=4\, doublesize=8   d_longlong=undef\, longlongsize=8\, d_longdbl=define\, longdblsize=10   alignbytes=8\, usemymalloc=n\, prototype=define   Linker and Libraries​:   ld='link'\, ldflags ='-nologo -nodefaultlib -release -machine​:x86'   libpth="C​:\usr\lib\core" "C​:\Program Files\Mts\Lib"   libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib PerlCRT.lib   libc=C​:\usr\lib\core\PerlCRT.lib\, so=dll\, useshrplib=yes\, libperl=perlcore.lib   Dynamic Linking​:   dlsrc=dl_win32.xs\, dlext=dll\, d_dlsymun=undef\, ccdlflags=' '   cccdlflags=' '\, lddlflags='-dll -nologo -nodefaultlib -release -machine​:x86'

Locally applied patches​:   ACTIVEPERL_LOCAL_PATCHES_ENTRY


@​INC for perl 5.00502​:   C​:\root\perldev\lib   C​:\usr\lib   C​:\usr\site\lib   .


Environment for perl 5.00502​:   HOME=C​:\root   LANG (unset)   LD_LIBRARY_PATH (unset)   LOGDIR (unset)

PATH=c​:\cygnus\CYGWIN~1\H-I586~1\bin;.;C​:\usr\bin;C​:\WINNT\system32;C​:\WINNT;C​:\rootbin;c​:\cygnus\CYGWIN~1\H-I586~1\bin;G​:\jdk1.2.2\bin;c​:\Program Files\Winzip;C​:\DAYNA   PERL5LIB=C​:\root\perldev\lib   PERL_BADLANG (unset)   SHELL=/bin/sh

-- Marcel\, Perl Padawan sub AUTOLOAD{$_=$AUTOLOAD;s;.*​::;;;y;_; ;;print}&Just_Another_Perl_Hacker;