0

Hello I have a question with perl.

Now I have a code that calls a perl code from c#. Now what if I need to pass some values to perl code?

string arguments = @"C:\Users\Desktop\test.pl";

Process myProcess = Process.Start(fullPath, arguments);

myProcess.WaitForExit(999);

if (!myProcess.HasExited)
{

  myProcess.Kill();

  throw new Exception("Timed out while running process");

}

thank you very much

2 Answers 2

1

this is my c# code and perl code. to simplify, it just passes two variable and print to txt file.

namespace testperl

{

class Program

{

    static void Main(string[] args)
    {
        string fullPath = @"C:\Perl64\bin\perl.exe";

        string arguments = @"C:\Users\Desktop\old.pl"+"aa" + "bb";

        Process myProcess = Process.Start(fullPath, arguments);

        myProcess.WaitForExit(999);

        if (!myProcess.HasExited)
        {

            myProcess.Kill();

            throw new Exception("Timed out while running process");

        }

        Console.WriteLine("all set");

        Console.ReadKey();

    }
}

the perl code:

use strict;

use warnings;

use Carp;

use English qw(-no_match_vars);

my $files = 'report.txt';

if (-f $files) {
unlink $files
    or croak "Cannot delete $files: $!";
}

my $OUTFILE;

my ($name, $number) = @ARGV;

if (not defined $name) {

   die "Need name\n";
 }


 open $OUTFILE, '>>', $files

 or croak "Cannot open $files: $OS_ERROR";

 if (defined $number) {

    print { $OUTFILE } "thing, $name \n"

    or croak "Cannot write to $files: $OS_ERROR";

}

   close $OUTFILE

   or croak "Cannot close $files: $OS_ERROR";
Sign up to request clarification or add additional context in comments.

1 Comment

Look at your arguments variable in c# code. You need spaces after filename old.pl aa bb
0

Just pass in the arguments as

string arguments = @"C:\Users\Desktop\test.pl ARG1 ARG2";

(and then you can access them in Perl using @ARGV)

3 Comments

I am quite new to perl, is that a way that you can assign ARG1 and ARG2 to different variables in perl?
@ARGV is an array of comand line args provided when the script executed. See e.g. perlmaven.com/argv-in-perl
Hello rbm, I just pasted the code and still it does not work. Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.