yicstone has asked for the wisdom of the Perl Monks concerning the following question:

I was always being puzzled by some basic conception.
Maybe you are glad to explain them to help me : )
1.what is namespace 2.my() & local()'s usage & difference
Thanks.

Replies are listed 'Best First'.
RE: basic conception?
by Aighearach (Initiate) on Apr 25, 2000 at 20:58 UTC
    If you want a concise definition, you should check out the manual pages. You can find them in the Library, if you don't already have them.
    That said, here is a brief in plain english.

    my() creates a lexical variable that is local to the block it's in. If you declare my $foo;and call function my_function(), my_function() will not have access to $foo.

    local() on the other hand, creates a variable that is local to the current block, and blocks it calls. so my_function() will be able to get at $foo, if you declare it local $foo;

    There is more to it than this, but it's a start.

    --4c6966653a205468652073656172636820666f
      7220746861742070657266656374204765656b
      20436869632c20746865206f6e652077697468
      2074686520737061726b6c696e672065796573
      2077686f20706c617973206973207261696e2e
    
Re: basic conception?
by btrott (Parson) on Apr 25, 2000 at 21:08 UTC
    Take a look at perlsub--specifically, the sections "Private Variables via my()" and "Temporary Values via local()".

    my() variables aren't stored in the symbol table; local() variables are stored in the symbol table, in the sense that they give "local values to global (package) variables."

    From perlsub:

    In general, you should be using ``my'' instead of ``local'', because it's faster and safer. Exceptions to this include the global punctuation variables, filehandles and formats, and direct manipulation of the Perl symbol table itself. Format variables often use ``local'' though, as do other variables whose current value must be visible to called subroutines.
Re: basic conception?
by ZZamboni (Curate) on Apr 25, 2000 at 21:23 UTC
    To answer your first question: a namespace is the "space" in which identifiers corresponding to a program are stored. These identifiers include variable names, subroutine names, file handle names, and mostly anything that you can refer to by name in a Perl program (or in any other, the concept is not exclusive to Perl).

    The main reason to be aware of namespaces is that you cannot have two things of the same type with the same name in the same namespace, but you can do that if they are in different namespaces. For example, each package in Perl defines a new namespace, which is why different packages can have variables with the same name and not have them conflict with each other. In perl, the fully qualified name of a variable or a subroutine is given by prepending its package name separated by two colons. So for example, if both packages A and B define a variable v, they are actually two different variables, with the full names $A::v and $B::v.

      Even variables that you do not declare to be in any package are by default in the "mother of all packages", main. So when you write "$x=10;" perl really sees it as
      $main::x=10;

      Since everything is based off of main, the examples above can also be written as:

      $main::A::v and $main::B::v
      (Both ways are 'correct', although the previous way is actually preferred.)

      Unless you are creating your own packages, you don't have to worry about namespace or about using the double colon identifier - perl does all of that behind your back. But it is a nice feeling to know that perl is back there, keeping track of everything for you. :)

Re: basic conception?
by yicstone (Initiate) on Apr 26, 2000 at 06:17 UTC
    Thanks for your help! I love perlmonks ^_^ .