Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using a snippet of code found here at the monestary to display photos on the web based on the file's time stamp. The code works great except for the fact that the first image diplayed is a dot (.) How can I eliminate this? TIA for any suggestions.
#!/usr/bin/perl use File::Find; $photo_dir = '/path/to/public_html/photos'; $photo_url = "http://www.foo.com/photos"; print "Content-type: text/html\n\n"; find(\&wanted, $photo_dir); sub wanted { if ( (stat $_)[9] > time() - (86400 * 1) ) { # all pics w +ithin the past 24 hours print "<img src=\"$photo_url/$_\" height=150 width=150><p +>\n"; } }

Replies are listed 'Best First'.
Re: File::Find help requested
by VSarkiss (Monsignor) on Aug 07, 2001 at 18:57 UTC

    Tell your wanted sub to ignore files whose name starts with a period:

    sub wanted { return if /^\./; ...
    This is more stringent than you asked for. If you want to specifically exclude only the "." entry, change the expression to:
    return if /^\.$/;

    HTH

    Update
    arturo correctly points out there's no reason to use a regex to test a simple string equality. The second one should be:     return if $_ eq '.';Thanks, arturo!

Re: File::Find help requested
by Hofmator (Curate) on Aug 07, 2001 at 19:00 UTC

    Skip it! by altering your wanted sub:

    sub wanted { if ( (stat $_)[9] > time() - (86400 * 1) ) { return if /^\.\.?$/; print "<img src=\"$photo_url/$_\" height=150 width=150><p>\n"; } }
    This skips the . (and ..) directory entries.

    -- Hofmator

Re: File::Find help requested
by lemming (Priest) on Aug 08, 2001 at 00:58 UTC
    This returns immediatly if the subject is a directory.
    sub wanted { return if -d $_; if ( (stat $_)[9] > time() - (86400 * 1) ) { print "<img src=\"$photo_url/$_\" height=150 width=150><p>\n"; } }
Re: File::Find help requested
by arturo (Vicar) on Aug 08, 2001 at 01:07 UTC

    Just a small note: the -M filetest returns the amount of time since the file was last modified (i.e. the difference between now and its mtime), expressed as a number of days, so the wanted sub could also be written:

    sub wanted { return if -d $_; print "<img src='$photo_url/$_' height='150' width='150'><p>\n" if + -M _ < 1; }

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'