如何在不使用命令行的情况下运行Perl脚本?

时间:2022-11-08 01:07:59

I have written a few Perl scripts that allow me to do my job faster. Each of these scripts is run with a command like this:

我已经编写了一些Perl脚本,允许我更快地完成工作。每个脚本都使用如下命令运行:

perl PerlScript.pl InFile > OutFile

Both InFile and OutFile are generally tab-delimited files that the rest of my workplace only sees as Excel file. My coworkers would benefit from using these scripts but they are intimidated by the idea of running programs from a command line. Is there some way to run a Perl script from a more user-friendly interface like a GUI? My ideal solution would allow them to drag an Excel file into a folder and click a button with the mouse which would produce a new Excel file they could drag back to wherever they need it.

InFile和OutFile通常都是用制表符分隔的文件,在我的工作环境中,其他人只看到Excel文件。我的同事将从使用这些脚本中获益,但他们对从命令行运行程序的想法感到害怕。是否有某种方法可以从更用户友好的界面(如GUI)运行Perl脚本?我理想的解决方案是让他们将一个Excel文件拖到一个文件夹中,然后用鼠标点击一个按钮,这个按钮就会生成一个新的Excel文件,他们可以拖回到需要的地方。

I know nothing about Visual Basic other than that it exists but I have an inkling it might be useful to this problem. Is it possible to run Perl scripts within a Visual Basic script? Is it reasonable to think that someone who is afraid of the command line and computer science in general will be able to comfortably run Visual Basic scripts?

我对Visual Basic一无所知,只知道它的存在,但我觉得它可能对这个问题有用。是否可以在Visual Basic脚本中运行Perl脚本?认为害怕命令行和计算机科学的人能够轻松地运行Visual Basic脚本是合理的吗?

NOTE: I use a Mac OS as do most of my coworkers. A Windows-specific solution would be good because we have a few Windows computers dedicated to tasks that, for whatever reason, are easier done on a Windows OS.

注意:我和我的大多数同事都使用Mac OS。一个特定于Windows的解决方案会很好,因为我们有一些专门用于任务的Windows电脑,不管出于什么原因,在Windows操作系统上更容易完成。

2 个解决方案

#1


1  

This sounds like a perfect application for an OSX Folder Action. Basically, you start the Automator and tell it you want to create a Folder Action and it gives you a split screen where the things you can do are on the left and you develop your script on the right.

这听起来像是OSX文件夹操作的完美应用。基本上,你启动自动程序并告诉它你想要创建一个文件夹动作它会给你一个分割屏幕你可以做的事情在左边你可以在右边开发你的脚本。

Drag the action Run shell script from the left side to the right and select which folder it is to apply to at the top. Then change it from running bash to using the Perl interpreter in /usr/bin/perl and paste your script in.

将操作Run shell脚本从左边拖到右边,并在顶部选择要应用的文件夹。然后将其从运行bash更改为在/usr/bin/perl中使用Perl解释器,并将脚本粘贴进来。

Then all you need to do is drop a file on the folder and it will run the Perl script on it.

然后,您需要做的就是将一个文件放到文件夹中,它将在文件夹上运行Perl脚本。

You could share that folder with your Windows users using Samba in System Preferences-> Sharing so they benefit from the power of OS X.

您可以在系统首选项中使用Samba与Windows用户共享该文件夹——>共享,以便他们受益于OS X的强大功能。

#2


0  

You can do this in VBA with the Shell command. This is an example in a Windows environment. I'm not sure how well the commands work in a OSX environment.

您可以使用Shell命令在VBA中执行此操作。这是Windows环境中的一个例子。我不确定这些命令在OSX环境中的工作效果如何。

Thanks to Onorio Catenacci for some code stolen outright.

感谢Onorio Catenacci发来的一些直接窃取的代码。

Public Const CmdPath = "C:\Windows\system32\cmd.exe"
Public Const PerlExecPath = "C:\path\to\perl\bin\perl.exe"
Public Const ScriptPath = "C:\Temp\script.pl"
Public Const OutputDir = "C:\Temp"

Function FileExists(ByVal FileToTest As String) As Boolean
   FileExists = (Dir(FileToTest) <> "")
End Function

Function GetWorkbookIfLoaded(ByVal FileName As String) As Workbook
   Dim lWbName As String
   lWbName = Dir(FileName)
   If lWbName = "" Then
       lWbName = FileName
   End If
   On Error Resume Next
   Set GetWorkbookIfLoaded = Workbooks(lWbName)
   On Error GoTo 0
End Function

Function DeleteFile(ByVal FileToDelete As String)
   Dim lUserOK As Boolean
   Dim lReturn As Boolean
   Dim lWbName As String
   Dim oBk As Workbook

   lUserOK = True
   lWbName = Dir(FileToDelete)

   lReturn = (lWbName = "")

   If Not lReturn Then
      Set oBk = GetWorkbookIfLoaded(lWbName)

      If Not oBk Is Nothing Then
         oBk.Close
         Set oBk = GetWorkbookIfLoaded(lWbName)
         lUserOK = (oBk Is Nothing)
      End If

      If lUserOK Then
          SetAttr FileToDelete, vbNormal
          Kill FileToDelete
          lReturn = True
      End If

   End If
   DeleteFile = lReturn

End Function

