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

I have to populate a directory structure one level with creating directory names and two files per directory ( actually a redirect to an other CGI in perl ) But I need to know how to send the shell comands like I was using zsh or something should I write a shell command file that does it and then call it through opening a pipe? Passinf the dirctory name through the perl script to the shell script?

Replies are listed 'Best First'.
Re: Making directories and files
by strider corinth (Friar) on Oct 29, 2002 at 19:31 UTC
    There are a couple of ways to do this. The cleanest doesn't use shell code at all:
    my $topdir = '.'; my @subdirectory_names = ( 'bob', 'bill', 'sue' ); foreach my $dir( @subdirectory_names ){ mkdir( "$topdir/$dir" ) or die "Couldn't mkdir $topdir/$dir: $!"; foreach my $redirect( 'redirect_1.html', 'redirect_2.html' ){ open( OUT, ">$topdir/$dir/$redirect" ); print OUT $redirect_html_code; # this should be set above close OUT; } }

    If you must use shell for some reason, though, there are three functions that execute shell code: exec, system, and backticks (``, or qx//). Each of those links lead to an explanation of each function.

    Hope that helps!
    --

    Love justice; desire mercy.
Re: Making directories and files
by nothingmuch (Priest) on Oct 29, 2002 at 19:28 UTC
    You can probably manage with perl, using mkdir, and open to create files and directories.

    In order to use shell scripts, however, you may use the back ticks (`command`), and perhaps system and exec (i'm not one hundred per cent on that - i'm not a unix guy).

    the backticks will return the output, but if you want a real pipe, you should use
    open (READ,"command |"); # this opens a pipe from 'command' open (WRITE,"| command"); # this opens a pipe to 'command'


    -nuffin
    zz zZ Z Z #!perl
Re: Making directories and files
by dondelelcaro (Monk) on Oct 29, 2002 at 19:35 UTC
    I'm not quite clear exactly what you are trying to do, but if it is all possible, you should (in general) avoid calling shell scripts (and other non-portable commands) in your perl scripts. Instead, where possible, use modules that provide equivalent behavior.

    That being said, you can easily run shell commands using the back-ticks operator `echo "blah"`, or the regex like quote operators qx{echo "blah"}. Output to STDOUT from the shell command is returned.

    If that doesn't answer your question, perhaps you could clarify a bit, or post some example code?