Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re^2: Pearls (not really) of Perl programming

by ikegami (Pope)
on Nov 24, 2004 at 19:55 UTC ( #410233=note: print w/replies, xml ) Need Help??

in reply to Re: Pearls (not really) of Perl programming
in thread Pearls (not really) of Perl programming

I've done that! For two reasons, one good, one bad.

Bad Reason

I thought

for my $i (1..10) { # some stuff with $i }

was the same as

my $i; for $i (1..10) { # some stuff with $i }

rather than being the same as

{ my $i; for $i (1..10) { # some stuff with $i } }

because in C++, (IIRC)

for (int i=0, i<5; i++) { ... } // i in still in scope here!

I didn't do use int as shown here, but my coworkers did and I would find it misleading. Perl doesn't have that problem, but I only learned that recently.

Good Reason

This fails ('Modification of a read-only value attempted')

foreach my $var ('a', 'b') { ...something that may modify $var... print($var); }

but this doesn't

foreach ('a', 'b') { my $var = $_; ...something that may modify $var... print($var); }

Replies are listed 'Best First'.
Re^3: Pearls (not really) of Perl programming
by revdiablo (Prior) on Nov 24, 2004 at 21:17 UTC

    Your good reason has a corollary, which happens to be another good reason to use the original code. This modifies the original array:

    @array = 1 .. 10; for my $var (@array) { $var++; print "$var\n"; } print "@array\n";

    But this does not:

    @array = 1 .. 10; for (@array) { my $var = $_; $var++; print "$var\n"; } print "@array\n";
Re^3: Pearls (not really) of Perl programming
by Anonymous Monk on Nov 25, 2004 at 18:11 UTC
    Actually, I believe that the C++ behaviour you pointed out is an MSVC glitch.
    for(int i = 0; ;) { }
    ... should have "i" visible only inside the for loop. I think gcc actually does this properly, as does MSVC .NET. ... unless I'm horribly wrong?

      Close... VC++ .NET allows it, gcc 2.95.4 forbids it.


      #include <stdio.h> int main() { for (int i=0; i<5; i++) { printf("%d\n", i); } printf("%d\n", i); return 0; }

      VC++ .NET

      >cl a.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for +80x86 Copyright (C) Microsoft Corporation 1984-2002. All rights reserved. a.cpp Microsoft (R) Incremental Linker Version 7.10.3077 Copyright (C) Microsoft Corporation. All rights reserved. /out:a.exe a.obj >a 0 1 2 3 4 5

      gcc 2.95.4

      $ gcc a.cpp -o a a.cpp: In function `int main()': a.cpp:9: name lookup of `i' changed for new ANSI `for' scoping a.cpp:5: using obsolete binding at `i'
        And gcc points out why. Because it was allowed at one time (in C++) it was introduced into C. Then the C++ guys discovered that it was inconsistent with if() (where it was forbidden from day 1) and removed it. ANSI followed (much) later in C. MS wants to be 'helpful' and allows it. But I would want to know which i is used if the outer block has one too. I would think MS to try to combine the old and new behavior in some MS-unique style and make a mess of it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410233]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2018-01-19 23:46 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (226 votes). Check out past polls.