Mutual call collection between VC and Perl

Source: Internet
Author: User
Tags perl interpreter readfile install perl perl script

Call the Perl subroutine in VC
Author: Source: ZZ published on: Views: 2691 font size: large medium small

As needed, you need to call the Perl subroutine in VC to process the file. You can run it after reading the Perl instruction document for a long time. I would like to share with you some help ^-^
1. Add perl58.dll to the project file

2. Add the header file to VC.
# Include <extern. h>
# Include <Perl. h>

3.
Void perl_sub (char *)
{
Int retval;
DSP;
Enter;
Savetmps;
Pushmark (SP );
Xpushs (sv_2mortal (newsvpv (A, 0 )));
Putback;
Retval = call_pv ("readfile", g_array );
Spagain;
For (INT I = 1; I <= retval; ++ I)

{Int T = popl;
Printf ("Value % d = % d \ n", I, T );
}
Putback;

Freetmps;
Leave;

}

Int main (INT argc, char ** argv, char ** env)
{
Char * ARGs [] = {"", "sub. pl "};
Perl_sys_init3 (& argc, & argv, & env );
My_perl = perl_alloc ();
Perl_construct (my_perl );

Perl_parse (my_perl, null, argc, argS, (char **) null );
Pl_exit_flags | = perl_exit_destruct_end;
Perl_sub ("test.txt ");
Perl_destruct (my_perl );
Perl_free (my_perl );
Perl_sys_term ();
Int I;
Scanf ("% d", & I );
}/////////////////////////////////////// //// Call Perl to complete

Note: Sub. pl content

Sub readfile
{

My ($ S) = @_;
# My ($ S) = "test.txt ";
Open (FH, $ s) | die "can't open FH; $! "; # Read text files
My @ readstr = <FH>;
# Print @ readstr;

$ Filename = "22.bin ";
Open (in, ">: Raw", $ filename) or die "cannot open $ filename! \ N "; # Open (in," >>: raw ", $ filename) is an append write.
Seek (in, 0, seek_set );
Foreach my $ one (@ readstr) # process each row
{
# Chop ($ one );
My @ words3 = Split (/\ s +/, $ one );

If ($ words3 [12] EQ "green light ")
{$ Words3 [12] = 0 ;}
If ($ words3 [12] EQ "green yellow light ")
{$ Words3 [12] = 1 ;}
# Print "\ n ";

# Print @ words3;

Print in pack ("n16", @ words3 );

}

# Close (in) or die "cannot close \ n ";
}

Additional:
1. Embed Perl fragments in VC
# Include "stdafx. H"
# Include <extern. h>
# Include <Perl. h>
Static perlinterpreter * my_perl;
Main (INT argc, char ** argv, char ** env)
{
Strlen N_a;
Char * embedding [] = {"", "-e", "0 "};
Perl_sys_init3 (& argc, & argv, & env );
My_perl = perl_alloc ();

Perl_construct (my_perl );
Perl_parse (my_perl, null, 3, embedding, null );
Pl_exit_flags | = perl_exit_destruct_end;
Perl_run (my_perl );
// Eval_pv ("$ A = 3; $ A ** = 2", true );
Eval_pv ("$ A = 3; $ A ** = 2", true );
Printf ("A = % d \ n", sviv (get_sv ("A", false )));
Eval_pv ("$ A = 3.14; $ A ** = 2", true );
Printf ("A = % F \ n", svnv (get_sv ("A", false )));
Eval_pv ("{$ A = 'rekcah lrep rehtona tsuj'; $ A = reverse ($ A) ;}", true );
Printf ("A = % s \ n", svpv (get_sv ("A", false), N_a ));

Eval_pv ("Print 'Please enter first number: '; $ number1 = <stdin>; chomp $ number1; $ number2 = <stdin>; chomp $ number2; $ sum = $ number1 + $ number2; ", true );
Printf ("sum = % F \ n", svnv (get_sv ("sum", false )));
Eval_pv ("Print 'hello'", true );
// Eval_pv ("My $ today = new date; $ today-> setdate (7, 14,200 0); print ($ today-> month ()); print ('\ n'); $ today-> Print (); print (' \ n'); ", true );

// Eval_pv ("use strict; $ A = 'rekcah lrep rehtona tsuj'; $ A = reverse ($ A);", true );
// Printf ("A = % s \ n", svpv (get_sv ("A", false), N_a ));

Perl_destruct (my_perl );
Perl_free (my_perl );
Perl_sys_term ();
Int I;
Scanf ("% d", & I );

}
2. Embed C in Perl
Use inline C;
{
# $ A = 5;
# $ B = 19;
@ C = (9, 19 );
Print "begin \ n ";
Print "9 + 19 =", add (@ C, "string"), "\ n ";
Print "9-16 =", subtract (9, 16), "\ n ";
Print "End \ n ";
}
_ End __
_ C __
Int add (int A, int B, char * z ){
Int words [2] = {0, 0 };
Words [0] =;
Words [1] = B;

Printf ("% s", Z );
Return words [0] + words [1];

}
Int subtract (int x, int y ){
Return x-y;

}

