School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,362 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,483 people online right now. Registration is fast and FREE... Join Now!




check variable if its defined

 

check variable if its defined, using "defined"

Sun751

20 Jun, 2009 - 12:37 PM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
CODE

        if (defined($ENV{FRUNEXT}) eq 'ifx')
        {
            print"[ENV is: Informix ]";
            $informix=1;
        }
        elsif(defined($ENV{FRUNEXT}) eq 'msv')
        {
            $sql=1;
        }
        elsif(defined($ENV{FRUNEXT}) eq 'ora')
        {
            $oracle=1;
        }


In above code, I am trying to check if $ENV{FRUNEXT} variable is defined or not in every if statement before checking if its 'ifx' 'msv' or 'ora'.
But its seems to be not working, Could any one suggest me where I go wrong? any better way of doing it?


User is offlineProfile CardPM
+Quote Post


KevinADC

RE: Check Variable If Its Defined

20 Jun, 2009 - 01:07 PM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
There is really no need to even use defined in this situation, just check the value of $ENV{FRUNEXT}:

CODE

        if ($ENV{FRUNEXT} eq 'ifx')
        {
            print"[ENV is: Informix ]";
            $informix=1;
        }
        elsif($ENV{FRUNEXT} eq 'msv')
        {
            $sql=1;
        }
        elsif($ENV{FRUNEXT} eq 'ora')
        {
            $oracle=1;
        }
        else {
             print "Did not equal any of the values, it equals: <$ENV{FRUNEXT}>";
        }


This post has been edited by KevinADC: 20 Jun, 2009 - 01:08 PM
User is offlineProfile CardPM
+Quote Post

Sun751

RE: Check Variable If Its Defined

20 Jun, 2009 - 10:24 PM
Post #3

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
QUOTE(KevinADC @ 20 Jun, 2009 - 01:07 PM) *

There is really no need to even use defined in this situation, just check the value of $ENV{FRUNEXT}:

CODE

        if ($ENV{FRUNEXT} eq 'ifx')
        {
            print"[ENV is: Informix ]";
            $informix=1;
        }
        elsif($ENV{FRUNEXT} eq 'msv')
        {
            $sql=1;
        }
        elsif($ENV{FRUNEXT} eq 'ora')
        {
            $oracle=1;
        }
        else {
             print "Did not equal any of the values, it equals: <$ENV{FRUNEXT}>";
        }



i am trying to use defined coz i am getting the error saying, comparasion of uninitialized variable!!!!!
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Check Variable If Its Defined

21 Jun, 2009 - 05:47 AM
Post #4

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(Sun751 @ 20 Jun, 2009 - 08:37 PM) *

CODE

        if (defined($ENV{FRUNEXT}) eq 'ifx')
        {
            print"[ENV is: Informix ]";
            $informix=1;
        }
        elsif(defined($ENV{FRUNEXT}) eq 'msv')
        {
            $sql=1;
        }
        elsif(defined($ENV{FRUNEXT}) eq 'ora')
        {
            $oracle=1;
        }


In above code, I am trying to check if $ENV{FRUNEXT} variable is defined or not in every if statement before checking if its 'ifx' 'msv' or 'ora'.
But its seems to be not working, Could any one suggest me where I go wrong? any better way of doing it?

"defined" returns a boolean value, true (1) if the variable is defined and false (an empty string) if it is undefined, so this will never be equal to any of the values you're comparing it against. There's also no need to test definedness every time; if $ENV{FRUNEXT} is defined the first time, it will be defined for the others and vice-versa, unless you do something to change it.

I would rewrite the section of code you posted as:
CODE

if (defined $ENV{FRUNEXT}) {
  if ($ENV{FRUNEXT} eq 'ifx')
  {
    print"[ENV is: Informix ]";
    $informix=1;
  }
  elsif($ENV{FRUNEXT} eq 'msv')
  {
    $sql=1;
  }
  elsif($ENV{FRUNEXT} eq 'ora')
  {
    $oracle= 1;
  }
}


If you're using perl 5.10 or later, this would also be a good place to use given/when, but I'm assuming that you (like most of the world) are still using 5.8.x.
User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Check Variable If Its Defined

21 Jun, 2009 - 05:09 PM
Post #5

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
Whatever environment you are running your script in you can check the ENV definitions:

CODE

foreach my $key (keys %ENV) {
    print "$key = $ENV{$key}\n";
}


I have never heard of FRUNEXT so I don't know what it is associated with but I have a feeling it missing from the your environment definitions. You may need to set it somehow before running your perl script. How are you running the perl script by the way?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:14PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month