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

when I do this:

ls > data

in the consol I get the trailing /

but when I do this:

system("ls > data")

in my program I dont

Replies are listed 'Best First'.
Re: how do I remember the trailing /
by Hofmator (Curate) on Jul 19, 2001 at 16:45 UTC

    try system("ls -p > data"); and if you are interested in the output you don't have to write to data and then open and read it in again. You can use backticks:

    my $output = `ls -p`; # or get each line into an array my @files = `ls -p1`;

    --Hofmator

Re: how do I remember the trailing /
by Anonymous Monk on Jul 19, 2001 at 16:24 UTC
    look at perlfunc:substr (particularly the negative examples)

    also, look at perlre(to see what $ means in a regex), and perlop (for the m/// operator)

    which should lead you to something like:

    if(length $foo > 1 and substr($foo,-1,1) ne '/') { $foo .= '/'; }
    or
    $foo .= '/' unless($foo =~ m/\/$/);
    btw - if you aren't used to the unless syntaxt above, see perlsyn
Re: how do I remember the trailing /
by bikeNomad (Priest) on Jul 19, 2001 at 17:44 UTC
    Your problem is (apparently) that the output of ls is different between your console shell session and your Perl script. The most likely reason for this is that the ls you are calling in your shell session is an alias set in one of your (or your system's) login scripts. I don't know what system you're using, but on my Linux box, if I type:

    $ which ls alias ls='ls --color=auto -B' /bin/ls
    so you see that my console session runs ls with some options. However, you don't generally inherit aliases. So what you want to do is either to run ls with the -p option as Hofmator suggested, or to use stat or -d to find out whether the file is a directory.
Re: how do I remember the trailing /
by jlongino (Parson) on Jul 19, 2001 at 21:16 UTC
    If you use a system("ls -F > data") you will get the trailing \. Note that you will also get the other trailing symbols as well. The man page for "ls" excerpt:
    -F Marks directories with a trailing slash (/), doors with a trailing greater-than sign (>), executable files with a trailing asterisk (*), FIFOs with a trailing vertical bar (|), symbolic links with a trailing at-sign (@), and AF_UNIX address family sockets with a trailing equals sign (=).
    I suspect that the user account you were using at the console has an alias in the startup files or is possibly defined at a higher system level to default the -F switch. I would think that you would be getting the same results if the latter were true or if you were logged in as the same user and performed both tasks.

    Update: Hofmator is probably closer to the mark. i.e., -p instead of -F

Re: how do I remember the trailing /
by CyCliC (Novice) on Jul 19, 2001 at 16:51 UTC
    oops! didn't see your reply Hofmator before I wrote
    thanks know it works

    Originally posted as a Categorized Answer.

Re: how do I remember the trailing /
by CyCliC (Novice) on Jul 19, 2001 at 16:48 UTC
    I dont think that will work because when I do that
    I dont know what entries are files or directorys

    my goal with the program is to enter a directory
    and all the sub directorys and create a file in each
    that contains the files and directorys in that directory

    Originally posted as a Categorized Answer.