defined $var

Discussion in 'Perl Misc' started by George Mpouras, Sep 21, 2011.

  1. why this give errors ?

    use strict;
    use warnings;
    #my $var=1;
    unless (defined $var) {die "variable \$var is undefined\n"}
    print $var;
     
    George Mpouras, Sep 21, 2011
    #1
    1. Advertising

  2. "George Mpouras" <> writes:

    > why this give errors ?
    >
    > use strict;
    > use warnings;
    > #my $var=1;
    > unless (defined $var) {die "variable \$var is undefined\n"}
    > print $var;


    Because 'use strict' forces you to declare variables. I thinks you are
    confused about the difference between not declaring a variable and a
    variable having the undefined value. It is two different things.

    //Makholm
     
    Peter Makholm, Sep 21, 2011
    #2
    1. Advertising

  3. "George Mpouras" <> wrote:
    >why this give errors ?
    >
    >use strict;
    >use warnings;
    >#my $var=1;
    >unless (defined $var) {die "variable \$var is undefined\n"}
    >print $var;


    Because you didn't declare $var.

    "defined()" is about the value of a variable, it has nothing to do with
    the existence i.e. declaration of a variable.

    jue
     
    Jürgen Exner, Sep 21, 2011
    #3
  4. "Jurgen Exner" <> wrote in message
    news:...
    > "George Mpouras" <> wrote:
    >>why this give errors ?
    >>
    >>use strict;
    >>use warnings;
    >>#my $var=1;
    >>unless (defined $var) {die "variable \$var is undefined\n"}
    >>print $var;

    >
    > Because you didn't declare $var.
    >
    > "defined()" is about the value of a variable, it has nothing to do with
    > the existence i.e. declaration of a variable.
    >
    > jue


    Ok , then what about the existence ?!
     
    George Mpouras, Sep 21, 2011
    #4
  5. "George Mpouras" <> writes:
    > "Jurgen Exner" <> wrote in message
    > news:...
    >> "George Mpouras" <> wrote:
    >>>why this give errors ?
    >>>
    >>>use strict;
    >>>use warnings;
    >>>#my $var=1;
    >>>unless (defined $var) {die "variable \$var is undefined\n"}
    >>>print $var;

    >>
    >> Because you didn't declare $var.
    >>
    >> "defined()" is about the value of a variable, it has nothing to do with
    >> the existence i.e. declaration of a variable.
    >>
    >> jue

    >
    > Ok , then what about the existence ?!


    'Existence' doesn't figure here. The variable will spring into
    existence upon first use. But when strict 'vars' is being used
    (IMO, generally a good idea) then a declaration (our $var) is required
    in order to inform the compiler that this variable is actually
    supposed to exist.

    I suggest to discuss the theory that what hasn't been declared cannot
    possibly exist with a customs officer in suitable situation in order
    to gain some inside into the meaning of the term 'declaration'.
     
    Rainer Weikusat, Sep 21, 2011
    #5
  6. George Mpouras

    Ted Zlatanov Guest

    On Wed, 21 Sep 2011 12:32:50 +0300 "George Mpouras" <> wrote:

    GM> "Jurgen Exner" <> wrote in message
    GM> news:...
    >> "George Mpouras" <> wrote:
    >>> why this give errors ?
    >>>
    >>> use strict;
    >>> use warnings;
    >>> #my $var=1;
    >>> unless (defined $var) {die "variable \$var is undefined\n"}
    >>> print $var;

    >>
    >> Because you didn't declare $var.
    >>
    >> "defined()" is about the value of a variable, it has nothing to do with
    >> the existence i.e. declaration of a variable.


    GM> Ok , then what about the existence ?!

    You can use Devel::Symdump to look at the full list of declared scalars,
    arrays, and hashes, for any scope, including $main.

    Ted
     
    Ted Zlatanov, Sep 21, 2011
    #6
  7. "George Mpouras" <> wrote:
    >"Jurgen Exner" <> wrote in message
    >> "defined()" is about the value of a variable, it has nothing to do with
    >> the existence i.e. declaration of a variable.

    >
    >Ok , then what about the existence ?!


    What about it?
    There are programming languages where a variable is created
    automatically on first use. Perl is such a language.
    However this methodology has been frowned upon for a long time since it
    leads to many hard to find errors where a user simply misspelled a
    variable. Therefore 'use strict' alters this behaviour and enforces a
    declaration before you can use the variable(*).

    jue

    *: somewhat simplified, there are a few exceptions
     
    Jürgen Exner, Sep 22, 2011
    #7
  8. Jürgen Exner <> writes:
    > "George Mpouras" <> wrote:
    >>"Jurgen Exner" <> wrote in message
    >>> "defined()" is about the value of a variable, it has nothing to do with
    >>> the existence i.e. declaration of a variable.

    >>
    >>Ok , then what about the existence ?!

    >
    > What about it?
    > There are programming languages where a variable is created
    > automatically on first use. Perl is such a language.
    > However this methodology has been frowned upon for a long time since it
    > leads to many hard to find errors where a user simply misspelled a
    > variable. Therefore 'use strict' alters this behaviour and enforces
    > a declaration before you can use the variable(*).


    [rw@sapphire]~ $perl -we '$a = 1'
    Name "main::a" used only once: possible typo at -e line 1.

    But that's - at best - as unreliable as relying on strict to catch
    accidental misspellings. That's something the computer cannot ever do
    because doing so requires both understanding the meaning of the
    executed code and knowing what effect the program was supposed to
    produce.
     
    Rainer Weikusat, Sep 22, 2011
    #8
    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:
    366
    rooster575
    Dec 2, 2003
  2. thomson
    Replies:
    10
    Views:
    2,499
    Eliyahu Goldin
    Jun 20, 2005
  3. thomson
    Replies:
    0
    Views:
    387
    thomson
    Jun 20, 2005
  4. Oodini
    Replies:
    1
    Views:
    1,779
    Keith Thompson
    Sep 27, 2005
  5. Replies:
    3
    Views:
    140
    Johann Kappacher
    Apr 6, 2008
Loading...

Share This Page