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

Hi, i like to write one liner of perl code in UNIX. Something is as such: cat file | perl -n0e 'perl code here' It works good. One problem i don't know how to pass variable into perl code inside ''. Thanks,

Replies are listed 'Best First'.
Re: one line perl code
by thelenm (Vicar) on May 08, 2002 at 20:37 UTC
    Probably the easiest way is to give Perl an argument, like this:
    cat file | perl -n0e 'BEGIN{$var = shift} #more code' myvar
    Then $var will contain the value 'myvar'. You might also use an environment variable accessible via the %ENV hash, like this:
    VAR=myvar; export VAR; cat file | perl -n0e 'BEGIN{$var = $ENV{VAR}} # +more code'
    I hope this is a useful answer to your question.
      Thank you very much. It really works. Your support makes me like perl even more. I can't wait that day i can help other people too. Have a nice day!
Re: one line perl code
by BazB (Priest) on May 08, 2002 at 20:39 UTC

    perldoc perlrun, particularly the -s option. </p.

    If things get too fancy - write a real script instead of a oneliner.