Why no warning when redclaring a variable in same scope

Discussion in 'Perl Misc' started by Mintcake, Sep 21, 2007.

  1. Mintcake

    Mintcake Guest

    #!/usr/local/bin/perl

    use strict;
    use warnings;

    for my $i (1..10)
    {
    my $i = 0; # Why no warning
    }
    Mintcake, Sep 21, 2007
    #1
    1. Advertising

  2. Mintcake wrote:
    > #!/usr/local/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > for my $i (1..10)
    > {
    > my $i = 0; # Why no warning
    > }


    Different scope.


    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Sep 21, 2007
    #2
    1. Advertising

  3. Mintcake

    Paul Lalli Guest

    On Sep 21, 12:25 am, Mintcake <> wrote:
    > #!/usr/local/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > for my $i (1..10)
    > {
    > my $i = 0; # Why no warning


    > }


    It's not the same scope. It's a subset of the other scope. The first
    $i's scope is for the entire loop, ncluding the loop header - the list
    of 1..10. The second $i's scope is only for the body of the loop,
    not for its header.

    {
    my $i;
    my $i; # Warning!
    {
    my $i; # No warning!
    }
    }

    Paul Lalli
    Paul Lalli, Sep 21, 2007
    #3
  4. Mintcake

    Mintcake Guest

    On Sep 21, 2:51 pm, "John W. Krahn" <> wrote:
    > Mintcake wrote:
    > > #!/usr/local/bin/perl

    >
    > > use strict;
    > > use warnings;

    >
    > > for my $i (1..10)
    > > {
    > > my $i = 0; # Why no warning
    > > }

    >
    > Different scope.
    >
    > John
    > --
    > Perl isn't a toolbox, but a small machine shop where you
    > can special-order certain sorts of tools at low cost and
    > in short order. -- Larry Wall


    Why different scope? - both instances of $i exist only with the {}
    Mintcake, Sep 24, 2007
    #4
  5. Mintcake wrote:
    >
    > On Sep 21, 2:51 pm, "John W. Krahn" <> wrote:
    >>
    >> Mintcake wrote:
    >>>
    >>> #!/usr/local/bin/perl
    >>>
    >>> use strict;
    >>> use warnings;
    >>>
    >>> for my $i (1..10)
    >>> {
    >>> my $i = 0; # Why no warning
    >>> }

    >>
    >> Different scope.

    >
    > Why different scope? - both instances of $i exist only with the {}


    The braces {} define scope (within a file) except (Perl has a lot of
    exceptions :) for the variable declared for a foreach loop which has "special"
    scope.

    $ perl -wle'
    for my $i ( 0 .. 9 ) {
    print __LINE__, ": $i";
    my $i = 20;
    print __LINE__, ": $i";
    }
    continue {
    print __LINE__, ": $i";
    }
    print __LINE__, ": $i";
    '
    Name "main::i" used only once: possible typo at -e line 10.
    3: 0
    5: 20
    8: 0
    3: 1
    5: 20
    8: 1
    3: 2
    5: 20
    8: 2
    3: 3
    5: 20
    8: 3
    3: 4
    5: 20
    8: 4
    3: 5
    5: 20
    8: 5
    3: 6
    5: 20
    8: 6
    3: 7
    5: 20
    8: 7
    3: 8
    5: 20
    8: 8
    3: 9
    5: 20
    8: 9
    Use of uninitialized value in concatenation (.) or string at -e line 10.
    10:



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Sep 24, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    940
    Paul Opal
    Oct 11, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    877
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,805
    Smokey Grindel
    Dec 2, 2006
  4. David Filmer
    Replies:
    19
    Views:
    238
    Kevin Collins
    May 21, 2004
  5. Andrew Falanga
    Replies:
    2
    Views:
    198
    Andrew Falanga
    Nov 22, 2008
Loading...

Share This Page