I second using a Getopt::* module, but to comment on your code, you are passing @args to findfile(), but not using it as proper function argument (you probably mean $_[0] instead of $args[0] in the findfile() function).

Also, there's no point in having named nested subroutines. Instead of \&wanted, just use an anonymous subroutine...but File::Find doesn't work like what you are trying to do anyway. you shouldn't try to return things from your wanted function (find() doesn't return anything useful), you might set a variable in an outer scope though.


So here's my untested rewrite:
sub findfile { my $target = $_[0]; my $location = "/tmp/dir1"; my $foundfile; $target = qr/$target/; my $filename = find(sub { ($foundfile = $File::Find::name) if $File::Find::name =~ $targ +et; }, $location); print $filename; }
But even with this, do you really want to search all subdirectories of /tmp/dir1 with find()? Or is -f "$location/target" what you want? Is "$target" supposed to be a regular expression? Or just the file basename (in which case you would want "if $_ eq $target" in the function)?

Update: Oops. I removed the accidentally left in "return $foundfile".


In reply to Re: Adding a dispatch table and getting "Variable $x will not stay shared" errors. by runrig
in thread Adding a dispatch table and getting "Variable $x will not stay shared" errors. by gctaylor1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.