global variables

Discussion in 'Perl Misc' started by worlman385@yahoo.com, Feb 19, 2007.

  1. Guest

    if I call a perl function 26 times,

    are the variables $decode, $flag global variables?

    I thought they are local variables like methods in Java, once the
    function returns, $decode , $flag will get destroy.

    But when i look into komodo debugger they are global variables.

    How can i use them as local variables like method variables in Java?

    Thanks



    for ( $i = 1; $i <= 26; $i++)
    {
    decode($i);
    }

    sub decode
    {
    $decode = "";
    $flag = $_[0];
    if ( $flag != undef ) {
    $data_file="brute.txt";
    } else {
    $data_file="encoded.txt";
    $flag = 3;
    }
    open(DAT, $data_file) || die("Could not open encoded.txt!");
    @raw=<DAT>;
    close(DAT);
    }
    , Feb 19, 2007
    #1
    1. Advertising

  2. kens Guest

    On Feb 18, 7:35 pm, wrote:
    > if I call a perl function 26 times,
    >
    > are the variables $decode, $flag global variables?
    >
    > I thought they are local variables like methods in Java, once the
    > function returns, $decode , $flag will get destroy.
    >
    > But when i look into komodo debugger they are global variables.
    >
    > How can i use them as local variables like method variables in Java?
    >
    > Thanks
    >
    > for ( $i = 1; $i <= 26; $i++)
    > {
    > decode($i);
    >
    > }
    >
    > sub decode
    > {
    > $decode = "";
    > $flag = $_[0];
    > if ( $flag != undef ) {
    > $data_file="brute.txt";
    > } else {
    > $data_file="encoded.txt";
    > $flag = 3;
    > }
    > open(DAT, $data_file) || die("Could not open encoded.txt!");
    > @raw=<DAT>;
    > close(DAT);
    >
    > }


    No, they are not 'local' variables. They are global to the package
    they are used in.

    Use a lexical variable (declared with the 'my' keyword to limit a
    variable's scope.

    For instance:

    sub decode
    {
    # This variables are only scoped for subroutine
    # decode.
    my $decode = "";
    my $flag = $_[0];
    ...

    Note that you should always start your programs with the following two
    lines:

    use strict;
    use warnings;

    Using strict will force you to define all variables, thus, they will
    not default to be a package global. It will alos save you from
    misspelling variable names (at least part of the time - you could
    misspell it to the name of another variable I suppose).

    HTH, Ken
    kens, Feb 19, 2007
    #2
    1. Advertising

  3. Joe Smith Guest

    wrote:
    > if I call a perl function 26 times,
    > are the variables $decode, $flag global variables?


    All variables are global unless explicitly specified otherwise.

    > I thought they are local variables like methods in Java, once the
    > function returns, $decode , $flag will get destroy.
    > But when i look into komodo debugger they are global variables.
    >
    > How can i use them as local variables like method variables in Java?


    To do that in perl is sort of like Java, in that such variables need
    to be explicitly declared. The keyword to do this is "my".

    my $decode = "";
    my $flag = $_[0];
    for my $i (1 .. 26) {...};

    Do not be mislead by "local" in perl. It refers to a global variable
    with a local value. Instead, put "use strict; use warnings;" near the
    top of the file to have perl help you make sure all variables are
    properly declared.

    -Joe
    Joe Smith, Feb 19, 2007
    #3
  4. <> wrote:

    > I thought they are local variables like methods in Java, once the
    > function returns, $decode , $flag will get destroy.



    "Coping with Scoping":

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Feb 19, 2007
    #4
    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. Wayne
    Replies:
    2
    Views:
    456
    Wayne
    Nov 11, 2003
  2. jubelbrus
    Replies:
    5
    Views:
    602
    JohnQ
    Jul 20, 2007
  3. mark4asp
    Replies:
    1
    Views:
    203
  4. Tony Archer

    Global Variables? Multi-Application Variables?

    Tony Archer, Nov 24, 2003, in forum: ASP General
    Replies:
    5
    Views:
    210
    Tony Archer
    Nov 25, 2003
  5. king
    Replies:
    2
    Views:
    158
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page