differences between find2perl and find

Discussion in 'Perl Misc' started by GU, Aug 28, 2009.

  1. GU

    GU Guest

    first of all i don't know if this is the proper group to discuss this
    problem, but i can't find a better one.....

    I'm using "SunOS h213 5.9 Generic_122300-29 sun4u sparc SUNW,Sun-
    Fire-15000" (uname -a) and perl v5.6.1 and want to copy a lot of files
    to a remote host. So i'm looking up for those files using find:

    rsync -avz `find * -prune -type d -mtime -30|grep -v archive`
    user@remote:dir

    To add some funtionality (logging, mail if completed e.g) i'd like to
    do this with perl.
    Now i gonna try to convert find to a perl script using find2perl.....

    and will have some problems:
    gm@h213:/tmp ># redirect STDERR to avoid warnings caused by
    permissions
    gm@h213:/tmp >find * -prune -type d -mtime -30 2>/dev/null | tail -2
    tmpdir.21790
    tmpdir.27721
    gm@h213:/tmp >
    gm@h213:/tmp >find2perl -prune -type d -mtime -30 | perl
    Found = in conditional, should be == at - line 26.
    gm@h213:/tmp >

    line 26 is that one with prune-check, but if using only -prune those
    error does not occur (but i still have different results)

    gm@h213:/tmp >find * -prune 2>/dev/null | tail -2
    wkz_adj_maf
    wkz_adj_rep.csv
    gm@h213:/tmp >find2perl -prune | perl
    gm@h213:/tmp >

    One solution for me is to glob "*". As far as i can see glob "*" and
    `find * -prune` will produce the same output. So i will be able to
    find the files i need by 'grep {(-d $_ && (int(-M $_) <30))} glob "*"'

    But that's not that what i want to use. If i've tools like find2perl i
    wanna use those ones.

    So what's the problem and how can i use the find2perl-result.
    Do you know what's to do?

    Thanks
    gerhard


    p.s. enclose the output of 'find2perl * -prune -type d -mtime -30'

    gm@h213:/tmp >find2perl * -prune -type d -mtime -30
    #! /usr/local/bin/perl -w
    eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
    if 0; #$running_under_some_shell

    use strict;
    use File::Find ();

    # Set the variable $File::Find::dont_use_nlink if you're using AFS,
    # since AFS cheats.

    # for the convenience of &wanted calls, including -eval statements:
    use vars qw/*name *dir *prune/;
    *name = *File::Find::name;
    *dir = *File::Find::dir;
    *prune = *File::Find::prune;


    # Traverse desired filesystems
    File::Find::find({wanted => \&wanted}, **** list of files **** );
    exit;


    sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    ($File::Find::prune = 1) &&
    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    -d _ &&
    (int(-M _) < 30);
    }

    gm@h213:/tmp >
     
    GU, Aug 28, 2009
    #1
    1. Advertising

  2. GU

    Guest

    On Thu, 27 Aug 2009 23:53:55 -0700 (PDT), GU <> wrote:

    ># Traverse desired filesystems
    >File::Find::find({wanted => \&wanted}, **** list of files **** );
    >exit;
    >
    >
    >sub wanted {
    > my ($dev,$ino,$mode,$nlink,$uid,$gid);
    >
    > ($File::Find::prune = 1) &&
    > (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    > -d _ &&
    > (int(-M _) < 30);
    >}
    >
    >gm@h213:/tmp >


    As far as find2perl:

    -prune is going to stop it from enterring sub-direcoties in the found directory.
    This is more of a mechanism to operate on a single directory that are found in
    the direcoties list. So even if you want to examine the directories under
    the current one, this prevents it. Therefore its totally useless in this context.

    >find2perl * -prune -type d -mtime -30

    ^
    This is not something like glob '*'.

    >File::Find::find({wanted => \&wanted}, **** list of files **** );


    find2perl dir1 dir2 dir3 file1 -prune -type d -mtime -30
    does
    File::Find::find({wanted => \&wanted}, 'dir1', 'dir2', 'dir3', 'file1' );

    and there doesen't appear to be any other way to to get a system generated
    list. In fact because of this, find2perl is not that usefull in this context.
    Its ok for generating a prototype wanted() which you can manipulate and execute
    as a standalone script.

    The documentation for find is pretty bizarr and not much explanation or examples.
    Its all about magic and hocus-pocus.

    From the find2perl docs (on the bottom):
    " ... or if you use the warnings pragma,
    File::Find will report warnings for several weird situations..."

    If you want to turn off warnings if piping the find2perl output to perl,
    just add this:

    find2perl * -prune -type d -mtime -30 | perl -M-warnings

    Good luck. I'm not too suprised there were no responses.
    -sln
     
    , Aug 28, 2009
    #2
    1. Advertising

  3. GU

    Gerhard Guest

    On 28 Aug., 22:28, wrote:

    >
    > Good luck. I'm not too suprised there were no responses.
    >


    but there is one that helps (--> perl -M-warnings)

    thanks

    by thw way ... i was a little bit confused by that difference => find
    does not needs -print to print out files found, find2perl will not
    print anything without this option
     
    Gerhard, Aug 31, 2009
    #3
  4. On 2009-08-31 16:42, Gerhard <> wrote:
    > On 28 Aug., 22:28, wrote:
    >> Good luck. I'm not too suprised there were no responses.
    >>

    >
    > but there is one that helps (--> perl -M-warnings)
    >
    > thanks
    >
    > by thw way ... i was a little bit confused by that difference => find
    > does not needs -print to print out files found, find2perl will not
    > print anything without this option


    Traditionally find *did* need -print to print out the files found. Now
    it is optional, but the find2perl utility is quite old and probably
    understands only find syntax from the 1980's.

    hp
     
    Peter J. Holzer, Aug 31, 2009
    #4
  5. On 2009-08-28 06:53, GU <> wrote:
    > Now i gonna try to convert find to a perl script using find2perl.....
    >
    > and will have some problems:
    > gm@h213:/tmp ># redirect STDERR to avoid warnings caused by
    > permissions
    > gm@h213:/tmp >find * -prune -type d -mtime -30 2>/dev/null | tail -2
    > tmpdir.21790
    > tmpdir.27721
    > gm@h213:/tmp >
    > gm@h213:/tmp >find2perl -prune -type d -mtime -30 | perl


    Note that there is a very important difference between your call to find
    and your call to find2perl:

    In

    find * -prune ...

    the * will be expanded to all files in the current directory (except
    those starting with "."). So here you really call

    find a_dir file1 file2 tmpdir.21790 tmpdir.27721 whatever -prune ...

    But in

    find2perl -prune ...

    you don't give any file arguments. So this is equivalent to

    find2perl . -prune ...

    If you call

    find . -prune ...

    you probably will get the same results.


    To get the same same result as with your find command, you could call

    find2perl * -prune ...

    but that would include the list of currently existing files in your
    script which almost certainly means that the script is useless the next
    time you want to run it (I assume that 'find2perl -prune -type d -mtime
    -30 | perl' is only used for demonstration purposes. Normally you would
    save the script into a file and run it several times).

    So you need to get the glob('*') that is normally performed by the shell
    into you script:

    > #! /usr/local/bin/perl -w
    > eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
    > if 0; #$running_under_some_shell
    >
    > use strict;
    > use File::Find ();
    >
    > # Set the variable $File::Find::dont_use_nlink if you're using AFS,
    > # since AFS cheats.
    >
    > # for the convenience of &wanted calls, including -eval statements:
    > use vars qw/*name *dir *prune/;
    > *name = *File::Find::name;
    > *dir = *File::Find::dir;
    > *prune = *File::Find::prune;
    >
    >
    > # Traverse desired filesystems
    > File::Find::find({wanted => \&wanted}, **** list of files **** );

    ^^^^^^^^^^^^^^^^^^^^^^^
    replace this with
    glob("*")
    or
    @ARGV
    > exit;


    hp
     
    Peter J. Holzer, Aug 31, 2009
    #5
    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. Ed Doyle

    find2perl

    Ed Doyle, Dec 28, 2005, in forum: Perl
    Replies:
    1
    Views:
    3,916
    Joe Smith
    Dec 29, 2005
  2. Bob
    Replies:
    2
    Views:
    392
  3. Home_Job_opportunity
    Replies:
    0
    Views:
    527
    Home_Job_opportunity
    Jan 8, 2009
  4. Sunil

    help with find2perl

    Sunil, Nov 25, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    154
    Anno Siegel
    Nov 25, 2003
  5. Sunil

    find2perl and File::Find examples

    Sunil, Nov 28, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    1,065
    Michele Dondi
    Nov 28, 2003
Loading...

Share This Page