Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to read the input directory from command line arguments using perl?

by bart (Canon)
on Mar 22, 2017 at 04:28 UTC ( #1185398=note: print w/replies, xml ) Need Help??


in reply to How to read the input directory from command line arguments using perl?

I'll assume you can have more than one project.txt file in different locations, and that the directory is relative to the directory that project.txt file is in. $otherscript contains the script that starts with the code you gave.

Then you can do something like:

# Assuming you already have read/set the values for $output_dir and $m +ap_file... # and a list of project.txt files is given in @ARGV use File::Spec::Functions qw(rel2abs); use File::Basename qw(dirname); while(<>) { if(/^REVISION_LOCATION:(.*)/) { # $ARGV contains the name of the project.txt file you're readi +ng system($otherscript, "--root=".rel2abs($1, dirname($ARGV)), "- +-outdir=$out_dir", "--map=$map_file"); } }

Alternatively you could put this code somewhere inside the $otherscript and call the relevant function instead of the system call.

  • Comment on Re: How to read the input directory from command line arguments using perl?
  • Download Code

Replies are listed 'Best First'.
Re^2: How to read the input directory from command line arguments using perl?
by finddata (Sexton) on Mar 22, 2017 at 05:16 UTC
    How can i run the above file in terminal?.I had used as follows
    ./html.pl --root=/home/data/check_data/project.txt --outdir=/home/data +/generate --mapfile=/home/data/check_data/mapfile.txt

      You already have all the pieces that you need:

      1. In your root node you show how you get the command line arguments.
      2. bart showed you how to
        1. get the pathname of the project.txt file using File::Basename,
        2. parse the value out of the REVISION_LOCATION: line, and
        3. turn that filename from relative to absolute using File::Spec's rel2abs, using the pathname of project.txt as the base.

      If you're unsure on how to open the project.txt file instead of using the magic while(<>) that bart used, please see "Files and I/O" in perlintro.

      Updated wording slightly.

        The thing which he added i understood .But the problem is am not geeting how to run the file in terminal by passing input file
Re^2: How to read the input directory from command line arguments using perl?
by finddata (Sexton) on Mar 23, 2017 at 04:37 UTC
    My query is this script is here where are you taking project.txt as your input in the above code.

      My query is this script is here where are you taking project.txt as your input in the above code.

      Why while is empty without considering and input file? (source)

      You can read about the magic <> operator and its use in while loops in I/O Operators. In the code that bart posted, you would provide project.txt either on the command line, as in "perl script.pl project.txt", or by feeding it into the script's STDIN, as in "cat project.txt | perl script.pl".

Re^2: How to read the input directory from command line arguments using perl?
by finddata (Sexton) on Mar 23, 2017 at 04:38 UTC
    Why while is empty without considering and input file?
      "...while is empty..."

      It isn't ;-) From "Learning Perl":

      "Another way to read input is with the diamond operator: <>. This is useful for making programs that work like standard Unix utilities, with respect to the invocation arguments (which we'll see in a moment). If you want to make a Perl program that can be used like the utilities cat, sed, awk, sort, grep, lpr, and many others, the diamond operator will be your friend. If you want to make anything else, the diamond operator probably won't help."

      diamond.pl

      #!/usr/bin/env perl while (<>) {print}

      data.txt

      foo bar nose cuke

      Run it: ./diamond.pl data.txt

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      Furthermore I consider that Donald Trump must be impeached as soon as possible

Re^2: How to read the input directory from command line arguments using perl?
by finddata (Sexton) on Mar 22, 2017 at 04:44 UTC
    Here what is meant by $otherscript?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2020-06-04 20:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?