question about variable assignment - easy

Discussion in 'Perl Misc' started by Danny, Apr 29, 2004.

  1. Danny

    Danny Guest

    what is the difference between using

    my $test = 5;
    $test = 5;

    can I use either one?

    Thanks Danny
    Danny, Apr 29, 2004
    #1
    1. Advertising

  2. Danny wrote:
    > what is the difference between using
    >
    > my $test = 5;
    > $test = 5;


    The first line is declaring $test to be a lexically scoped variable ie
    local to the enclosing block or file; the second is autovivifying $test
    as a package global. You should be declaring

    use strict;
    use warnings;

    at the top of your script. Using strict catches a whole load of
    potential errors at compile time and will prevent you from using the
    second form - have a look at perldoc strict. If you *really* need to use
    globals then you can declare such variables using our (perl 5.6+) or
    with

    use vars qw($test);

    though this is subtly different from our.

    Mark
    Mark Clements, Apr 29, 2004
    #2
    1. Advertising

  3. Danny

    Robin Guest

    "Danny" <> wrote in message
    news:Q8ekc.49141$...
    > what is the difference between using
    >
    > my $test = 5;
    > $test = 5;
    >
    > can I use either one?
    >
    > Thanks Danny


    you *can* use either one, but if you want to use strict "vars", you have to
    use the first one, because strict requires you to declare your variables,
    see the documentation for strict.
    -Robin
    Robin, Apr 29, 2004
    #3
  4. Robin wrote:

    > "Danny" <> wrote in message
    > news:Q8ekc.49141$...
    >>
    >> my $test = 5;
    >> $test = 5;

    >
    > you *can* use either one, but if you want to use strict "vars", you have
    > to use the first one, because strict requires you to declare your


    An incomplete answer, but at least that's better than a wrong answer. You're
    improving.

    > variables, see the documentation for strict.


    See also:

    perldoc -q scoping
    perldoc -f my
    perldoc -f our
    perldoc perlsub (section "Private Variables via my()")

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Apr 29, 2004
    #4
  5. Danny

    Danny Guest

    "Sherm Pendley" <> wrote in message
    news:...
    > Robin wrote:
    >
    > > "Danny" <> wrote in message
    > > news:Q8ekc.49141$...
    > >>
    > >> my $test = 5;
    > >> $test = 5;

    > >
    > > you *can* use either one, but if you want to use strict "vars", you have
    > > to use the first one, because strict requires you to declare your

    >
    > An incomplete answer, but at least that's better than a wrong answer.

    You're
    > improving.
    >
    > > variables, see the documentation for strict.

    >
    > See also:
    >
    > perldoc -q scoping
    > perldoc -f my
    > perldoc -f our
    > perldoc perlsub (section "Private Variables via my()")
    >
    > sherm--
    >
    > --
    > Cocoa programming in Perl: http://camelbones.sourceforge.net
    > Hire me! My resume: http://www.dot-app.org
    >


    I appreciate all the responses.

    Thanks very much
    Danny, Apr 30, 2004
    #5
    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. Hazzard
    Replies:
    2
    Views:
    635
    Hazzard
    Apr 6, 2004
  2. nagy
    Replies:
    36
    Views:
    1,002
    Terry Reedy
    Jul 20, 2006
  3. Chris
    Replies:
    34
    Views:
    1,513
  4. Bruno Desthuilliers
    Replies:
    5
    Views:
    383
    Bruno Desthuilliers
    Aug 29, 2007
  5. Derek Basch
    Replies:
    8
    Views:
    134
    Ben Morrow
    Aug 12, 2006
Loading...

Share This Page