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

dear monks, i had been using some linux commands like grep and cat, but it seems as they doesn't take any variable except $_. this code doesn't work but when i replace it with $_ , it works. 1 more thing i had to ask is how many $_ variables can be used simultaneously and how. thanks for sharing your knowledge

foreach $keye (@key_en) { $linb=`cat /home/vikash/pro_1/en_1000 | grep --regexp '$keye'`; print "$linb\n"; }

Replies are listed 'Best First'.
Re: why does it not work???
by kennethk (Abbot) on Jul 14, 2011 at 17:17 UTC
    Your issue is unclear to me - please read I know what I mean. Why don't you?. Specifically, providing input, expected output and a full script that replicates your problem would be helpful. Given that you are apparently not using strict, I suspect the reason you are having trouble with named variables is typographic in nature. See Use strict warnings and diagnostics or die.

    foreach $keye (@key_en) { $linb=`cat /home/vikash/pro_1/en_1000 | grep --regexp '$keye'`; print "$linb\n"; }

    and

    foreach (@key_en) { $linb=`cat /home/vikash/pro_1/en_1000 | grep --regexp '$_'`; print "$linb\n"; }

    are functionally equivalent. Why do you think it doesn't work?

    1 more thing i had to ask is how many $_ variables can be used simultaneously and how.

    There can only be one variable of any name at a given location in a script, so there is only one variable named $_.

    As a side note, there is no reason to pipe cat into grep. You can save characters and possible confusion by writing that as

    $linb=`grep --regexp '$keye' /home/vikash/pro_1/en_1000`;