Today, we need to rewrite some Perl code to generate an incremental number. To meet the test requirements, we need to constantly run to generate a new 1 million random number. Because the Starting number is different, you need to modify the code each time and then run it (or press the command to pass it ). If I use it only, it will not be too troublesome. However, it is inconvenient for teammates to use it. Use VC to make an interface and call the Perl script in the C/C ++ code to make it a dumb-based one...

 

   To be honest, I really don't know how to use C/C ++ to call Perl? (Of course, C/C ++ is in the vs environment ).

 

   Search online and find out a simple instruction document (the author forgot, don't blame me) with three points in csdn ). The procedure is as follows:

1. engineering settings in:

Tools-> options-> projects and solutions-> VC ++ directory, select "include files" in the "show the following directories (s):" drop-down box ", add * \ bin, * \ Lib \ core, and * to indicate the installation path of Perl.

 

Tools-> options-> projects and solutions-> VC ++ directory, select "library file" in the "show the following directories (s):" drop-down box ", add * \ Lib \ core, which indicates the installation path of Perl.

 

Add perl58.lib to the project, project-> "project" properties-> Configuration properties-> linker-> command line, add perl56.lib to the additional options (because the Perl version I use is 5.6)

2. Add the header file in the project code.

# Include "extern. H"   // Reference external header files
# Include "Perl. H"     // Perl header file

3.Project code

Int_tmain (INT argc, _ tchar * argv [])
{
//Char * embedding [] = {"", "C: \ data. PL "," e :\\ dumptobin "}; // Perl script and parameters. The first one is empty with no space,
                                                       // The second is the script file, and the third is the input parameters of the script.
   Char * embedding [] = {"", "C: \ perl2 \ createnumber. pl", ""}; // The real path of your Perl script. No parameter is input.

 Static perlinterpreter * my_perl; // defines a Perl interpreter.

 My_perl = perl_alloc (); // allocate resources to the interpreter

 Perl_construct (my_perl); // create an interpreter

 Perl_parse (my_perl, null, 3, embedding, null); // Perl Parsing
  // The perl_parse function is prototype:
  // Perl_callconv intperl_parse (perlinterpreter * interp, xsinit_t xsinit, int argc, char ** argv, char ** env)

 Perl_run (my_perl); // run

//Int STR = sviv (perl_get_sv ("lastvalue", false); // transmits the lastvalue variable in the Perl script to the STR variable in C. The statement is invalid.
//Printf ("Return Value: % d. \ n", STR );

   Perl_destruct (my_perl); // destructor

 Perl_free (my_perl); // release the interpreter

 Return 0;
}

In general. Others use vs as the interface box, and each user needs to do it by themselves. (Of course, you must install Perl first ).

Of course, this is still a VC program that is hard bound to a Perl script. If you want to implement the Program Interface (the console is currently used) and select the Perl script for running, hey, read a configuration file to pass the Script Name.

 

 

Reference page: (embed Perl in a C program)

Http://bbs.chinaunix.net/viewthread.php? Tid = 770248 ###

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.