Sub CreateNewExcel()
    Dim lProceed    As Boolean
    Dim lRetVal     As Double
    Dim lMsgResult  As VbMsgBoxResult
    Dim lOutputPath As String
    Dim lCommand    As String

    lOutputPath = OutputDir & "\output.tab"
    lCommand _
        = CmdPath & " /C " & PerlExecPath & " -e " _
        & Chr(34) & "print qq[${\(scalar localtime)}\t1\t2\t3\t4\n]" _
        & Chr$(34) & " > " & lOutputPath

    lProceed = DeleteFile(lOutputPath)
    If Not lProceed Then Exit Sub

    DoEvents

    lRetVal = Shell(lCommand, vbHide)
    Application.Wait (Now + TimeValue("0:00:01"))

    If lRetVal = 0 Or Not FileExists(lOutputPath) Then
        MsgBox ("Failed to run script")
    Else
        Workbooks.Open (lOutputPath)
    End If

End Sub

I had to throw in an Application.Wait call because the the test for the file existence would sometimes catch the not-yet-deleted file, or not catch the not-fully-created file.

我不得不提交一份申请。等待调用,因为对文件存在的测试有时会捕获尚未删除的文件,或者不会捕获未完全创建的文件。

I did find some stuff on executing stuff in an Apple environment elsewhere on *.

我确实在*的其他地方找到了一些在苹果环境中执行的东西。

#1


1  

This sounds like a perfect application for an OSX Folder Action. Basically, you start the Automator and tell it you want to create a Folder Action and it gives you a split screen where the things you can do are on the left and you develop your script on the right.

这听起来像是OSX文件夹操作的完美应用。基本上,你启动自动程序并告诉它你想要创建一个文件夹动作它会给你一个分割屏幕你可以做的事情在左边你可以在右边开发你的脚本。

Drag the action Run shell script from the left side to the right and select which folder it is to apply to at the top. Then change it from running bash to using the Perl interpreter in /usr/bin/perl and paste your script in.

将操作Run shell脚本从左边拖到右边,并在顶部选择要应用的文件夹。然后将其从运行bash更改为在/usr/bin/perl中使用Perl解释器,并将脚本粘贴进来。

Then all you need to do is drop a file on the folder and it will run the Perl script on it.

然后,您需要做的就是将一个文件放到文件夹中,它将在文件夹上运行Perl脚本。

You could share that folder with your Windows users using Samba in System Preferences-> Sharing so they benefit from the power of OS X.

您可以在系统首选项中使用Samba与Windows用户共享该文件夹——>共享,以便他们受益于OS X的强大功能。

#2


0  

You can do this in VBA with the Shell command. This is an example in a Windows environment. I'm not sure how well the commands work in a OSX environment.

您可以使用Shell命令在VBA中执行此操作。这是Windows环境中的一个例子。我不确定这些命令在OSX环境中的工作效果如何。

Thanks to Onorio Catenacci for some code stolen outright.

感谢Onorio Catenacci发来的一些直接窃取的代码。

Public Const CmdPath = "C:\Windows\system32\cmd.exe"
Public Const PerlExecPath = "C:\path\to\perl\bin\perl.exe"
Public Const ScriptPath = "C:\Temp\script.pl"
Public Const OutputDir = "C:\Temp"

Function FileExists(ByVal FileToTest As String) As Boolean
   FileExists = (Dir(FileToTest) <> "")
End Function

Function GetWorkbookIfLoaded(ByVal FileName As String) As Workbook
   Dim lWbName As String
   lWbName = Dir(FileName)
   If lWbName = "" Then
       lWbName = FileName
   End If
   On Error Resume Next
   Set GetWorkbookIfLoaded = Workbooks(lWbName)
   On Error GoTo 0
End Function

Function DeleteFile(ByVal FileToDelete As String)
   Dim lUserOK As Boolean
   Dim lReturn As Boolean
   Dim lWbName As String
   Dim oBk As Workbook

   lUserOK = True
   lWbName = Dir(FileToDelete)

   lReturn = (lWbName = "")

   If Not lReturn Then
      Set oBk = GetWorkbookIfLoaded(lWbName)

      If Not oBk Is Nothing Then
         oBk.Close
         Set oBk = GetWorkbookIfLoaded(lWbName)
         lUserOK = (oBk Is Nothing)
      End If

      If lUserOK Then
          SetAttr FileToDelete, vbNormal
          Kill FileToDelete
          lReturn = True
      End If

   End If
   DeleteFile = lReturn

End Function

Sub CreateNewExcel()
    Dim lProceed    As Boolean
    Dim lRetVal     As Double
    Dim lMsgResult  As VbMsgBoxResult
    Dim lOutputPath As String
    Dim lCommand    As String

    lOutputPath = OutputDir & "\output.tab"
    lCommand _
        = CmdPath & " /C " & PerlExecPath & " -e " _
        & Chr(34) & "print qq[${\(scalar localtime)}\t1\t2\t3\t4\n]" _
        & Chr$(34) & " > " & lOutputPath

    lProceed = DeleteFile(lOutputPath)
    If Not lProceed Then Exit Sub

    DoEvents

    lRetVal = Shell(lCommand, vbHide)
    Application.Wait (Now + TimeValue("0:00:01"))

    If lRetVal = 0 Or Not FileExists(lOutputPath) Then
        MsgBox ("Failed to run script")
    Else
        Workbooks.Open (lOutputPath)
    End If

End Sub

I had to throw in an Application.Wait call because the the test for the file existence would sometimes catch the not-yet-deleted file, or not catch the not-fully-created file.

我不得不提交一份申请。等待调用,因为对文件存在的测试有时会捕获尚未删除的文件,或者不会捕获未完全创建的文件。

I did find some stuff on executing stuff in an Apple environment elsewhere on *.

我确实在*的其他地方找到了一些在苹果环境中执行的东西。