Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Find images regardless of filetype extension.

by merlyn (Sage)
on Aug 01, 2005 at 06:10 UTC ( [id://479806]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Find images regardless of filetype extension.
in thread Find images regardless of filetype extension.

But not nearly as fun, nor nearly as educational. Actually not using any other module was the point for me creating it.
Fine, then post it here with "I know there are ways to do this with a few modules, but one of my design goals was to not use modules". Otherwise, I waste a lot of time trying to figure out why you didn't use modules, which is a silly goal for production code.
I would appreciate any comments on code quality, if you have any.
Well, let's just start with the inefficiencies and errors of these two lines:
my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/ +$_" } @tmp; my @dirs = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -d "$dir/ +$_" } @tmp;
Let's see. Twice as many stats as you need (because you can get whether it's a file or a dir with one stat). Breaks on files named "..\n" (because you'll reject that with your regex match needlessly). So there's a bug and a misfeature, just in those two lines.
I personally do not see anything wrong with reinventing the wheel as long as you know it's not necessary.
I do. You've posted this code here. Some crazy fool is going to cargo-cult your code without paying attention to your design goals or the following commentary. And that puts more bad Perl code in the world, not good code. {sigh}

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^4: Find images regardless of filetype extension.
by zzspectrez (Hermit) on Aug 01, 2005 at 06:46 UTC

    Fine, then post it here with "I know there are ways to do this with a few modules, but one of my design goals was to not use modules

    Fair enough. I think I should add that as my standard disclaimer since I have a habit of doing that quite often.

    Twice as many stats as you need (because you can get wether it's a file or a dir with one stat).

    I was aware that there had to be a better way to end up with both @files, @dirs with one test but was and still am unsure how to do it. What do you suggest? Really... I was hopping for some code suggestions/improvements

    Breaks on files name "..\n" (because you'll reject that with your regex match needlessly).

    Where in the world are you going to find an image named '..'. I tried to create a file named '..' on windows and it will not let me. Regardless, I agree if you can create a file named '..' it will miss it, but is that really break it?? It's a bug that will silently ignore such file at most, correct? Really... I have seen mention of this before, but have never understood what the problem is. Is the problem eliminated with the use of !/^\.{1,2}\z/??

    I do. You've posted this code here. Some crazy fool is going to cargo-cult your code without paying attention to your design goals or the following commentary. And that puts more bad Perl code in the world, not good code. {sigh}

    merlyn, you need to stop worring about crazy people. No matter what anyone does they will still be crazy and eventually shoot themselves in the foot. :) However, this discussion has shown me some things I would not have been aware if I had not posted it.

    I think I understand your frustration, however I disagree with your ideas on coping with it. The standard use CPAN response is valid but does not lead to people who know how to write better code. They do write better code because the bugs that would have been in their implementation are not there. However, they do not understand why their version had bugs. I think it keeps them as cargo-cult programers who just know how to do the routine to get the results they want. They do not grow. Im trying to grow. Telling me bad boy, dont rewrite. Will just keep me writing bad code in this world. However, If you tell me what Im doing wrong I will (hopefully :) ) learn from my mistakes.

    THANKS

    zzSPECTREz
    My Standard Disclaimer
      I was aware that there had to be a better way to end up with both @files, @dirs with one test but was and still am unsure how to do it. What do you suggest? Really... I was hopping for some code suggestions/improvements

      here's one way to do it:

      # your version # my @tmp = readdir(DIR); # my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/$_" } +@tmp; # my @dirs = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -d "$dir/$_" } +@tmp; # another version my @files; my @dirs; my @tmp = grep { ! /^\./ } readdir DIR; # grep once. foreach (@tmp) { if ( -d "$dir/$_" ) { # stat once. push @dirs, "$dir/$_"; } else { push @files, "$dir/$_"; } }

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
      Chady | http://chady.net/
      Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.

        When I was writing this, I started off just looking for files in the current directory. So,  my @files = map { "$dir/$_" } grep { !/^\.{1,2}$/ && -f "$dir/$_" } was nice and compact and seemed very perlish.

        I then decided to allow directory traversal and kept the same code format. Still nice and compact, only 3 lines compared to 10 in your method. Of course, mine is obviously way less efficient.

        I can't think of a way to eliminate the extra grep and stat and keep the same compactness.

        Thanks for you suggestion.

        zzSPECTREz

        please read my disclaimer

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://479806]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found