CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
while defined, what if not defined?

 



Iconx
Novice

Jun 4, 2014, 11:17 AM

Post #1 of 5 (5486 views)
while defined, what if not defined? Can't Post

I have a gap in my knowledge here.. given this simple code:


Code
#!/usr/bin/perl -w 
use strict;

my $command = 'userinfo -p -n myusername';
open( USRLIST, "$command |" ) or die "Could not execute $command \n";

while ( defined( my $line = <USRLIST> ) ) {
print "printing: $line \n";
}
close(USRLIST)


What if the command returns nothing? Then $line is not defined so how do I return an error from here?


Laurent_R
Veteran / Moderator

Jun 4, 2014, 2:16 PM

Post #2 of 5 (5461 views)
Re: [Iconx] while defined, what if not defined? [In reply to] Can't Post

If your USRLISTfilehandler does not return anything, your program will just exit the while loop and go to the next instruction. If you need to detect that the command did not return anything, then you might add a counter in the while loop:


Code
my $count = 0; 
while ( defined( my $line = <USRLIST> ) ) {
$count ++;
print "printing: $line \n";
}
if ($count == 0) {
# raise an error or do whatever is fit ...
}


It is probable that testing the $. variable (line number on the last filehandler) after the end of the while loop will be sufficient, as shown in this session under the Perl debugger:

Code
  DB<1> $command = "ls -l"; 

DB<2> open( USRLIST, "$command |" ) or die "Could not execute $command \n";

DB<3> while ( defined( my $line = <USRLIST> )) { next;};

DB<4> print $.
186

So, my command returned 186 lines. If $. is equal to 0 right after your while loop,, it would mean that your command did not return anything.

However, a possibly better alternative might be to use the backticks to do something like this:

Code
my @command_output = `userinfo -p -n myusername`; 
die "the unserinfo command returned nothing" unless scalar @command_output;
# now process the @command_output array content



Iconx
Novice

Jun 4, 2014, 2:40 PM

Post #3 of 5 (5454 views)
Re: [Laurent_R] while defined, what if not defined? [In reply to] Can't Post

Oh yea I like that last one with the backticks, it seems to be working ok. I'll keep testing that thanks.


Iconx
Novice

Jun 4, 2014, 3:19 PM

Post #4 of 5 (5444 views)
Re: [Iconx] while defined, what if not defined? [In reply to] Can't Post

ok another question, mostly just to complete my understanding of different ways of doing things. The code looks like this now:


Code
#!/usr/bin/perl -w  
use strict;

my @command = `userinfo -p -n myusername`;
print "Note: The account was not found in the userlist file. \n" unless scalar @command;

while(@command){
my $line = shift(@command);
print "printing: $line \n";
}
close(USRLIST)


If the account is not found I dont want to the program to exit but if the command fails to run because it doesnt exist I want the program to exit. How would I do that?


Laurent_R
Veteran / Moderator

Jun 5, 2014, 10:34 AM

Post #5 of 5 (5060 views)
Re: [Iconx] while defined, what if not defined? [In reply to] Can't Post

What you can do is to check the value of the special $! variable immediately after the command with the backticks. It should contain an error message if the command failed and be an empty string if the command succeeded. The problem is that it is not very reliable (you might get an empty string for some types of failures), probably because Perl and the OS might have different views of success and failure. For example, if you issue a non existing command, $! will probably be populated with an error message saying that the command does not exist. But if you do something like this:

Code
my @files = `ls foobar.*`; 

Code
$! will probably be an empty string if there is no such file in the current directory (even though the same command at the shell prompt would print at the screen a message such as "No such file or directory". It is not really a problem here, because you'll be able to figure out that the @files is empty, so no file was found.  Well, all this to say that the result might be a bit surprising in some cases. 

So you should really test with your command and see what $! and the destination array look like when it succeeds and when it fails.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives