Porting Visual Basic staements to Perl Win32::OLE ones - perl

I read all the standard Win32::OLE texts such as Jan Dubois' classic and of course the official package doc, Microsoft's terse article and many others.
What I miss are rules to convert a captured VBA Excel macro statement into a Perl Win32::OLE one.
Say I have a captured VBA macro:
Sub cntrl_arrow_down()
Range(Selection, Selection.End(xlDown)).Select
End Sub
Assuming I already obtained an application (Excel) object, a "book" object, a "sheet" object and activated the sheet. Further, I selected a cell and I am looking to find the extent down of the column in which the cell is. I would like to get a 'Range' object that I can next read the range into a 2D Perl array.
Please, I need the rules, not just the answer to this particular line.
Thanks!

OK, I still don't know the answer to my original question (and that a good answer to it is still going to be useful to many), but I found an alternative set of tools, see jimtut comments and my response above.

Copying my original comment here into a full Answer, since it seemed to help the op (and maybe it will be Accepted as the Answer):
If you're using Perl, why do you have to use Win32::OLE, and not one of the Excel modules? Of course, if you're really after how to run VBA macros, that's one thing. But if you just need to interface with Excel file, there are modules for doing that in Perl (without OLE).

Related

MatLab - dicm2nii - Custom output filenames (.json, .gz)

converting dicm to nii works great with the help of this script from xiangruili :
https://github.com/xiangruili/dicm2nii/blob/master/dicm2nii.m
BUT I need to modify the output filenames and add a string to it. The function save_json of the script (dicm2nii.m) was promising, but I am new to matlab and have the feeling that there is a simple solution to this problem.
Cant somebody help me, please!
Thanks!
As #Wolfie correctly mentioned, this is not something easily addressed for people unfamiliar with the particular program. But I took a very quick look since I currently use other tools for DICOM to NiFTI conversion and was curious about this one. Here are some general comments that hopefully will help.
The "json" file is just for metadata, and you probably care more about the .nii image file (or both). It looks like nii_tool('save', nii, filename, force_3D) handles the latter.
The nii_tool and save_json calls are just passed a variable containing the output filename, which you could create/modify using any of the standard MATLAB methods (e.g., sprintf or strcat). There are already some examples of this within the code in the calls to nii_tool('save', ...).
Since you say that you are new to MATLAB, it is probably easiest for you (and likely everyone) to write a script to rename the files after exportation. That way, you don't have to worry about catching all of the cases/ instances within the 3000 lines of code that someone else wrote and just fix it with a simple program at the other end. Becomes a much more tractable problem that way.
As an aside, I currently use dcm2niix ( available from GitHub or NITRC) for this conversion outside of MATLAB.

Check if word is part of contextfree language

Good evening to all!
As a fan of regular expressions in Perl, I came up with a question, which I could not answer by googling and searching for myself.
So let me give you a minimal example of my problem:
I have two text-files:
FileA.txt:
aaabbb
FileB.txt:
abbb
I want to check each file-content if it is a word, generated by a specific contextfree language. For example in this case:
L={a^nb^n | n > 0}.
Now I have the problem, that the regex from Perl won't work, since it isn't a regular language.
For sure I could script a little PDA and check if it terminates.
But is there another way in Perl to solve this problem? Maybe a way to pass a context-free-grammar or sth.?

