Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Defining directory/Datas the perl script should work with

by kcott (Archbishop)
on Oct 21, 2021 at 23:29 UTC ( [id://11137870]=note: print w/replies, xml ) Need Help??


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

    Thank you very much!! But I think I still have some questions to this code:  BEGIN { delete @ENV{qw{PATH IFS CDPATH ENV BASH_ENV}} } --> why should I use this? I haven't used it in former scripts... Moreover, I don't understand why I need the sub_ after I run the system? And somehow when I try to run the modified script (with my directories etc) an error " "use" not allowed in expression at script.pl line 17, at end of line" " is given out. So do I need the use  GetOptions::LONG at all? Thanks again. Best Regards, M

      kcott's script is a good way to start this project.

      So do I need the use  GetOptions::LONG at all?

      I would encourage you to use it (note that it's Getopt::Long click to see examples of use) as it offers the best way of dealing with user-supplied parameters at runtime instead of hardcoding those in the script. It's even better than a configuration file read when your script starts because it can allow specifying both a configuration file and extra command-line options which can override options in the configuration file. E.g. myprog --config a/b/c  --iterations 100 --bin-dir /usr/bin/protokoll2. So, it will help you start this project from a solid foundation. I guess extract-brain needs a hell of parameter tweaking depending on protocol, resolution, quality. That's a good way to deal with that complexity. Next step would be to decide on a configuration-file format, plenty of options in Perl's CPAN repository of modules. Additionally, you may want to run your script with different configurations and parameters in parallel to assess their efficacy efficiently. Again, using configuration files and command-line parameters will help immensely.

      Also, note that you can specify subs (functions) anywhere in your Perl script and not necessarily before calling them(re: I don't understand why I need the sub_ after I run the system?). I personally define all my subs at the end of a script, leaving the beginning for the "main" section.

      bw, bliako

        ++ Thanks, bliako.

        There were no responses to the OP's follow-up question when I started to compose mine; however, after I posted it, I saw that you had already replied.

        I can't see any conflicts between our two posts; in some places there's complementary information.

        — Ken

      "I think I still have some questions to this code"

      I'm pleased to see that you are not a cargo cult programmer. See "Cargo cult programming" if you are unfamiliar with that term.

      I provided a variety of links which you should follow. The one to perlsec explains the inclusion of the BEGIN block. If you're stumped by the meaning of the BEGIN itself, read "perlmod: BEGIN, UNITCHECK, CHECK, INIT and END". You may also want to look at: the delete() function; "perldata: Slices"; "perlvar: %ENV"; and, perlop for qw (search the page: qw appears in a number of places). You can also try commenting out the BEGIN line, running with the taint checking I showed (perl -T ...); then checking the messages output.

      I'm not too sure about "sub_" as it doesn't appear in my code (perhaps a typo in your code). There are two subroutine declarations of the form sub _name {...}. These are called earlier in the code with _name(...). Prefixing a subroutine name with an underscore is an informal way of indicating it is intended to be private (used by the code itself, but not directly called by the user of the code): this is fairly common practice and you're likely to encounter it in many places. Also look at perlsub; in particular, the To declare subroutines: and To call subroutines: sections of its SYNOPSIS.

      If you have GetOptions::LONG in your code, it's a fairly major typo: the first 'O' should be lowercase; there's no 'ions'; and, the last part is capitalised, not all uppercase. Case and spelling matter. I wrote: "use Getopt::Long;" at line 14 and "GetOptions(...);" at line 17. You may have somehow confused those two lines, omitted a semicolon near those lines, or something else: without seeing exactly what code you wrote, I'm completely guessing.

      That covers everything you enquired about. If more questions arise, feel free to ask.

      — Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found