Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by finddata (Sexton)
on Mar 22, 2017 at 03:56 UTC ( [id://1185394]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to read the input directory from command line arguments using perl?
by davido (Cardinal) on Mar 22, 2017 at 04:27 UTC

    It seems like what you are asking is how to provide a default arg to the command line in the case that a command line field is not populated. ...or maybe you're asking how to parse your project.txt file, though that seems less likely since your question never explicitly mentioned that. So let's go with "I have a script that should use a default command line arg value when one isn't passed in explicitly."

    use strict; use warnings; my $file = "foo.txt"; # Set a default. use Getopt::Long; GetOptions( 'file=s' => \$file, # Default will be used unless --file ... appea +rs on command line. ); print $file, "\n"; # prints 'foo.txt' unless the command line explicit +ly set something else.

    If that's not what you're asking, please step back from your problem long enough to re-read your question, imagining you are someone who has no context on what you're trying to do, and rephrase the question in a way that would still make sense.

    Also, you still seem to be struggling with how to format your posts here. Have a look at Writeup Formatting Tips. Hopefully that will clear things up for you.


    Dave

      The things which i want to convey are as follows: 1.i have to read the project.txt file from one directory and consider +the relative path from the line which statrts with REVISION_LOCATION: + from the text file.Then my relative path should be considered as my +input_dir(i.e --root) for my following execution. <br> ./html.pl --root=/home/data/check_data/project.txt --outdir=/home/data +/generate --mapfile=/home/data/check_data/mapfile.txt </br>
        The perl script won't be executed in HTML code!!! See here ...
Re: How to read the input directory from command line arguments using perl?
by huck (Prior) on Mar 22, 2017 at 04:18 UTC

    ../setup/projects/DCMS_DEMO/project_space is a relative directory. your first problem is determining what it is relative to.

      Am not getting what you mean to convey
Re: How to read the input directory from command line arguments using perl?
by bart (Canon) on Mar 22, 2017 at 04:28 UTC
    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.

      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.

      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".

      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

      Here what is meant by $otherscript?

Log In?
Username:
Password:

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

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

    No recent polls found