How to find literals in source code of Smartforms and in SAPScripts (or reports, if the others can't be done)

I'd like to check hardcoded values in (a lot of) Smartforms and SAPScript forms.
I have found a way to read the source code of both of these, but it seems that i will have to go through a lot of parsing before I get anything reliable.
I've come across function module GET_LITERAL but that doesn't seem to help me much since i have to specify the offset of the value, if i got right what the function is doing in the first place.
I also found RS_LITERAL_LIST but that also doesn't do what i expect.
I also tried searching for reports and methods, but haven't found anything that seemed to help.
A backup plan would be to get some good parsing tool, so do you know of anything like that.
Anyway, any hints would be helpful and appreciated.
[EDIT]
Forgot to mention, the version of my system is 4.6C
If you have a fairly recent version of ABAP, you can use a regex.
Follow the pattern of this example, but use your source as the text and create your own regex. Have it look for any single quotes on the end of a word separated by spaces or any integers with spaces on either side. That's just a start, you might need to work on a better pattern.
String functions count, find, and match

Why are Perl source filters bad and when is it OK to use them?

It is "common knowledge" that source filters are bad and should not be used in production code.
When answering a a similar, but more specific question I couldn't find any good references that explain clearly why filters are bad and when they can be safely used. I think now is time to create one.
Why are source filters bad?
When is it OK to use a source filter?
Why source filters are bad:
Nothing but perl can parse Perl. (Source filters are fragile.)
When a source filter breaks pretty much anything can happen. (They can introduce subtle and very hard to find bugs.)
Source filters can break tools that work with source code. (PPI, refactoring, static analysis, etc.)
Source filters are mutually exclusive. (You can't use more than one at a time -- unless you're psychotic).
When they're okay:
You're experimenting.
You're writing throw-away code.
Your name is Damian and you must be allowed to program in latin.
You're programming in Perl 6.
Only perl can parse Perl (see this example):
#result = (dothis $foo, $bar);
# Which of the following is it equivalent to?
#result = (dothis($foo), $bar);
#result = dothis($foo, $bar);
This kind of ambiguity makes it very hard to write source filters that always succeed and do the right thing. When things go wrong, debugging is awkward.
After crashing and burning a few times, I have developed the superstitious approach of never trying to write another source filter.
I do occasionally use Smart::Comments for debugging, though. When I do, I load the module on the command line:
$ perl -MSmart::Comments test.pl
so as to avoid any chance that it might remain enabled in production code.
See also: Perl Cannot Be Parsed: A Formal Proof
I don't like source filters because you can't tell what code is going to do just by reading it. Additionally, things that look like they aren't executable, such as comments, might magically be executable with the filter. You (or more likely your coworkers) could delete what you think isn't important and break things.
Having said that, if you are implementing your own little language that you want to turn into Perl, source filters might be the right tool. However, just don't call it Perl. :)
It's worth mentioning that Devel::Declare keywords (and starting with Perl 5.11.2, pluggable keywords) aren't source filters, and don't run afoul of the "only perl can parse Perl" problem. This is because they're run by the perl parser itself, they take what they need from the input, and then they return control to the very same parser.
For example, when you declare a method in MooseX::Declare like this:
method frob ($bubble, $bobble does coerce) {
... # complicated code
}
The word "method" invokes the method keyword parser, which uses its own grammar to get the method name and parse the method signature (which isn't Perl, but it doesn't need to be -- it just needs to be well-defined). Then it leaves perl to parse the method body as the body of a sub. Anything anywhere in your code that isn't between the word "method" and the end of a method signature doesn't get seen by the method parser at all, so it can't break your code, no matter how tricky you get.
The problem I see is the same problem you encounter with any C/C++ macro more complex than defining a constant: It degrades your ability to understand what the code is doing by looking at it, because you're not looking at the code that actually executes.
In theory, a source filter is no more dangerous than any other module, since you could easily write a module that redefines builtins or other constructs in "unexpected" ways. In practice however, it is quite hard to write a source filter in a way where you can prove that its not going to make a mistake. I tried my hand at writing a source filter that implements the perl6 feed operators in perl5 (Perl6::Feeds on cpan). You can take a look at the regular expressions to see the acrobatics required to simply figure out the boundaries of expression scope. While the filter works, and provides a test bed to experiment with feeds, I wouldn't consider using it in a production environment without many many more hours of testing.
Filter::Simple certainly comes in handy by dealing with 'the gory details of parsing quoted constructs', so I would be wary of any source filter that doesn't start there.
In all, it really depends on the filter you are using, and how broad a scope it tries to match against. If it is something simple like a c macro, then its "probably" ok, but if its something complicated then its a judgement call. I personally can't wait to play around with perl6's macro system. Finally lisp wont have anything on perl :-)
There is a nice example here that shows in what trouble you can get with source filters.
http://shadow.cat/blog/matt-s-trout/show-us-the-whole-code/
They used a module called Switch, which is based on source filters. And because of that, they were unable to find the source of an error message for days.

Is there autoexpect for Perl's Expect?

I would like to generate Perl Expect code automatically, does something like autoexpect exist for Perl's Expect??
This is not a good answer, but will have to do until a good answer comes along.
I ran the TCL autoexpect and it created a script file, I then wrote couple lines of perl code that parses the lines with "send" and "expect" tags and then uses the perl expect module to run them along with some other actions.
This hybrid approach gets me by, but I am still hoping for a better answer to come.