Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl 5.6.1 populating an @array

Status
Not open for further replies.

alfie002

Technical User
Joined
Mar 3, 2004
Messages
121
Location
GB
Dear all,

I have created a script that populates an array. The script was initially developed on a Windows platform and the script then ported across to Solaris. The only things that changed were any filesystem references from \..\ to /.../.

However, I have experienced a problem populating an array. On the Windows system this works ok, on the Unix system, although the array does seem to be populated, when I print the array or try to reference the array, the value is 0.

Strange because it works ok on the Windows platform.

Any ideas = much appreciated.

Alf

Code:

#!/usr/perl5/5.6.1/bin/perl

use File::Copy;
use File::stat;
use Time::Local;

sub file_list {
@dirlisting = system "/usr/bin/ls /tmp";
print "dirlisting should be full\n\n";
print @dirlisting;
}

file_list();


 
You must have changed something other than just the direction of the slashes - \usr\bin\ls won't do anything useful on Windows.

For that code, it is normal that @dirlisting would contain a single element with the exit code of the call to "ls" in it. Have a read of the perldoc relating to "system" - I doubt it's doing what you're expecting it to. That will only give you the exit value of whatever command you run - i.e. it won't give you the output of the command.

To do that, use backticks (i.e. @dirlisting = `ls /tmp`);

Better still, don't shell out to "ls" at all and use Perl's built-in "glob" function - far more cross-platform:

Code:
my @dirlisting = glob '/tmp/*';
 
Hello ishnid,

I see what you are saying regarding the exit value. A value of 0 appeared in the array confirming the successful completion of the listing of the directory, that is, the directory was there.

Will give your suggestion a go.

Many thanks

Alf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top