perl : question about defined()


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl : question about defined()
# 1  
Old 06-09-2010
perl : question about defined()

Hi there

rather than doing this
Code:
if (defined($new)) {
    unless (defined($hostname)) {
        print "ERROR: If using --new, you must define a hostname\n";
        exit 1;
    }
}


is there some way of doing a "notdefined" (i appreciate there is no such thing Smilie)


Code:
 if (defined($new) && notdefined($hostname)) {
        print "ERROR: If using --new, you must define a hostname\n"; 
        exit 1;
 }


any advise would be great
# 2  
Old 06-09-2010
Hi,

Code:
If ( ! defined $var)  { print "test" ; }

# 3  
Old 06-09-2010
Thanks pravin27, this works fine

Code:
        if (defined($new) && ( ! defined $hostname)) {
                        print "ERROR: If using --new, you must define a hostname\n";
                        exit 1;
        }




Actually, just playing around ...this seems to work


Code:
        if (defined($new) && (defined($hostname) eq "")) {
                        print "ERROR: If using --new, you must define a hostname\n";
                        exit 1;
        }

# 4  
Old 06-09-2010
Hi,

You missed one closing bracket of IF .

Code:
 if (defined($new) && ( ! defined $hostname) ) {
                        print "ERROR: If using --new, you must define a hostname\n";
                        exit 1;
        }


Last edited by Franklin52; 06-09-2010 at 10:44 AM.. Reason: adding code tags
# 5  
Old 06-09-2010
Quote:
Originally Posted by hcclnoodles
...
is there some way of doing a "notdefined" (i appreciate there is no such thing Smilie)

Code:
 if (defined($new) && notdefined($hostname)) {
        print "ERROR: If using --new, you must define a hostname\n"; 
        exit 1;
 }

...
While there is no such operator as notdefined, the Logical Not - not defined would work for your case. That's because not unary operator is the equivalent of the ! operator(except for the precedence).

Code:
$
$ perl -le '$new="x"; if(defined($new) && ! defined($hostname)){print "ERROR: If using --new, you must define a hostname"}'
ERROR: If using --new, you must define a hostname
$
$ perl -le '$new="x"; if(defined($new) && not defined($hostname)){print "ERROR: If using --new, you must define a hostname"}'
ERROR: If using --new, you must define a hostname
$

tyler_durden
# 6  
Old 06-09-2010
thanks pravi27 (you were very quick, i realised i missed a bracket and edited the post pretty quickly) Smilie


Tyler , thanks for the explanation, it is clear now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping in Perl based on defined keys in Map

Hello All, I am writing the below script where it will connect to database and returns the results. #!/sw/gcm/perl510/bin/perl use SybaseC; &openConnection; &loadvalues; sub openConnection { $dbproc = new SybaseC(SYDB}, $ENV{DBDFLTUSR}, $ENV{DBDFLTPWD}); if... (2 Replies)
Discussion started by: filter
2 Replies

2. Shell Programming and Scripting

Question about awk - create a user-defined variable

Hi, guys, The content of file is below (from <UNIX® Shells by Example Fourth Edition>): My code is below: gawk -F'' ' { OFS = "****"; $3 = "(904)"; $8 = $5 + $6 + $7; print } ' lab3.data The result is below: So, where is the $1? Why is the variable $8 located at the wired position? (3 Replies)
Discussion started by: franksunnn
3 Replies

3. Shell Programming and Scripting

Perl question

Hi All, I am new to Perl and got a real stupid question. We are trying to install the Date:Calc package for some calculations with dates.The security guys mentioned they won't install it as root in /usr/bin/perl but have asked us to install it in any directory and use it from there. Here's the... (2 Replies)
Discussion started by: nua7
2 Replies

4. AIX

AIX : User Defined Authorizations Question

Hello everyone, We have got a Server say Testserver with AIX 6.1 running on it. We want to create user defined authorization for syslogd, ntpd and named . We don't want to use pre-defined authorization (aix.network.daemon). Is it possible to create an independent authorization for commands?... (1 Reply)
Discussion started by: coolr
1 Replies

5. Shell Programming and Scripting

PERL question

Hi I am new to PERL and need to add a line to the perl script which issues a system call if certain pattern exists in the file, for example: $system "my_command" if "my_pattern" exists in "my file" Can somebody help me with the syntax? Thanks (4 Replies)
Discussion started by: aoussenko
4 Replies

6. Shell Programming and Scripting

Is a Perl method defined?

In my code, I know I can write... if ( defined &test_sub ) { test_sub(); } else { print "Subroutine doesn't exist"; } This tests the existence of the test_sub subroutine without actually calling it. If, though, I replace test_sub with a package method... if ( defined... (1 Reply)
Discussion started by: JerryHone
1 Replies

7. Shell Programming and Scripting

Perl Question

I have created some DTS packages in SQL server 2000. The packages map input .dbf files to corresponding tables in the database. When I try to run the package through Command prompt, it runs successfully and loads the data into tables. However same package when ran through perl gives different... (1 Reply)
Discussion started by: b.paramanatti
1 Replies

8. Shell Programming and Scripting

perl question

If I use 2 system commands in a script, will one finish before the next one starts? or will it start the first and the second at the same time? i.e. system("ps | grep rminer"); system("ls -al | grep 431"); (1 Reply)
Discussion started by: BG_JrAdmin
1 Replies

9. Shell Programming and Scripting

PERL Question

I am new to perl. I have adopted some perl scripts that seem not to work 100%. I 've corrected such things as file sharing contention etc. What these perl scripts basically do is extract specific records for statistical reporting of authentication and authorization from syslog daemon local... (5 Replies)
Discussion started by: Gary Dunn
5 Replies

10. Shell Programming and Scripting

PERL Question

Can anyone tell me if the copy command in PERL has the same functionality as in KSH shell in UNIX or does it actually move the file ?? $cp_stat=system("cp $ENV{OLAMEBSDIR}/data/olam.ddabal$type $ENV{OLAMDIR}/balance/data/olam.ddabal$type.$HeaderDate"); (1 Reply)
Discussion started by: frank
1 Replies
Login or Register to Ask a Question