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

Hi all,
Simple question.. I'm looking to for a way to replace this call in my code with a perlish one. The reasoning is simple.. if no *.sf files exist ls gives me a complaint. I don't want to see any complaining..
if ( $design eq '?' ){ system("ls --color -c $ext"); print "\nEnter the $_[0] File: "; $design = <STDIN>; chomp $design;
I'm sure someone can help me.. Thanks so much

Replies are listed 'Best First'.
Re: Perlish way to do an ls *.sf
by Zaxo (Archbishop) on Mar 29, 2002 at 17:22 UTC

    { local $\=$/; print for glob("*.$ext"); }
    is one way. There are lots.

    Update: bareblocked and added output record seperator.

    After Compline,
    Zaxo

      And for slightly less typing you could do

      { local $\=$/; print for (<"*.$ext">); }
      :)

        If you must golf...

        { local$\=$/; print for<*.$ext> }

        Not that I would advocate such a course in general... ;-)



        If God had meant us to fly, he would *never* have given us the railroads.
            --Michael Flanders

Re: Perlish way to do an ls *.sf
by gellyfish (Monsignor) on Mar 29, 2002 at 17:14 UTC

    I think that you might want to look at glob in the first instance - If that isn't sufficient then you might want to look at using opendir()/readdir() and grep()

    /J\

Re: Perlish way to do an ls *.sf
by ehdonhon (Curate) on Mar 29, 2002 at 17:10 UTC

    As always in perl, there is more than one way to do that.

    The simplest solution may not be in Perl, though..

    system("ls --color -c $ext >& /dev/null" );
    (Re-direct standard error to /dev/null)
Re: Perlish way to do an ls *.sf
by lestrrat (Deacon) on Mar 29, 2002 at 17:16 UTC

    I don't get how the lines after system() are going to work with the data, but you could do something like:

    opendir( DIR, $targetdir ) || die $!; my @files = grep{ /\.sf$/ } readdir( DIR ); ## do interesting stuff with @files

    If you are supposed to receive a real shell-expanded text, like /foo/bar/*.sf, then use a glob

    ## when passing something like *.pl, make sure ## to single quote it so that the shell doesn't ## automatically expand it: ## ## scrip.pl '/foo/bar/*.pl' ## my @entries = <$ARGV[0]>;