Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can perl do this?

Reply
Thread Tools

Can perl do this?

 
 
rublind@gmail.com
Guest
Posts: n/a
 
      04-02-2005
Okay, I don't know perl yet, but I have a book to be read when I get
some time. But there is something I need soon and I'd like to know if
perl can do it.
Here's what I need:
* Whenever the contents of a directory chnage (filesize, date
modified, new file, anything) the script needs to call a system
command,
* The system command will require a password, so the STDIN and STDOUT
need to be played with so the password can be sent by the file
* after the command, the script needs to monitor the folder again, and
repeat after a change.

Is this possible? If it is, would it be that complicated of a script?
Or can I do it in five minutes after I learned the language?

Thanks.

 
Reply With Quote
 
 
 
 
peter pilsl
Guest
Posts: n/a
 
      04-02-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Okay, I don't know perl yet, but I have a book to be read when I get
> some time. But there is something I need soon and I'd like to know if
> perl can do it.
> Here's what I need:
> * Whenever the contents of a directory chnage (filesize, date
> modified, new file, anything) the script needs to call a system
> command,
> * The system command will require a password, so the STDIN and STDOUT
> need to be played with so the password can be sent by the file
> * after the command, the script needs to monitor the folder again, and
> repeat after a change.
>
> Is this possible? If it is, would it be that complicated of a script?
> Or can I do it in five minutes after I learned the language?
>


It is possible. When the password is read via STDIN, then its fairly
easy and done withing five minutes. Otherwise things get a bit more
complicated (like in any other language) and you'll want to take a look
at the Expect::Simple - Module to help you and it will take you 10minutes.

The problem ist - as with any other language - that learning the
language itself will take you much more than 10 minutes.
But your planned script look like a good way to start.


Below my unworthy script that monitors a given folder and reports if it
changes - it took three minutes to write and another three minutes to
comment. I did not implement the command-calling and password-stuff
cause this really depends on the command you'd like to call. Note that I
assumed a *nix-system and use the system-command ls to get the content
of the directory. There are other ways using perl-internas, but it seems
that calling ls might be suffient.


--------------------mon.pl-----------------------------
#!/usr/bin/perl -w

use strict;

my $folder=$ARGV[0]; # the first argument is the folder to watch
my $dir_old; # last version of the folder-content
my $dir_new; # this will hold the new version

while (1) { # run forever (until ctrl-c breaks)
$dir_new=`ls -l $folder`; # get the content of the folder

# if it's not the first loop where $dir_old ist still undefined and
# if there was a change
if ($dir_old and $dir_new ne $dir_old) {
print "change in $folder\n";
}

$dir_old=$dir_new; # let the new get old
sleep 1; # wait one second
}
------------------------------------------------------------


example:

$ ./mon.pl /tmp
change in /tmp
CTRL-C
$


best,
peter



--
http://www.goldfisch.at/know_list
 
Reply With Quote
 
 
 
 
rublind@gmail.com
Guest
Posts: n/a
 
      04-02-2005
The command I wanted to run was either an SVN update or a CVS update,
but even though I don't really know pel that will, I think I understand
what you are doing.

But what I don't get is where/how you are running the command. I see
how you made $dir_new equal to the ls command, so I'm assuming that
you're making it contain the folders contents, but I'm not sure.

I think I'll try the script out though. Thanks!

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-02-2005
peter pilsl <(E-Mail Removed)> wrote in
news:424f0ac3$0$1945$(E-Mail Removed):

> (E-Mail Removed) wrote:


>> * Whenever the contents of a directory chnage (filesize, date
>> modified, new file, anything) the script needs to call a system
>> command,


> Below my unworthy script that monitors a given folder and reports if
> it changes - it took three minutes to write and another three minutes
> to comment.


