in reply to Defining directory/Datas the perl script should work with
G'day perlnewbie,
Welcome to the Monastery.
"I think there a lot of syntax errors"
Yes, there are but these seem to have been addressed. I was concerned with this block of code:
my $001= "prob_001"; my $002= "prob_002"; my $003= "prob_003"; @filter: ("001", "002", "003"); #Brainextraction foreach ("001", "002", "003")
I may be wrong, but it looks like you were planning to, or at least thinking about, using symbolic references. This is generally considered to be a bad move and I would recommend that you steer clear of them until you're more experienced; even then, think very carefully before using them.
Follow the link I provided. Note that they can easily result in coding errors or simply confusing code. Your lexical (my) variables do not work with this mechanism. Perhaps inadvertently, you've expressly forbidden your code from using them (with use strict;): it will abort compilation if you attempt to do it. The strict pragma restricts their usage for a very good reason.
"In my script I want the outputimage to be saved in an other folder. Could anyone help me with this?"
I would set up default paths for your input and output directories; then allow users to specify alternatives via options. I would include thorough checking of all input from options and clean any tainted data (see perlsec for more about that).
Unsurprisingly, I have neither the bet program nor any MRI data. For testing, I set up an extremely minimal script to replace bet:
$ cat zombie cp $1 $2
and two directories, whole_head and just_brain, for input and output, respectively. The former has four tiny files, the latter starts empty:
ken@titan ~/tmp/pm_11137850/whole_head $ for i in `ls -1 wh_*`; do echo $i; cat $i; done wh_001 W_001 wh_002 X_002 wh_003 Y_003 wh_004 Z_004 ken@titan ~/tmp/pm_11137850/just_brain $ ls -l total 0
Here's the code (pm_11137850_system.pl):
#!/usr/bin/env perl use strict; use warnings; BEGIN { delete @ENV{qw{PATH IFS CDPATH ENV BASH_ENV}} } use autodie ':all'; use constant { INDIR => '/home/ken/tmp/pm_11137850/whole_head', OUTDIR => '/home/ken/tmp/pm_11137850/just_brain', ZOMBIE => '/home/ken/tmp/pm_11137850/zombie', }; use Getopt::Long; my ($in_dir, $out_dir); my $filter_string = ''; GetOptions( 'indir=s' => \$in_dir, 'outdir=s' => \$out_dir, 'filters=s' => \$filter_string, ); my @filters; my @raw_filters = split /,/, $filter_string; my @problems; _set_dirs(\$in_dir, \$out_dir, \@problems); _get_filters(\@raw_filters, \@filters, \@problems); die @problems if @problems; system(ZOMBIE, INDIR."/wh_$_", OUTDIR."/jb_$_") for @filters; sub _get_filters { my ($raw_filters, $filters, $problems) = @_; my $re = qr{^(\d+)$}; if (! @$raw_filters) { push @$problems, "No filters specified.\n"; } else { for my $raw_filter (@$raw_filters) { if ($raw_filter =~ $re) { push @$filters, $1; } else { push @$problems, "Invalid filter: '$raw_filter'\n"; } } } return; } sub _set_dirs { my ($in, $out, $problems) = @_; my $re = qr{^(\/[\w\/]+)$}; if (length $$in) { if ($$in =~ $re) { $$in = $1; if (! -d $$in) { push @$problems, "Non-existent input directory: '$$in' +\n"; } } else { push @$problems, "Invalid input directory name: '$$in'\n"; } } else { $$in = INDIR; } if (length $$out) { if ($$out =~ $re) { $$out = $1; if (! -d $$out) { push @$problems, "Non-existent output directory: '$$ou +t'\n"; } } else { push @$problems, "Invalid output directory name: '$$out'\n +"; } } else { $$out = OUTDIR; } return; }
You'll obviously need to make a number of changes for your own needs: the paths with '/home/ken/...'; perhaps s/ZOMBIE/BETCMD/; the regexes for checking user input; and so on.
Do note that the system() function takes a list. You'll probably want to expand my simplistic one line
system(ZOMBIE, INDIR."/wh_$_", OUTDIR."/jb_$_") for @filters;
to include some error checking, handling and reporting. And, obviously, the list of arguments will need to be changed.
Take a look at Getopt::Long to change or extend options. Also, the "Documentation and help texts" section, along with Pod::Usage, for an improved user experience.
Here's a sample run showing that there's no tainting:
ken@titan ~/tmp/pm_11137850 $ perl -T ./pm_11137850_system.pl -filters 001,004 ken@titan ~/tmp/pm_11137850/just_brain $ for i in `ls -1 jb_*`; do echo $i; cat $i; done jb_001 W_001 jb_004 Z_004
Here's another showing what would probably be more normal usage:
ken@titan ~/tmp/pm_11137850 $ ./pm_11137850_system.pl -filters 003 ken@titan ~/tmp/pm_11137850/just_brain $ for i in `ls -1 jb_*`; do echo $i; cat $i; done jb_001 W_001 jb_003 Y_003 jb_004 Z_004
Here's some example error messages:
ken@titan ~/tmp/pm_11137850 $ ./pm_11137850_system.pl -outdir /fred -indir wilma Invalid input directory name: 'wilma' Non-existent output directory: '/fred' No filters specified.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Defining directory/Datas the perl script should work with
by perlnewbie (Novice) on Oct 22, 2021 at 08:06 UTC | |
by bliako (Abbot) on Oct 22, 2021 at 10:04 UTC | |
by kcott (Archbishop) on Oct 22, 2021 at 11:22 UTC | |
by kcott (Archbishop) on Oct 22, 2021 at 11:01 UTC |