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.
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
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:
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]])) } @list
It looks complex, but it is actually quite simple if I put it in a nicer looking form.
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).
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 $$.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)