Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# equivalent of perl's $_

Tags:

c#

perl

Is there an equivalent to Perl's $_ function? I'm rewriting some old perl scripts in C# and I never learned any perl. Heres an example of what i'm trying to figure out

sub copyText {
        while($_[0]){
            $_[1]->Empty();
            $_[0] = $_[1]->IsText();
            sleep(1);
         }
like image 936
pyCthon Avatar asked Nov 15 '12 20:11

pyCthon


1 Answers

First of all, $_ is not a function. It's just an ordinary variable (that happens to be read and changed by a lot of builtins).

Second of all, the code you posted does not use $_. It's accessing elements of @_, the parameter list.

A more more readable version of the code you posted would be:

sub copyText {
   my ($arg1, $arg2) = @_;
   while ($arg1) {
      $arg2->Empty();
      $arg1 = $arg2->IsText();
      sleep(1);
   }

   $_[0] = $arg1;   # arg1 is passed by reference
}
  • arg1 is a boolean passed by reference.
  • arg2 is some kind of object with a method named Empty and one named IsText.

Sorry, I don't know C#, but hopefully you can move on with this.

like image 59
ikegami Avatar answered Oct 04 '22 05:10

ikegami