Why `for local $var (LIST) ...' not allowed?

Discussion in 'Perl Misc' started by Todd, Feb 16, 2008.

  1. Todd

    Todd Guest

    Why can't we explicit using local loop variable in the code snippet
    below?

    #! /bin/perl

    for local $a (1..2) {
    print $a;
    }

    __END__
    syntax error at - line 3, near "for local"
    Execution of - aborted due to compilation errors.

    You see here `my'/`our' are allowed in the code snippet below, why not
    `local'?

    #! /bin/perl

    for my $a (1..2) {
    print $a;
    }

    __END__

    12

    #! /bin/perl

    for our $a (1..2) {
    print $a;
    }

    __END__
    12


    -Todd
    Todd, Feb 16, 2008
    #1
    1. Advertising

  2. Todd wrote:
    > Why can't we explicit using local loop variable in the code snippet
    > below?


    Because it is a syntax error.

    > #! /bin/perl
    >
    > for local $a (1..2) {
    > print $a;
    > }
    >
    > __END__
    > syntax error at - line 3, near "for local"
    > Execution of - aborted due to compilation errors.
    >
    > You see here `my'/`our' are allowed in the code snippet below, why not
    > `local'?


    Because that is the way perl works.



    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, Feb 16, 2008
    #2
    1. Advertising

  3. Todd

    Uri Guttman Guest

    >>>>> "T" == Todd <> writes:

    T> Why can't we explicit using local loop variable in the code snippet
    T> below?

    T> #! /bin/perl

    T> for local $a (1..2) {
    T> print $a;
    T> }

    T> __END__
    T> syntax error at - line 3, near "for local"
    T> Execution of - aborted due to compilation errors.

    T> You see here `my'/`our' are allowed in the code snippet below, why not
    T> `local'?

    why would you want local? do you know the difference between local and
    my? do you know you can localize an our variable? do you also know not
    to use $a (even in examples) as it is special cased with strict? this is
    important as you are playing with scoped variables but i don't see
    strict enabled.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 17, 2008
    #3
  4. Todd

    Todd Guest

    Uri Guttman wrote:
    > why would you want local? do you know the difference between local and
    > my? do you know you can localize an our variable? do you also know not
    > to use $a (even in examples) as it is special cased with strict? this is
    > important as you are playing with scoped variables but i don't see
    > strict enabled.


    It's only an example. BTW, you seems like showing yourself as `guru'
    rather than answer the question. Or did you answer the question?

    -Todd
    Todd, Feb 18, 2008
    #4
  5. Todd <> wrote:
    >
    > Uri Guttman wrote:
    >> why would you want local? do you know the difference between local and
    >> my? do you know you can localize an our variable? do you also know not
    >> to use $a (even in examples) as it is special cased with strict? this is
    >> important as you are playing with scoped variables but i don't see
    >> strict enabled.

    >
    > It's only an example.



    Don't use $a for anything other than sorting, even in examples.


    > BTW,



    You should have resisted the urge to get smartass.

    Off to the killfile you go...


    > you seems like showing yourself as `guru'
    > rather than answer the question.



    Do you want to learn from a guru or from another newbie?


    > Or did you answer the question?



    He did.

    If you researched the answers to his 4 questions, you would have
    learned how to localize foreach's loop control variable, along
    with 3 other things that are important to understand if you
    hope to do much programming in Perl.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Feb 18, 2008
    #5
  6. Todd

    Uri Guttman Guest

    >>>>> "T" == Todd <> writes:

    T> Uri Guttman wrote:
    >> why would you want local? do you know the difference between local and
    >> my? do you know you can localize an our variable? do you also know not
    >> to use $a (even in examples) as it is special cased with strict? this is
    >> important as you are playing with scoped variables but i don't see
    >> strict enabled.


    T> It's only an example. BTW, you seems like showing yourself as `guru'
    T> rather than answer the question. Or did you answer the question?

    do you like showing yourself off to be an idiot? i asked you a couple of
    questions to see if you have any real basis for asking your
    question. you obviously don't. perl doesn't support local that
    way. it actually makes little sense do it as it is trivial to work
    around. but you don't seem to know why local is bad in most cases
    anyhow. if you did you wouldn't ask such a question and you would
    quickly know how to get the same results. (hint: localize a var BEFORE
    the loop).

    there, i answered the question. will you answer mine? i doubt it.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 18, 2008
    #6
  7. Todd

    Todd Guest

    OK, let me reply you one by one:

    Uri Guttman wrote:
    > why would you want local?

    I don't want to use local. I like to use `my' here. But I just want to
    know why `local' is not allowed here, so i put the example.

    > do you know the difference between local and my?

    Why i dont know it? just try perldoc below if you want to:

    perldoc -q local my

    You'll the FAQ about the difference at faq7.

    >do you know you can localize an our variable?

    Yes. `our' variable is just a global variable.

    >do you also know not to use $a (even in examples) as it is special cased with strict?

    The result is same if use `$aaa' instead of $a, even i know $a is
    magical, but why bother to be boring of this?


    So you answer my question ?!!!? Seems you don't know the reason behind
    it or the anser would be very simple rather than long useless paragrah
    saying nothing useful.

    -Todd
    Todd, Feb 18, 2008
    #7
  8. Todd

    Uri Guttman Guest

    >>>>> "T" == Todd <> writes:

    T> OK, let me reply you one by one:

    T> Uri Guttman wrote:
    >> why would you want local?

    T> I don't want to use local. I like to use `my' here. But I just want to
    T> know why `local' is not allowed here, so i put the example.

    then why do you care about local? you rarely need it and it is not
    needed for loop variables. so perl doesn't allow it as it has no good
    use there. simple. don't try to use local on a loop variable.

    >> do you know the difference between local and my?

    T> Why i dont know it? just try perldoc below if you want to:

    T> perldoc -q local my

    huh??

    T> You'll the FAQ about the difference at faq7.

    huh?? speak english.

    >> do you know you can localize an our variable?

    T> Yes. `our' variable is just a global variable.

    >> do you also know not to use $a (even in examples) as it is special cased with strict?

    T> The result is same if use `$aaa' instead of $a, even i know $a is
    T> magical, but why bother to be boring of this?

    boring?

    T> So you answer my question ?!!!? Seems you don't know the reason behind
    T> it or the anser would be very simple rather than long useless paragrah
    T> saying nothing useful.

    and you ask nothing useful. i told you in another post the hint on how
    to work around this.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Architecture, Development, Training, Support, Code Review ------
    ----------- Search or Offer Perl Jobs ----- http://jobs.perl.org ---------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
    Uri Guttman, Feb 18, 2008
    #8
  9. Todd

    Todd Guest

    Uri Guttman wrote:
    > >>>>> "T" == Todd <> writes:

    >
    > T> OK, let me reply you one by one:
    >
    > T> Uri Guttman wrote:
    > >> why would you want local?

    > T> I don't want to use local. I like to use `my' here. But I just want to
    > T> know why `local' is not allowed here, so i put the example.
    >
    > then why do you care about local? you rarely need it and it is not
    > needed for loop variables. so perl doesn't allow it as it has no good
    > use there. simple. don't try to use local on a loop variable.
    >
    > >> do you know the difference between local and my?

    > T> Why i dont know it? just try perldoc below if you want to:
    >
    > T> perldoc -q local my
    >
    > huh??
    >
    > T> You'll the FAQ about the difference at faq7.
    >
    > huh?? speak english.
    >
    > >> do you know you can localize an our variable?

    > T> Yes. `our' variable is just a global variable.
    >
    > >> do you also know not to use $a (even in examples) as it is special cased with strict?

    > T> The result is same if use `$aaa' instead of $a, even i know $a is
    > T> magical, but why bother to be boring of this?
    >
    > boring?
    >
    > T> So you answer my question ?!!!? Seems you don't know the reason behind
    > T> it or the anser would be very simple rather than long useless paragrah
    > T> saying nothing useful.
    >
    > and you ask nothing useful. i told you in another post the hint on how
    > to work around this.
    >


    I like the answer of this run. Why can't you be kind at first?

    -Todd
    Todd, Feb 18, 2008
    #9
  10. Todd <> writes:

    > I like the answer of this run. Why can't you be kind at first?


    The answer is: it doesn't work that way because nobody's bothered to
    imlement it because it's extremely uncommon to need a local() loop
    variable ($_ excepted).

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Feb 18, 2008
    #10
  11. Todd

    Achim Peters Guest

    Tad J McClellan schrieb:
    > Todd <> wrote:
    >> Uri Guttman wrote:
    >>> do you also know not
    >>> to use $a (even in examples)

    >> It's only an example.

    >
    > Don't use $a for anything other than sorting, even in examples.


    Agreed, but shouldn't the FAQ be a shining example of that rule? ;-)

    * http://faq.perl.org/perlfaq6.html :
    | How do I substitute case insensitively on the LHS while
    | preserving case on the RHS?
    ....
    | $a = "this is a TEsT case";
    | $a =~ s/(test)/preserve_case($1, "success")/egi;
    | print "$a\n";

    * http://faq.perl.org/perlfaq7.html :
    | How do I temporarily block warnings?
    ....
    | $a = $b + $c; # I know these might be undef

    and

    | Why do Perl operators have different precedence than C operators?
    ....
    | ($maybe ? $a : $b) = $x;

    * http://faq.perl.org/perlfaq8.html :
    | How do I tell the difference between errors from the shell and perl?
    ....
    | $a = 1 + undef;

    Bye
    Achim
    Achim Peters, Feb 19, 2008
    #11
  12. On Sun, 17 Feb 2008 22:10:52 -0600, Tad J McClellan
    <> wrote:

    >> Or did you answer the question?

    >
    >
    >He did.
    >
    >If you researched the answers to his 4 questions, you would have
    >learned how to localize foreach's loop control variable, along
    >with 3 other things that are important to understand if you
    >hope to do much programming in Perl.


    I beg to differ. He wasn't asking about *how* to localize for's loop
    control variable, but why local() is not valid *syntax* there: that it
    would be pretty much useless being a possible answer, albeit not a
    compelling one. I for one think it *may* be made valid, with the
    obvious semantics associated to it. But that's just because it's in my
    nature to strive for consistency, which indeed may in turn be
    completely out of place in Perl (5)'s realms...


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Feb 29, 2008
    #12
  13. On Mon, 18 Feb 2008 08:23:06 GMT, Uri Guttman <>
    wrote:

    > T> I don't want to use local. I like to use `my' here. But I just want to
    > T> know why `local' is not allowed here, so i put the example.
    >
    >then why do you care about local? you rarely need it and it is not


    Some people, like me, are often concerned about consistency in
    programming language concepts. It's just not always a matter of
    "getting things done".

    > >> do you also know not to use $a (even in examples) as it is special cased with strict?

    > T> The result is same if use `$aaa' instead of $a, even i know $a is
    > T> magical, but why bother to be boring of this?
    >
    >boring?


    I presume he may mean annoying. For some reason the Italian verb
    "annoiare," which has the same source of "to annoy," does not
    translate directly into the latter, but into "to bore." Todd may not
    be a native English speaker, and have incurred in a similar problem.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Feb 29, 2008
    #13
  14. Todd

    Todd Guest

    Great thanks, Michele Dondi.
    You make many things clear enough here.
    Hopes i'm not distract now due to ... :(

    -Todd
    Todd, Apr 6, 2008
    #14
    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. Alvin Bruney

    Threads.. Session var lost, App var ok

    Alvin Bruney, Dec 2, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    rooster575
    Dec 2, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  3. James Mills
    Replies:
    2
    Views:
    361
    geremy condra
    Jun 10, 2010
  4. Iñaki Baz Castillo
    Replies:
    13
    Views:
    497
    Iñaki Baz Castillo
    May 1, 2011
  5. Jang
    Replies:
    1
    Views:
    92
Loading...

Share This Page