Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Command-line arguments to command-line Perl

by NetWallah (Canon)
on Oct 03, 2010 at 20:35 UTC ( #863204=note: print w/replies, xml ) Need Help??

in reply to Command-line arguments to command-line Perl

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

  • Comment on Re: Command-line arguments to command-line Perl

Replies are listed 'Best First'.
Re^2: Command-line arguments to command-line Perl
by RecursionBane (Beadle) on Oct 03, 2010 at 20:51 UTC

    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.


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

        Yes, you're right. I didn't quite understand what -p did. Thanks for your suggestion regarding perldoc! I have a better understanding of how -p and -i work. Also, -s seems interesting.
      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

        Thank you, suhailck!

        This tactic worked for me; I'd never have thought to use BEGIN() to stall the argument preprocessor until specific args could be gathered before allowing the program to proceed (and thereby still keeping the ability to auto-parse all supplied files "in-line")!

        This is what my one-liner now looks like:

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

        The main advantage here is that I can supply "ConfigFile1 ConfigFile2 ... ConfigFileN ADCIF" (or, indeed, "ConfigFile* ADCIF") to have the change applied to all of them. Thanks again for your solution!


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://863204]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2018-01-22 06:14 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (232 votes). Check out past polls.