Community
    • Login

    Perl subroutine calltips - with PythonScript

    Scheduled Pinned Locked Moved General Discussion
    52 Posts 4 Posters 3.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EkopalypseE
      Ekopalypse @PeterJones
      last edited by

      @PeterJones

      and then I used nmake to create the pm’s.
      I’ve copied the resulting Win32 to …/perl/lib/
      but now I get Can't locate Win32/API.pm in @INC (you may need to install the Win32::API module) (@INC contains: d:/perl/site/lib d:/perl/lib) at d:/perl/lib/Win32/Mechanize/NotepadPlusPlus/Notepad.pm line 9.

      1 Reply Last reply Reply Quote 1
      • PeterJonesP
        PeterJones @Ekopalypse
        last edited by

        @Ekopalypse ,

        The other thing I realized: you probably won’t have all the prerequisite modules installed. I normally use the cpanm utility, which distributes with strawberry perl, and that automatically downloads and installs prereqs before the module you’re trying to install. Without such a tool (and if you built your own perl, which I think you did to get the DLL), I don’t know that you have that.

        I think the make process will tell you what prereqs you’re missing … but it’s been a long time since I’ve done a complicated manual install of a module.

        Oh, yep, there, you posted the error while I was typing. You’re going to have to install at least Win32::API first… doing that manually is a pain.

        When you built your own perl, did you at least get cpan, if not cpanm? I think one or both would be in the same directory as perl.exe… if you have cpanm, just do cpanm Win32::Mechanize::NotepadPlusPlus, and everything should work. If you have just cpan, I forget whether it handles prereqs; try cpan Win32::Mechanize::NotepadPlusPlus; if that’s not sufficient, do the missing prereqs one at a time with the cpan client. If you have neither… um… That’s going to be tough.

        Michael VincentM 1 Reply Last reply Reply Quote 1
        • Michael VincentM
          Michael Vincent @PeterJones
          last edited by

          @PeterJones said in Perl subroutine calltips - with PythonScript:

          If you have just cpan, I forget whether it handles prereqs

          I believe it does - I normally just use CPAN and it fetches what I need to build.

          Cheers.

          1 Reply Last reply Reply Quote 1
          • EkopalypseE
            Ekopalypse
            last edited by

            I have cpan and it keeps installing stuff - did I choose to install the world??

            1 Reply Last reply Reply Quote 0
            • EkopalypseE
              Ekopalypse
              last edited by

              it does this now since ~10 minutes

              1 Reply Last reply Reply Quote 0
              • EkopalypseE
                Ekopalypse
                last edited by

                finished - but no Win32::API. started cpan Win32::API now.

                1 Reply Last reply Reply Quote 0
                • Michael VincentM
                  Michael Vincent
                  last edited by

                  @Ekopalypse
                  @PeterJones

                  Is there a reason you build your own Perl? I have Strawberry 5.24 installed and doing this works:

                  970a1b75-52c3-4044-b6aa-e253bb3e979c-image.png

                  That’s @Ekopalypse script from above using my “system” Strawberry Perl 5.24 DLL (the Perl that has my Win32::Mechanize::NotepadPlusPlus and all my other stull installed on it). Not sure why it’s printing “9” in the PythonScript console - is that what it’s supposed to do? I thought I’d get that string "print something ...

                  Cheers.

                  EkopalypseE 1 Reply Last reply Reply Quote 1
                  • EkopalypseE
                    Ekopalypse @Michael Vincent
                    last edited by Ekopalypse

                    @Michael-Vincent

                    I get also 9 and from the function it seems correct to return an int.
                    See my previous post. RunPerl returns only an int. Not sure how to get the result (the print output), so far.

                    Michael VincentM 1 Reply Last reply Reply Quote 1
                    • Michael VincentM
                      Michael Vincent @Ekopalypse
                      last edited by

                      @Ekopalypse said in Perl subroutine calltips - with PythonScript:

                      I get also 9

                      Oh, OK - great then. Seems I may be able to keep testing along without having to build a Perl and just use my Strawberry install.

                      Cheers.

                      EkopalypseE 1 Reply Last reply Reply Quote 1
                      • EkopalypseE
                        Ekopalypse @Michael Vincent
                        last edited by

                        @Michael-Vincent

                        My Win32::API has been built but not installed because of failing tests I guess. Can you try the "use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();" and see what happens?

                        Michael VincentM 2 Replies Last reply Reply Quote 1
                        • Michael VincentM
                          Michael Vincent @Ekopalypse
                          last edited by

                          This post is deleted!
                          1 Reply Last reply Reply Quote 0
                          • Michael VincentM
                            Michael Vincent @Ekopalypse
                            last edited by Michael Vincent

                            @Ekopalypse said in Perl subroutine calltips - with PythonScript:

                            Can you try

                            from ctypes import CDLL, POINTER, c_int, c_char_p
                            
                            perllib = CDLL(r'C:\Strawberry\perl\bin\perl524.dll')
                            
                            ["-le", "use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"]
                            
                            perllib.RunPerl.restype = c_int
                            perllib.RunPerl.argtypes = c_int, POINTER(c_char_p), POINTER(c_char_p)
                            args = (c_char_p * 2)(b"-le", b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();")
                            print(perllib.RunPerl(len(args),args, None))
                            

                            It just prints 9 in the PythonScript console. No new tab is opened. If I run the command itself from a prompt:

                            C:\ > perl -e "use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"
                            

                            I get a new tab in Notepad++ as expected.

                            At least if it printed 42 we’d know we’re on to something …

                            Cheers.

                            EkopalypseE 1 Reply Last reply Reply Quote 2
                            • EkopalypseE
                              Ekopalypse
                              last edited by

                              Ja, 42 would be nice :-)
                              So 9 seems to be an error code.

                              1 Reply Last reply Reply Quote 1
                              • EkopalypseE
                                Ekopalypse @Michael Vincent
                                last edited by Ekopalypse

                                @Michael-Vincent

                                Can you try one more thing?
                                Replace the RunPerl call with this
                                print(perllib.RunPerl(len(args), args, (c_char_p * 1)(b'')))
                                With the newFile code, please.

                                Michael VincentM 1 Reply Last reply Reply Quote 1
                                • Michael VincentM
                                  Michael Vincent @Ekopalypse
                                  last edited by

                                  @Ekopalypse

                                  Same 9.

                                  I tried both:

                                  from ctypes import CDLL, POINTER, c_int, c_char_p
                                  
                                  perllib = CDLL(r'C:\Strawberry\perl\bin\perl524.dll')
                                  
                                  ["-e", "use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"]
                                  
                                  perllib.RunPerl.restype = c_int
                                  perllib.RunPerl.argtypes = c_int, POINTER(c_char_p), POINTER(c_char_p)
                                  args = (c_char_p * 2)(b"-e", b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();")
                                  print(perllib.RunPerl(len(args),args,(c_char_p * 1)(b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();")))
                                  

                                  and

                                  from ctypes import CDLL, POINTER, c_int, c_char_p
                                  
                                  perllib = CDLL(r'C:\Strawberry\perl\bin\perl524.dll')
                                  
                                  ["-e", "use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"]
                                  
                                  perllib.RunPerl.restype = c_int
                                  perllib.RunPerl.argtypes = c_int, POINTER(c_char_p), POINTER(c_char_p)
                                  args = (c_char_p * 2)(b"-e", b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();")
                                  print(perllib.RunPerl(len(args),args,(c_char_p * 1)(b'')))
                                  

                                  HOWEVER … this:

                                  from ctypes import CDLL, POINTER, c_int, c_char_p
                                  
                                  perllib = CDLL(r'C:\Strawberry\perl\bin\perl524.dll')
                                  
                                  ["use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"]
                                  
                                  perllib.RunPerl.restype = c_int
                                  perllib.RunPerl.argtypes = c_int, POINTER(c_char_p), POINTER(c_char_p)
                                  args = (c_char_p * 1)(b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();")
                                  print(perllib.RunPerl(len(args),args,None))
                                  

                                  produces 0 in the PythonScript console.

                                  Cheers.

                                  1 Reply Last reply Reply Quote 2
                                  • EkopalypseE
                                    Ekopalypse
                                    last edited by

                                    Ok, I should have installed strawberry perl in first place, solved all my issues.

                                    First success

                                    from ctypes import CDLL, POINTER, c_int, c_char_p
                                    
                                    perllib = CDLL(r'D:\strawberry\perl\bin\perl532.dll')
                                    
                                    perllib.RunPerl.restype = c_int
                                    perllib.RunPerl.argtypes = c_int, POINTER(c_char_p), POINTER(c_char_p)
                                    
                                    __args = [b"",
                                              b"-le",
                                              b"use Win32::Mechanize::NotepadPlusPlus qw/:main/; notepad->newFile();"]
                                              
                                    args = (c_char_p * len(__args))(*__args)
                                    x = perllib.RunPerl(len(args), args, None)
                                    print(x)
                                    

                                    This works, but only one time. A second call results in a
                                    OSError: exception: access violation writing 0x0000000000000024

                                    Seems some cleanup needs to be done afterwards.

                                    Michael VincentM PeterJonesP 2 Replies Last reply Reply Quote 2
                                    • Michael VincentM
                                      Michael Vincent @Ekopalypse
                                      last edited by Michael Vincent

                                      @Ekopalypse said in Perl subroutine calltips - with PythonScript:

                                      Seems some cleanup needs to be done afterwards.

                                      Yes, in my late night Google-ing I saw lots of references to free(args) after the RunPerl() call.

                                      Examples:
                                      https://comp.lang.perl.misc.narkive.com/r7M6eENL/dll-unload-question-for-embedded-perl-on-windows
                                      https://www.nntp.perl.org/group/perl.wxperl.users/2017/01/msg9715.html

                                      Cheers.

                                      1 Reply Last reply Reply Quote 2
                                      • PeterJonesP
                                        PeterJones @Ekopalypse
                                        last edited by

                                        @Ekopalypse said in Perl subroutine calltips - with PythonScript:

                                        __args = [b"", …

                                        Weird. If I don’t have the empty zeroth argument, the call fails (x==9). I wonder why it needs the blank argument…

                                        Michael VincentM 1 Reply Last reply Reply Quote 2
                                        • Michael VincentM
                                          Michael Vincent @PeterJones
                                          last edited by

                                          @PeterJones said in Perl subroutine calltips - with PythonScript:

                                          I wonder why it needs the blank argument…

                                          I read some stuff on Par::Packer and it seems the first argument may be the optional path to the perl executable.

                                          https://oliverbetz.de/pages/Artikel/Portable-Perl-Applications

                                          Cheers.

                                          1 Reply Last reply Reply Quote 2
                                          • EkopalypseE
                                            Ekopalypse
                                            last edited by

                                            Hmm … it looks like freeing the interpreter is the issue.
                                            I tried to replicate what RunPerl is doing and when I use this

                                            from ctypes import CDLL, POINTER, c_int, c_char_p, c_void_p, byref
                                            
                                            perllib = CDLL(r'D:\strawberry\perl\bin\perl532.dll')
                                            
                                            # perllib.RunPerl.restype = c_int
                                            # perllib.RunPerl.argtypes = [c_int, POINTER(c_char_p), POINTER(c_char_p)]
                                            
                                            # Perl_sys_init3(int* argc, char*** argv, char*** env)
                                            perllib.Perl_sys_init3.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p)), POINTER(POINTER(c_char_p))]
                                            
                                            # PerlInterpreter * perl_alloc(void)
                                            perllib.perl_alloc.restype = c_void_p
                                            perllib.perl_alloc.argtypes = []
                                            
                                            # void perl_construct(pTHXx)
                                            perllib.perl_construct.argtypes = [c_void_p]
                                            
                                            # int perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)  # only 4 params ??
                                            perllib.perl_parse.restype = c_int
                                            perllib.perl_parse.argtypes = [c_void_p, c_void_p, c_int, POINTER(c_char_p), POINTER(c_char_p)]  # we need 5 params according to RunPerl
                                            
                                            # int perl_run(pTHXx)
                                            perllib.perl_run.restype = c_int
                                            perllib.perl_run.argtypes = [c_void_p]
                                            
                                            __args = [b"", b"D:\\scripts\\perl\\1.pl" ]
                                            # ********************************  content of 1.pl  ********************************
                                            # use strict;
                                            # use warnings;
                                            
                                            # my $timestamp = localtime(time);
                                            
                                            # sub logit {
                                            	# my $message = shift;
                                            	# my $filename = 'D:/report.txt';
                                            	# open(my $fh, '>>', $filename) or die "Could not open file '$filename' $!";
                                            	# print $fh $timestamp, " $message\n";
                                            	# close $fh;
                                            # }
                                            
                                            # logit("test");
                                            # **********************************************************************************
                                            
                                            
                                            args = (c_char_p * len(__args))(*__args)
                                            
                                            perllib.Perl_sys_init3(byref(c_int(len(args))), None, None)
                                            my_perl = perllib.perl_alloc()
                                            perllib.perl_construct(my_perl)
                                            result = perllib.perl_parse(my_perl, None, len(args), args, None)
                                            print('perl_parse', result)
                                            result = perllib.perl_run(my_perl)
                                            print('perl_run', result)
                                            print(open(r'D:\report.txt', 'r').read())
                                            
                                            

                                            I can run 1.pl multiple times

                                            6adc4473-18cb-43e8-acb2-48c5cb32b519-image.png

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            The Community of users of the Notepad++ text editor.
                                            Powered by NodeBB | Contributors