Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

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

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

I have the project.txt files at following directory (/home/data/check_ +data). project.txt contins following contents: <code> PROJECT { NAME:DCMS_DEMO BLOCKS:test,de_top,new_block2 DENY_USERS: WELCOME: REVISION_LOCATION:../setup/projects/DCMS_DEMO/project_space CHECKLISTS:Synthesis_L2_checklist,Block_Physical_design_checklist,ICC_ +L2_Checklist,Overall_DV,Formality_DCT_Vs_ICC,Fullchip_Physical_design +_checklist,Formality_RTL_Vs_DCT,synthesis,Timing_analysis_checklist,c +hecklist_tmp,Power_analysis_checklist,Block_DV,DFT,ICC_Top_level_Chec +klist,test_copy_checklist,ICC_DPMHV_Checklist,CAD_checklist,FrontEnd_ +Design_Checklist DESCRIPTION:Demonstration project OWNER:sanandak ALLOWED_UNIX_GROUPS: }
Now i like to read the directory whereever the line starts with (REVISION_LOCATION:) from the project.txt files.And those directories should be consider as my input directory from the command line arguments.How can i do it. code which i had tried are as follows:
#! /usr/bin/env perl use strict; use warnings; use Getopt::Long qw( GetOptions ); GetOptions( 'root=s' => \my $input_dir, 'outdir=s' => \my $output_dir, 'file=s' => \my $mapfile, ) In the above i called the directory straightly from the location but m +y question is first i should read the project.txt file from one locat +ion and from that project.txt file i should read the directory(i.e t +he line starts with REVISION_LOCATION).How can i script it using perl +?

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.


      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> ./ --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 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
      ./ --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 project.txt", or by feeding it into the script's STDIN, as in "cat project.txt | perl".

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

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


        foo bar nose cuke

        Run it: ./ 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?
Re: How to read the input directory from command line arguments using perl?
by huck (Parson) 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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2020-09-29 14:30 GMT
Find Nodes?
    Voting Booth?
    If at first I don’t succeed, I …

    Results (146 votes). Check out past polls.