Dear monks, I have a shell script with a perl oneliner in it, and want to pass a shell variable to this oneliner.
The idea is this:
#!/bin/bash file=$1 var=$2 cat $1 | perl -e 'perlcodeinhere' >output
The question is how to get the $var variable into the perl code since I apparently can not get in is as regular argument while using the -e parameter.

Rgds, Ole C.

Update:
Thanks for the input, several nice ways of doing it.
I will also be sure to utilize the perl pie construct in the future, that is most certainly usefull for quite a few of my daily tasks.

Another Update:
As Bart points out it is possible to have the variable as a regular argument. What kept me from realising that was that I was using a while(<>) loop inside my code, my entire oneliner was like this:
cat $i | perl -e 'while(<>){$_=~ s/$ARGV[0]//g;print;}' $var >$i.new
and that did not work. I guess that is because the while(<>) expects to find files to work on in the @ARGV list. When I changed it to
cat $i | perl -e 'while(<STDIN>){$_=~ s/$ARGV[0]//g;print;}' $var >$i. +new
it worked the way I expected.

In reply to arguments to perl -e by olecs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.