Can perl do this?

Discussion in 'Perl Misc' started by rublind@gmail.com, Apr 2, 2005.

  1. Guest

    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.
    , Apr 2, 2005
    #1
    1. Advertising

  2. peter pilsl Guest

    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
    peter pilsl, Apr 2, 2005
    #2
    1. Advertising

  3. Guest

    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!
    , Apr 2, 2005
    #3
  4. peter pilsl <> wrote in
    news:424f0ac3$0$1945$:

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 2, 2005
    #4
  5. Guest

    Problem with using that windows api, is that the repository is on a
    linux machine, so that does me no good.
    , Apr 3, 2005
    #5
  6. 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Apr 3, 2005
    #6
  7. Big and Blue Guest

    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.
    Big and Blue, Apr 3, 2005
    #7
  8. Guest

    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...
    , Apr 3, 2005
    #8
  9. Joe Smith Guest

    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
    Joe Smith, Apr 3, 2005
    #9
  10. 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.
    Brian McCauley, Apr 3, 2005
    #10
  11. wrote:

    > The command I wanted to run was either an SVN update or a CVS update,


    Most such commands are designed to be scriptable to provide ways to
    avoid the need to enter a password interactively. This makes your
    problem (already not too complex) a _lot_ simpler. No need to use
    anything Expect-like or manipuate both the the VC program's STDIN and
    STDOUT at the same time. You can run it with a simple qx() or open().
    Brian McCauley, Apr 3, 2005
    #11
  12. A. Sinan Unur wrote:

    > 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.


    There are places for both. Obviously in a perfect world you have manual
    check-in where the VC respository is the prime truth.

    But where this has not been achieved there's still value in having a VC
    repository that holds a snapshot of something else.

    For example I have a project that uses a Microsoft SQL database.
    Ideally all the table schemas, views, functions, static table content
    etc should be imported from scripts in some sort of VC repository that
    is the prime truth. They are not.

    In this situation there is still value in having a Perl script that
    periodically dumps all this information from the database into a VC
    respository so at least I have a way to roll back changes and a reliable
    record of what was changed when even if it's not properly tied up with
    by whom and why.

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


    Indeed.

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


    I wouldn't trust this 100% - I often find I have to hit F5 in Windows
    Explorer. Directory change notification _will_ help you to get timely
    updates but to be on the safe side I'd still poll occasionally as a
    fallback.
    Brian McCauley, Apr 3, 2005
    #12
  13. Brian McCauley cut off mid-sentence:

    > 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


    ....various OS specific mechanisms.
    Brian McCauley, Apr 3, 2005
    #13
  14. peter pilsl Guest

    A. Sinan Unur wrote:
    >
    >>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.
    >


    It would, if there werent a sleep-statement inside this loop which
    controls the check-intervals.

    at least in my experience a busy-loop like this does not need any
    resources, cause the sleep is for free :)


    best,
    peter

    --
    http://www.goldfisch.at/know_list
    peter pilsl, Apr 3, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    288
    PerlFAQ Server
    Jan 23, 2011
  3. PerlFAQ Server
    Replies:
    0
    Views:
    657
    PerlFAQ Server
    Feb 3, 2011
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Feb 27, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    279
    PerlFAQ Server
    Feb 27, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    662
    PerlFAQ Server
    Apr 4, 2011
Loading...

Share This Page