> while (1) { # run forever (until ctrl-c breaks)
> $dir_new=`ls -l $folder`; # get the content of the folder


Polling like this, i.e. writing a busy loop is probably not a good idea.

Elsthread, the OP has pointed out that he wants to run a version control
program to be run every time the contents of the working directory
changes.

Humbly, I think it is better for humans to decide when it is a good time
to check sources in to a version control repository than having every
change you make be checked in regardless of what that change is, but
that's just me.

It might be useful to do this on a schedule, in which case the OS
dependent scheduling facilities (cron, at) ought to be used.

On the other hand, Windows has a directory change notification callback
API. Using that would avoid the busy loop.

Sinan


--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
rublind@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Problem with using that windows api, is that the repository is on a
linux machine, so that does me no good.

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-03-2005
(E-Mail Removed) wrote in news:1112487637.856491.163600
@l41g2000cwc.googlegroups.com:

> Problem with using that windows api, is that the repository is on a
> linux machine, so that does me no good.


Please quote some context when you are replying.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      04-03-2005
(E-Mail Removed) wrote:
> Problem with using that windows api, is that the repository is on a
> linux machine, so that does me no good.


A few quick things then.

1) Make sure you sleep in the loop. If you can accept a 30s delay between
new files arriving and being handled then sleep for 30s.

2) Don't use a system call to "ls -l" to look for changes.
i) stat the directory. Has the mtime changed since last time you
stat()ed it? If not, then no new file has arrived.
ii) If something has, the use readdir() to get the entries.

3) Try to make it so that you don't need to give id+password via STDIN.
Doesn't CVS allow you to set an environment variable or something?


--
Just because I've written it doesn't mean that
either you or I have to believe it.
 
Reply With Quote
 
rublind@gmail.com
Guest
Posts: n/a
 
      04-03-2005
I couldn't get anything working with cvs, I'm using SVN, and I don't
need to pass it a password. Another problem with checking if new files
were added or anything is that it uses Berkley DB's to keep all that
data, so I actually don't know how everything is stored...

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      04-03-2005
Big and Blue wrote:

> 2) Don't use a system call to "ls -l" to look for changes.
> i) stat the directory. Has the mtime changed since last time you
> stat()ed it? If not, then no new file has arrived.
> ii) If something has, the use readdir() to get the entries.


But that won't catch the case where a currently existing file
is modified. The stat() value of that file will have changed
but the value from stat() on the directory will not.

>>> * Whenever the contents of a directory change (filesize, date
>>> modified, new file, anything)


That's going to require doing a stat() on every file in the directory.
-Joe

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      04-03-2005
(E-Mail Removed) wrote:

> Subject: Re: Can perl do this?


Please put the subject of your post in the Subject of your post.

> Okay, I don't know perl yet, but I have a book to be read when I get
> some time. But there is something I need soon and I'd like to know if
> perl can do it.
> Here's what I need:
> * Whenever the contents of a directory chnage (filesize, date
> modified, new file, anything) the script needs to call a system
> command,


Perl has all the normal filesystem functions and is good at comparing
things so you could poll.

There is no portable standard for automatic notification of directory
updates but if you look in these newsgroups you'll find many discussions
of interfacing to the

> * The system command will require a password, so the STDIN and STDOUT
> need to be played with so the password can be sent by the file


This sounds like you want something like Expect. Perl has an
implementation of Expect.

> * after the command, the script needs to monitor the folder again, and
> repeat after a change.


Perl is a general purpose programming language. You can do loops and
conditions.

>
> Is this possible?


Yes is it possible.

> If it is, would it be that complicated of a script?


Nothing you've said is all that complex.

> Or can I do it in five minutes after I learned the language?


There's more to reading a language than just reading a book.

Let me turn it round.

This problem is suffiently simple that, if your lack of skills
specifically in the Perl language were the factor preventing you from
solving this problem in Perl then you would not be able to say you had
"learned the language".

There's nothing specific to Perl here - the same could be said of any
programming language that has an implementation of Expect or something
similar.

How many languages do you know?

How many languages could you already solve this in?

There's a big difference between learning a programming language and
learning to program.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 04-04-2011 10:00 PM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 02-27-2011 11:00 PM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments