foreach loops and lexically scoped loop variables

Discussion in 'Perl Misc' started by Derek Basch, May 26, 2006.

  1. Derek Basch

    Derek Basch Guest

    Why does this:

    foreach my($key) (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don't";
    }

    produce this:

    Missing $ on loop variable at test.cgi line 28, <GEN1> line 3.i

    I searched alot and I can't find a good answer except for my Perl
    version being outdated and mine is not:

    perl -v

    This is perl, v5.8.3 built for x86_64-linux-thread-multii

    Thanks,
    Derek Basch
     
    Derek Basch, May 26, 2006
    #1
    1. Advertisements

  2. Derek Basch

    Dr.Ruud Guest

    Derek Basch schreef:
    $ perl -MO=Deparse -e '
    foreach my($key) (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don-t";
    }
    '
    Missing $ on loop variable at -e line 2.


    Remove the () around $key:


    $ perl -MO=Deparse -e '
    foreach my $key (sort(keys(%reactivate_account_ref))) {
    print "I should work...but I don-t";
    }
    '
    foreach my $key (sort keys %reactivate_account_ref) {
    print 'I should work...but I don-t';
    }
    -e syntax OK
     
    Dr.Ruud, May 26, 2006
    #2
    1. Advertisements


  3. Because you are attempting to use invalid syntax.

    foreach my $key (sort(keys(%reactivate_account_ref))) {
     
    Tad McClellan, May 26, 2006
    #3
  4. Derek Basch

    Matt Garrish Guest

    Because you can only assign to a single scalar not a list (or array or
    hash), which is what you are doing by wrapping $key in parentheses.

    foreach my $key (sort keys %reactive_account_ref) {

    Matt
     
    Matt Garrish, May 26, 2006
    #4
  5. Derek Basch

    Derek Basch Guest

    Thanks everyone,

    I guess I just find it confusing what to put in parentheses and what
    not to in Perl. For instance I see examples all the time the have none
    around 'keys' and other that do. There doesn't seem to be a consistency
    to it.

    Derek Basch
     
    Derek Basch, May 27, 2006
    #5
  6. Derek Basch

    Uri Guttman Guest

    DB> I guess I just find it confusing what to put in parentheses and what
    DB> not to in Perl. For instance I see examples all the time the have none
    DB> around 'keys' and other that do. There doesn't seem to be a consistency
    DB> to it.

    perl is actually very consistant. you just don't understand the
    consistancies. the conditional of an if/while is always in parens. keys
    is a builtin function and can be called with/without parens just like
    all the other builtins. perl is consistantly flexibile there. now you
    still may need to use parens for precedence but that is another
    consistancy you need to learn.

    uri
     
    Uri Guttman, May 27, 2006
    #6
  7. While I agree with all the rest of this answer of yours, and I find
    Perl to be much more consistent than is often regarded by people who
    don't really know it, it is perhaps slightly exaggerated to call it
    "very consistent". We all know two key ingredients in its design have
    been magic and pragmatism, possibly at the expense of some
    consistency...


    Michele
     
    Michele Dondi, May 29, 2006
    #7
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.