Can you explain LOCAL, MY and OUR with examples?

Discussion in 'Perl Misc' started by Picker Leon, Dec 6, 2003.

  1. Picker Leon

    Picker Leon Guest

    I read the perldoc -f local, my our, but would like to know what exactly is
    "eval", because without knowing "eval" I can not understand LOCAL, MY, OUR
    clearly. I got some ideas what those are, but very confused what difference
    they have.

    Anyone can give me an example script which will act differently with MY,
    LOCAL and OUR?

    Thank you.
    Picker Leon, Dec 6, 2003
    #1
    1. Advertising

  2. Picker Leon <> wrote:

    > I read the perldoc -f local,



    Have you tried

    perldoc -q local

    as well?

    What's the difference between dynamic and lexical (static)
    scoping? Between local() and my()?


    > my our, but would like to know what exactly is
    > "eval", because without knowing "eval" I can not understand LOCAL, MY, OUR
    > clearly.



    Lexical (my) vs dynamic (local) scoping can be understood *without*
    knowing about eval().

    Why do you think you need to know eval() to understand scoping?

    Just about every programming language has scoping, only a few
    of them have eval()...


    > I got some ideas what those are, but very confused what difference
    > they have.



    If you don't tell us your ideas, then we cannot confirm that you have
    it right, nor can we help correct them if they are not. (hint)


    See also:

    "Coping with Scoping":

    http://perl.plover.com/FAQs/Namespaces.html


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 6, 2003
    #2
    1. Advertising

  3. Picker Leon

    Ben Morrow Guest

    "Picker Leon" <> wrote:
    > I read the perldoc -f local, my our, but would like to know what exactly is
    > "eval",


    Have you read perldoc -f eval? If so, what in the first two paragraphs
    do you not understand (this is not (just) sarcasm: if it is unclear to
    a beginner, then it would be good to know so someone can fix the docs)?

    > because without knowing "eval" I can not understand LOCAL, MY, OUR
    > clearly.


    I don't know where you got that idea from... the concepts are not
    really related. This may be why you are confused... :)

    > I got some ideas what those are, but very confused what difference
    > they have.


    That's likely because it's confusing :).

    > Anyone can give me an example script which will act differently with MY,
    > LOCAL and OUR?


    For my/local, read the last entry in perldoc -q local. See also
    <http://perl.plover.com/FAQs/Namespaces.html>, which also deals with
    our(), and with 'use vars' which it replaces.

    Ben

    --
    If you put all the prophets, | You'd have so much more reason
    Mystics and saints | Than ever was born
    In one room together, | Out of all of the conflicts of time.
    |----------------+---------------| The Levellers, 'Believers'
    Ben Morrow, Dec 6, 2003
    #3
  4. Picker Leon

    ko Guest

    Picker Leon wrote:
    > I read the perldoc -f local, my our, but would like to know what exactly is
    > "eval", because without knowing "eval" I can not understand LOCAL, MY, OUR
    > clearly. I got some ideas what those are, but very confused what difference
    > they have.
    >
    > Anyone can give me an example script which will act differently with MY,
    > LOCAL and OUR?
    >
    > Thank you.
    >
    >


    perldoc -f local directs you to read perlsub. So do a:

    pelrdoc perlsub

    from your shell. The examples/explanations are *very* good and include
    code snippets. In particular, read the sections:

    1. Private Variables via my()
    2. Temporary Values via local()
    3. When to Still Use local()

    Then do a 'perldoc -f our' for an explanation of our.

    If you don't understand a particular section of perlsub, please ask.
    Someone will help.

    HTH - keith
    ko, Dec 6, 2003
    #4
  5. Picker Leon

    Picker Leon Guest

    I read perldoc -f eval. My understanding is that

    1
    {
    print 'hi';
    }

    is no difference from

    2
    eval
    {
    print 'hi';
    }

    because both 1 and 2 act as what in {} as a small perl program and returns
    back to the main block.
    Picker Leon, Dec 6, 2003
    #5
  6. Picker Leon

    Picker Leon Guest

    Can you explain this code:

    my $input = $_[0];
    local $matchkey = $_[1]; # must use local instead of my to be used in
    callback

    local @output = (); # must use local instead of my to be used in callback

    sub callback {
    my($tag, %attr) = @_;
    if ($tag eq $matchkey) {push(@output, values %attr);};
    return;
    }
    my $p = HTML::LinkExtor->new(\&callback);
    $p->parse($input);


    Why do I have to use local here? If I just use my @output, should callback
    have access @output, because callback is in the same scope with my @output?
    I tried using my, but records messed up totally.
    And I don't know why use my for $input but local for $matchkey.



    > Picker Leon wrote:
    > > I read the perldoc -f local, my our, but would like to know what exactly

    is
    > > "eval", because without knowing "eval" I can not understand LOCAL, MY,

    OUR
    > > clearly. I got some ideas what those are, but very confused what

    difference
    > > they have.
    > >
    > > Anyone can give me an example script which will act differently with MY,
    > > LOCAL and OUR?
    > >
    > > Thank you.
    > >
    > >

    >
    > perldoc -f local directs you to read perlsub. So do a:
    >
    > pelrdoc perlsub
    >
    > from your shell. The examples/explanations are *very* good and include
    > code snippets. In particular, read the sections:
    >
    > 1. Private Variables via my()
    > 2. Temporary Values via local()
    > 3. When to Still Use local()
    >
    > Then do a 'perldoc -f our' for an explanation of our.
    >
    > If you don't understand a particular section of perlsub, please ask.
    > Someone will help.
    >
    > HTH - keith
    >
    Picker Leon, Dec 6, 2003
    #6
  7. Picker Leon

    Picker Leon Guest

    "Picker Leon" <> дÈëÓʼþ
    news:HvdAb.399437$...
    > Can you explain this code:
    >
    > my $input = $_[0];
    > local $matchkey = $_[1]; # must use local instead of my to be used in
    > callback
    >
    > local @output = (); # must use local instead of my to be used in

    callback
    >
    > sub callback {
    > my($tag, %attr) = @_;
    > if ($tag eq $matchkey) {push(@output, values %attr);};
    > return;
    > }
    > my $p = HTML::LinkExtor->new(\&callback);
    > $p->parse($input);
    >
    >
    > Why do I have to use local here? If I just use my @output, should callback
    > have access @output, because callback is in the same scope with my

    @output?
    > I tried using my, but records messed up totally.
    > And I don't know why use my for $input but local for $matchkey.
    >
    >


    Here is a similar example to above, but both my and local work the same:
    local $temp=1;

    sub printtemp {
    print $temp;
    $temp++;
    }

    &printtemp;
    &printtemp;
    &printtemp;
    &printtemp;

    both my or local will give me 1234, why here local and my are the same but
    in the above example, only local works not my.
    Picker Leon, Dec 6, 2003
    #7
  8. Picker Leon

    Uri Guttman Guest

    >>>>> "PL" == Picker Leon <> writes:

    PL> Can you explain this code:
    PL> my $input = $_[0];
    PL> local $matchkey = $_[1]; # must use local instead of my to be used in
    PL> callback

    PL> local @output = (); # must use local instead of my to be used in callback


    that makes NO sense at all.

    PL> sub callback {
    PL> my($tag, %attr) = @_;
    PL> if ($tag eq $matchkey) {push(@output, values %attr);};
    PL> return;
    PL> }
    PL> my $p = HTML::LinkExtor->new(\&callback);
    PL> $p->parse($input);


    PL> Why do I have to use local here? If I just use my @output, should
    PL> callback have access @output, because callback is in the same
    PL> scope with my @output? I tried using my, but records messed up
    PL> totally. And I don't know why use my for $input but local for
    PL> $matchkey.

    there is NO need for local in callbacks. callbacks can be closure or
    have code refs which have access to lexicals. local there is dumb,
    useless and misleading.

    and you still haven't said what is confusing you about my and
    local. have you read the relevant docs? have you read the web pages that
    were mentioned?

    where did that code come from?

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Dec 6, 2003
    #8
  9. Picker Leon

    Uri Guttman Guest

    >>>>> "PL" == Picker Leon <> writes:


    PL> Here is a similar example to above, but both my and local work the same:
    PL> local $temp=1;

    PL> sub printtemp {
    PL> print $temp;
    PL> $temp++;
    PL> }

    PL> &printtemp;
    PL> &printtemp;
    PL> &printtemp;
    PL> &printtemp;

    PL> both my or local will give me 1234, why here local and my are the
    PL> same but in the above example, only local works not my.

    huh? i see only one example. you claim to have two. and you are not
    asking intelligent questions. don't use local as you have no clue about
    it so why are you asking about it?

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Dec 6, 2003
    #9
  10. Picker Leon

    Jay Tilton Guest

    [Please don't top-post replies. Please trim reply-quoted material to the
    minimum necessary to establish context.]

    "Picker Leon" <> wrote:

    : Can you explain this code:
    :
    : my $input = $_[0];
    : local $matchkey = $_[1]; # must use local instead of my to be used in
    : callback

    Be aware of how your news client handles word-wrapping.

    : local @output = (); # must use local instead of my to be used in callback
    :
    : sub callback {
    : my($tag, %attr) = @_;
    : if ($tag eq $matchkey) {push(@output, values %attr);};
    : return;
    : }
    : my $p = HTML::LinkExtor->new(\&callback);
    : $p->parse($input);
    :
    : Why do I have to use local here?
    :
    : If I just use my @output, should callback
    : have access @output, because callback is in the same scope with my @output?
    :
    : I tried using my, but records messed up totally.
    : And I don't know why use my for $input but local for $matchkey.

    For what's shown, there's no reason to use local() instead of my().

    local() _might_ make sense if the code shown is inside a subroutine that is
    being called recursively, but that seems unlikely.

    Where did you get this code? Is there more to it?
    Jay Tilton, Dec 6, 2003
    #10
  11. Picker Leon

    ko Guest

    Picker Leon wrote:
    > "Picker Leon" <> дÈëÓʼþ
    > news:HvdAb.399437$...
    >
    >>Can you explain this code:
    >>
    >> my $input = $_[0];
    >> local $matchkey = $_[1]; # must use local instead of my to be used in
    >>callback
    >>
    >> local @output = (); # must use local instead of my to be used in

    >
    > callback
    >
    >> sub callback {
    >> my($tag, %attr) = @_;
    >> if ($tag eq $matchkey) {push(@output, values %attr);};
    >> return;
    >> }
    >> my $p = HTML::LinkExtor->new(\&callback);
    >> $p->parse($input);
    >>
    >>
    >>Why do I have to use local here? If I just use my @output, should callback
    >>have access @output, because callback is in the same scope with my

    >
    > @output?
    >
    >>I tried using my, but records messed up totally.
    >>And I don't know why use my for $input but local for $matchkey.


    I'm not sure where you got that code from, but why not use the complete
    example from the HTML::LinkExtor docs? And you'll notice that local is
    nowhere to be found. In other words, you *don't* need to use local for
    the example above.

    > Here is a similar example to above, but both my and local work the same:
    > local $temp=1;
    >
    > sub printtemp {
    > print $temp;
    > $temp++;
    > }
    >
    > &printtemp;
    > &printtemp;
    > &printtemp;
    > &printtemp;
    >
    > both my or local will give me 1234, why here local and my are the same but
    > in the above example, only local works not my.


    Did you read the sections mentioned in the previous post regading my and
    local? Or any of the links given by the others? In the example above,
    you're not using local the way its supposed to be used. That's why you
    get the same results, whether you use a my declaration or a local
    declaration for $temp.

    Here's a direct (relavant) quote from perlsub:

    "WARNING: In general, you should be using my instead of local, because
    it's faster and safer. Exceptions to this include the global punctuation
    variables, global filehandles and formats, and direct manipulation of
    the Perl symbol table itself. local is mostly used when the current
    value of a variable must be visible to called subroutines."

    As has been pointed out by others, its hard to help you if you don't
    explain *exactly* what it is you don't understand/are trying to
    accomplish - the subject of your post asks about local, my, and our, but
    you have also made a reference to eval, and you have used
    HTML::LinkExtor in another example.

    keith
    ko, Dec 6, 2003
    #11
  12. Also sprach Picker Leon:

    > Can you explain this code:
    >
    > my $input = $_[0];
    > local $matchkey = $_[1]; # must use local instead of my to be used in
    > callback
    >
    > local @output = (); # must use local instead of my to be used in callback
    >
    > sub callback {
    > my($tag, %attr) = @_;
    > if ($tag eq $matchkey) {push(@output, values %attr);};
    > return;
    > }
    > my $p = HTML::LinkExtor->new(\&callback);
    > $p->parse($input);
    >
    >
    > Why do I have to use local here? If I just use my @output, should callback
    > have access @output, because callback is in the same scope with my @output?
    > I tried using my, but records messed up totally.
    > And I don't know why use my for $input but local for $matchkey.


    In the above code, there is no need for using local().

    First thing that can be said about local(), is that only deals with
    package variables. Package variables are those that live in a package
    (=namespace) and can be qualified:

    package Test;
    $var = "foobar";
    package main;
    print $var; # $var is undef here because it is $main::var

    versus

    package Test;
    $var = "foobar";
    package main;
    print $Test::var; # prints foobar

    Compare this with

    package Test;
    my $var = "foobar";
    package main;
    print $Test::var; # prints nothing
    print $var; # prints foobar

    So variables declared with my() are visible only in the enclosing block
    or - in case their is no block - throughout the whole file.

    local() can now be used to give a package variable temporarily a new
    value:

    $var = "old value"; # this is a package variable!
    {
    local $var = "new value";
    print $var, "\n";
    }
    print $var, "\n";
    __END__
    new value
    old value

    The previous value is restored when leaving the block in which the
    variable was localized.

    my() on the other hand doesn't do anything to the value of a variable.
    It is used to create _new_ lexical variables. local() on the other hand
    does not create variables, it simply gives them a new value. The reason
    why local() and my() are often mentioned in the same context is
    historical. Perl4 did not have lexical variables, there were only
    package ones. So it was impossible to write

    sub function {
    my ($var1, $var2) = @_;
    ...
    }

    instead one had to use global variables inside a function. That's why it
    was common to use

    sub function {
    local ($var1, $var2) = @_;
    ...
    }

    in order to make sure that you did not accidently overwrite $var1 and
    $var2 elsewhere in your program.

    This problem no longer exists with lexical variables because they only
    exist in the block in which they were declared:

    package main;
    {
    my $var = "foobar";
    }
    print $var; # tries to print $main::var

    The above will additionally give a fatal error when 'use strict' is in
    effect because global variables either have to be declared using our()
    or 'use vars'. So our() and 'use vars' are to package variables roughty
    what my() is for lexicals. I say 'roughly' because our() the vars pragma
    aren't quite the same. our() adds some odd scoping semantics (similar to
    those of my()) to package variables. But I suggest you forget about
    that. It's more than obscure.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Dec 6, 2003
    #12
  13. >>>>> "Picker" == Picker Leon <> writes:

    Picker> Anyone can give me an example script which will act
    Picker> differently with MY, LOCAL and OUR?

    Better than that, an entire article at

    <http://www.stonehenge.com/merlyn/UnixReview/col46.html>

    And alongside that one, another 198 feature-length articles on Perl
    to read. And another 2.5 each month!

    (Please post with a valid email address next time. I would have preferred
    emailing this to you instead.)

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
    Randal L. Schwartz, Dec 7, 2003
    #13
  14. Uri Guttman <> writes:

    > there is NO need for local in callbacks. callbacks can be closure or
    > have code refs which have access to lexicals. local there is dumb,
    > useless and misleading.


    I disagree. There is no _need_ for local() but I wouldn't throw the
    tool out just because it has some problems. (BTW: one problem, that
    local() cannot be applied to lexical variables, is totally
    artificial).

    And the "clean" alternative is not without it's problems. Perl
    closures require special care to avoid memory leaks. Anonymous
    subroutines make stack dumps less readable.

    I recently wrote a module with code like:

    use AtExit;
    sub awful {
    my ($various,%shared,@vars);
    my ($recusive1,$recursive2);
    my $cleanup = AtExit->new(sub{
    undef $recursive1;
    undef $recursive2;
    });
    $recursive1 = sub {
    # Do stuff with $various,%shared,@vars and make further calls to
    # &$recursive1 and &$recursive2
    }
    $recursive2 = sub {
    # Do stuff with $various,%shared,@vars and make further calls to
    # &$recursive1 and &$recursive2 and call
    frobnicate($recursive1);
    }
    $recursive1->();
    }

    I decided it was more maintainable as:

    our ($various,%shared,@vars);

    sub awful {
    local ($various,%shared,@vars);
    recursive1();
    }

    sub recursive1 {
    # Do stuff with $various,%shared,@vars and make further calls to
    # &recursive1 and &recursive2
    }

    sub recursive2 {
    # Do stuff with $various,%shared,@vars and make further calls to
    # &$recursive1 and &$recursive2
    frobnicate(\&recursive1);
    }

    Now there is a potential problem here if &frobnicate manages to
    callback to &recursive1 after after awful() completes or during
    another nested call to of awful(). But I happen to know that's not
    going to happen.

    Package variable and local() are a useful tool if you know what you
    are doing. If you decide not to use that tool you should do so on
    rational not emmotional grounds.

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
    Brian McCauley, Dec 11, 2003
    #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. Shapper
    Replies:
    3
    Views:
    408
    willow
    Jun 10, 2005
  2. Danno
    Replies:
    55
    Views:
    1,447
    Chris Smith
    Apr 25, 2006
  3. Advo
    Replies:
    11
    Views:
    3,277
  4. www.jrdesignstudio.com
    Replies:
    17
    Views:
    619
  5. John Laco

    Can you explain this?

    John Laco, Sep 26, 2003, in forum: ASP General
    Replies:
    7
    Views:
    131
    _Netah X
    Sep 26, 2003
Loading...

Share This Page