Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Command-line arguments to command-line Perl

by RecursionBane (Sexton)
on Oct 03, 2010 at 20:04 UTC ( #863203=perlquestion: print w/ replies, xml ) Need Help??
RecursionBane has asked for the wisdom of the Perl Monks concerning the following question:

Greetings, Monks!

Long have I assimilated knowledge from you, unseen. I emerge from the shadows of Lurkwood to place this query before you:

How do I provide command-line arguments to a command-line Perl call (from a tcsh script, no less)?

The purpose of this operation is to replace this line in ConfigFile:

'def' => '',

With this:

'def' => 'dumper/$tcsh_input',

(Where $tcsh_input is actually a command-line argument to the tcsh script)

Here is what I am attempting now in my tcsh script:

perl -pi -e '$design = $ARGV[0] ; s/.*'def'.*''/\t"def" => "dumper\/$design.def",/g' ConfigFile $tcsh_input

Any arguments after the -e switch, however, seem to be treated as input files, so I see this:

>> perl -pi -e '$design = $ARGV[0] ; s/.*'def'.*''/\t"def" => "dumper +\/$design.def",/g' ConfigFile ADCIF Can't open ADCIF: No such file or directory, <> line 194.

Is there a separate switch I can use before -e to represent the arguments (accessible through @ARGV) to the program?

Kindly bless me with your wisdom, so that I may express my gratitude and scurry back into the shadows from whence I came; all this light is hurting my eyes.

~Recursion

Comment on Command-line arguments to command-line Perl
Select or Download Code
Replies are listed 'Best First'.
Re: Command-line arguments to command-line Perl
by NetWallah (Abbot) on Oct 03, 2010 at 20:35 UTC
    Your shell is messing with the quote characters.

    THis looks like linux, since you use single quotes after the -e.

    In this case, you need to escape (for the shell) every other single quote till the end of the argument.

    What I usually do to avoid the isue is to use "qq" or "q" to qote the pieces that need quoting.

         Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

      Thanks for the response, NetWallah!

      This is, indeed, Linux. Rather than escape-character woes, I think the problem I'm facing is because everything after the -e <program code> switch is treated as an input file (and not an argument to the Perl program itself).

      In my code example, I would like $ARGV[0] to contain a certain value (ADCIF, in this case):

      perl -pi -e '$design = $ARGV[0] ; s/.*'def'.*''/\t"def" => "dumper\/$design.def",/g' ConfigFile $tcsh_input

      This code should behave like so:

      perl -pi -e '$design = "ADCIF" ; s/.*'def'.*''/\t"def" => "dumper\/$design.def",/g' ConfigFile

      Here, I am trying to provide "ADCIF" to the perl script on-the-fly (which is why I need the argument to be contained in @ARGV).

      I apologize if my initial question was unclear. Please let me know if you have any other suggestions.

      ~RecursionBane

        I think you don't really understand what -p does.

        I recommend that you read about it in "perldoc perlrun".

        In your example you seem to expect that "ConfigFile" is treated as the name of the file that -p workes on and all the other command-line arguments go to @ARGV.

        This is wrong.

        What happens is that ALL arguments (including "ConfigFile") go to @ARGV but then are treated (via -p's magic) as filenames. This is why you get the error.

        So you have to pass your data to perl via some other mechanism if you want to use -p (I would use an environment variable).

        This seems to be working
        cat test 1 2 3 4 5 perl -pe 'BEGIN{$prefix=pop;}s/(\d+)/$prefix.$1/' test A A.1 A.2 A.3 A.4 A.5
Re: Command-line arguments to command-line Perl
by BrowserUk (Pope) on Oct 04, 2010 at 00:57 UTC

    Take a look at Perl's -s option.

    Ignoring any quoting issues, something like this should work:

    perl -pi -se 's/.*'def'.*''/\t"def" => "dumper\/$D.def",/g' -D=ADCIF C +onfigFile

    Note: That -D=... becomes $D inside the script.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Thanks for the suggestion, BrowserUk. I wasn't able to get this to work, because perl seemed to think that the argument was meant for it, rather than the script, and I ended up using suhailck's BEGIN() solution, instead.

      I do appreciate everyone's time and effort in helping me understand one of Perl's lesser-known functionalities! Thanks, everyone!

      ~RecursionBane

        I wasn't able to get this to work, because perl seemed to think that the argument was meant for it, rather than the script,

        Sorry. I don't often use -s with -e.

        You have to use -- to delineate between switches meant for perl, and those meant for the -e script:

        perl -pi -se's/.*'def'.*''/\t"def"=>"dumper\/$D.def",/g' -- -D=ADCIF C +onfigFile

        Not very practical on the command line, but useful in shell scripts.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Command-line arguments to command-line Perl
by eyepopslikeamosquito (Chancellor) on Oct 03, 2010 at 22:21 UTC

    I'm curious as to why you need to write a tcsh script at all. Writing scripts in csh (or tcsh) is generally frowned upon; see Csh Programming Considered Harmful for the gory details. Good old /bin/sh is more portable and a generally sounder programming language.

    If you possibly can get away with it, however, I strongly urge you to consider writing the whole thing in Perl.

      eyepopslikeamosquito, I agree with you on this point. I usually script entirely in Perl and shy away from shell scripts on principle.

      In this instance, however, it was necessary to write a one-liner for inclusion in a previously existing shell script that couldn't have dependencies on external Perl scripts. An installation of Perl, however, is guaranteed on all target systems.

      ~RecursionBane

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2015-07-29 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls