Pass csh variable to Perl

Hi All,

I am trying to put the csh variable into a perl.
In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works.

Can anybody help me pls?

#!/bin/csh

set var = `echo "xxx"`

perl myperlcode.pl file

where the perl code is

#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

line: while (<>) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq '**' && $Fld[8] eq '2007' && $Fld[2] =~ /'"$var"'/) {
        print $_;
            }
 

Input File:

ddddd
ppp
** xxx qqqqq qqq qq qq qq 2007
jjjj
kkkk
lll
ppp

Expected Output:
** xxx qqqqq qqq qq qq qq 2007

Unless you are passing the value as an argument you will most probably have to export it.

Also, in perl try some form of getenv().

Hi Porter,

Can you explain what is meant by passing by arguement? Any examples?

On the export part , any examples?

just like you did with "file".

apparently "setenv" does the trick.

On the Perl side that is

$ENV{'var'}

Of course you have to export it from the shell for Perl to see it.

Hi cbkihong,

Do you mean the below? I encounter some error though.
I do not know how to export. Can you give me some guidance or show me an example ?

#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$ENV('var')

line: while (<>) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq '**' && $Fld[8] eq '2007' && $Fld[2] =~ /'"$var"'/) {
        print $_;
            }
}

It's curly braces {}, not parentheses. Please retry, and if there is any errors, please quote them if you post again.

Hi cbkihong,

There's no output after the modification. Can you help ?

#!/usr/local/bin/perl
$[ = 1;                 # set array base to 1
$, = ' ';               # set output field separator
$\ = "\n";              # set output record separator

$ENV{'var'};

line: while (<>) {
    chomp;      # strip record separator
    @Fld = split(' ', $_, 9999);
    if ($Fld[1] eq '**' && $Fld[8] eq '2007' && $Fld[2] =~ /'"$var"'/) {
        print $_;
            }
}

My csh is named as "trying"

$trying
$

First make sure your 'var' shell variable can be passed to Perl. If "print $ENV{'var'}" in Perl doesn't print anything, you can stop playing with the Perl side because you shell simply cannot export var to the Perl process.

Hi,

It seems that "print $ENV{'var'}" doesn't print anything. :frowning:
Looks like i have to find another way to do it