perl - system command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl - system command
# 1  
Old 09-22-2005
perl - system command

Can this be done without using te system command? I have a directory with a large number of files in it, but I am interested in only the 8 most recent.

The directory looks like
-rw-rw-rw- 1 adsm adsm 13412 Sep 22 08:31 events_dump_09222005.csv.gz
-rw-rw-rw- 1 adsm adsm 13405 Sep 21 08:31 events_dump_09212005.csv.gz
-rw-rw-rw- 1 adsm adsm 13237 Sep 20 08:31

I am getting the date out of the file to be used in another module. What I have works, but want I want to know is if it can be done without resorting to temporary file (listfiles) and the system command

#!/usr/bin/perl

system( 'ls -tr /adsm/DATASRC/events/ | tail -8r > /adsm/SCRIPTS/BKUPINFO/listfiles');
open (LSTFILES, "</adsm/SCRIPTS/BKUPINFO/listfiles");
while (<LSTFILES>) {
print($_);
($pt1,$pt2) = split(/_dump_/, $_ );
chomp($pt2);
print "$pt2\n";
($pt3,$pt4) = split(/\./,$pt2);
print "$pt3\n";
}



close(LSTFILES);
# 2  
Old 09-22-2005
Yes, in fact I won't resort to shell equivalents if something can be solely done from within Perl, because this ensures the highest portability. The following will give you a list of top 8 files with the latest mtime in descending order of mtime:

Code:
#!/usr/bin/perl

my $base = "/adsm/DATASRC/events/";

opendir DIR, $base or die "Cannot read dir!";
my @list = readdir DIR;
closedir DIR;
my @list_with_mtime = map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]]:()) } @list;
my @r_sorted_list = map { $$_[0] } sort { $$b[1] <=> $$a[1] } @list_with_mtime;

# Show top 8
{
	local $, = "\n";
	print @r_sorted_list[0..7];
}

# 3  
Old 09-23-2005
Thanks for the reply. It absolutely does what I am looking for.
Now I need to understand it. My understanding of the map function is that it works like a foreach loop. Is that right?

Can you explain this statement map { ($_ !~ /^\./?[$_, (stat($base . $_))[9]]Smilie)) } @list
# 4  
Old 09-23-2005
It looks complex, but it is actually quite simple if I put it in a nicer looking form.

Code:
map {
     (     
           $_ !~ /^\./?
                 [
                       $_, 
                       (stat($base . $_))[9]
                 ]:
                 ()
    ) 
} @list

So we read the list of file (including directory entries) names in @list. For each name read, we carry out the following operation:
  • If the filename does not start with a ".", it is not a hidden file. We will put the filename together with its mtime as an array reference to facilitate sorting later on.
  • Otherwise, a hidden file is ignored by yielding an empty array.

It will pass through every entry of @list and collect the evaluated value in the map{} block and return the aggregated list. At the end of this statement we will get an array of array references containing the filename and the corresponding mtime. Entries that yield an empty array are ignored because an empty array upon interpolated in a list will "dissolve" itself. The net effect in this piece of code is that hidden files will disappear from the resulting, aggregated list. This is what we want if we want to ignore all hidden files.

i.e. ("a", "b", (), "c") will give ("a", "b", "c") so this is how this trick works.

Yes, the next statement sorts this intermediate list in descending order of mtime, and then reconstruct an array with filenames only (by cleaning the temporary mtime values and removing the references).
# 5  
Old 09-26-2005
Thank you for the reply.

I am also trying to understand the sort statement { $$_[0] } sort { $$b[1] <=> $$a[1] . I understant what it does but what I am not familiar with is the $$.
# 6  
Old 09-26-2005
As I said, I have made some temporary references with the first statement. In my case this is an array reference (think about it like a pointer to an array) containing the filename together with the mtime as two separate array items.

To access an item from a normal array by index you will

$array[0], $array[1]

If the corresponding array is pointed to by a reference and you need to access an item through the reference, that becomes

$$arrayref[0], $$arrayref[1]

OR (both syntax will work)

$arrayref->[0], $arrayref->[1]

which literally means "dereference and then index in the underlying array".

Understanding these require some knowledge about references in Perl.

The official page that talks about this:
http://perldoc.perl.org/perlref.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem using "system" command in perl

Hello!!! I'm trying to pass the output from bash command to perl variable in a perl script, and I used the "system" command to execute the bash statment and pass the result to perl string variable, in this perl script I used a variable $file that store data for using it as a regular expression.... (2 Replies)
Discussion started by: evolabo
2 Replies

2. Shell Programming and Scripting

How to call the System command twice in the same perl script...

Hello experts, I have a perl script which looks for the ARGV and then loads the data as per it. Example. #Checking the server to connect if ($ARGV eq 'QA') { $ENV{"ORACLE_HOME"} = "/oracle/product/11.2.0"; $ENV{"PATH"} = "$ENV{'PATH'}:/oracle/product/11.2.0/bin"; ... (1 Reply)
Discussion started by: msrahman
1 Replies

3. Shell Programming and Scripting

perl: looping through the output of a 'system' command

Hi there could anybody point me in the right direction when it comes to looping through the output of a system command in perl (i.e. df -k) doing a test against each line to see if it matches? for example if i have a df -k output like this and I wanted to grab the lines that matched "sda" or... (3 Replies)
Discussion started by: rethink
3 Replies

4. Shell Programming and Scripting

file handling in perl without using system command

Hi , Is there any way to achieve following using perl program (i.e without using system command). 1.system ("echo 'test' > /usr/spool/ship.csv"); 2.system ("cat /usr/ajay_test* >> /usr/spool/RAM/work/patil.csv"); 3.system("> /usr/spool/ajay.txt"); e.g for system("rm -f... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

5. Shell Programming and Scripting

exit ststus 9 from perl system command

HI all, can anyone tell me what does exit status 9 from perl's system function meant. I am using system fuction to execute a shell script as : my $s=system ('script.sh' ,arg1 ,arg2); print $s; the output is 9. Thanks in advance. !!:confused: (1 Reply)
Discussion started by: glamo_2312
1 Replies

6. Shell Programming and Scripting

Run system command in perl cgi

Hi guys, got a problem with a perl cgi script over here. I need it to run a system command to get the status of a process. Unfortunately the process is owned by a specific user and only this user can get its status. So i tried running the command from the perl cgi with "su", but then i get the... (12 Replies)
Discussion started by: polki
12 Replies

7. Shell Programming and Scripting

Perl System command calls to variable

I am new to scripting in Perl so I have a dumb question. I know I can call system commands using system("date"); But I am not able to: 1. set its output to a variable 2. run in quiet mode(no output to the screen) The examples i have #!/usr/bin/perl print `date +\%y\%m\%d.\%H\%M`;... (5 Replies)
Discussion started by: 4scriptmoni
5 Replies

8. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

9. Shell Programming and Scripting

Need Help w/PERL system command

Hi, I'm wanting to run an nslookup, dig or whatever to check for the existence of a printer. The PERL script will display the results on the screen, but I can't figure out how to capture the result & test the value. Any ideas will be greatly appreciated!!! Thank You (1 Reply)
Discussion started by: lorik
1 Replies

10. Shell Programming and Scripting

Perl run system command

Can perl execute a system command similar to the C function System()? Thanks. Gregg (1 Reply)
Discussion started by: gdboling
1 Replies
Login or Register to Ask a Question