Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

trying to use a varible in the perl replace command

by Anonymous Monk
on Jul 08, 2003 at 19:16 UTC ( [id://272396]=perlquestion: print w/replies, xml ) Need Help??

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

Hi , I am trying to use the following command: #!/bin/sh model="1200" perl -e 's/mymodel/$model/i' -p mytext.txt But I could not replace the value of $model in the string. Is there a way to do this. Thanks
  • Comment on trying to use a varible in the perl replace command

Replies are listed 'Best First'.
Re: trying to use a varible in the perl replace command
by sauoq (Abbot) on Jul 08, 2003 at 20:06 UTC

    Is there any reason that you are writing a shell script rather than making it a pure perl script in the first place?

    Assuming there is, using double quotes to allow shell interpretation (as suggested by the pusher robot) is one way to do it, but it could get messy if you find yourself wanting both shell and perl variables.

    You could set the variables directly in the perl rather than use shell variables in the first place.

    Or, you could write your perl one-liner to read them off the command line.

    perl -spe 's/mymodel/$model/' -- -model=1200 mytext.txt
    You could use -model=$model in that example. And maybe you'd like to avoid using the same variable names in both perl and the shell...
    perl -spe 's/mymodel/$arg/' -- -arg=$model mytext.txt

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: trying to use a varible in the perl replace command
by dragonchild (Archbishop) on Jul 08, 2003 at 19:18 UTC
    Try $ENV{model} instead.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      Or replace the single quotes with double quotes (so the shell can do var substitution).

      That won't work. His $model is a shell variable, not an environment variable. (It hasn't been exported.)

      -sauoq
      "My two cents aren't worth a dime.";
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (None)
    As of 2024-04-25 00:02 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found