What is the difference between &GLOBAL-DEFINE and &SCOPED-DEFINE in Progress 4gl? - progress-4gl

I understand that both are preprocessor directives, but I am unable to figure out how they are different. All that is stated in the official Progress documentation is "The syntax of the &GLOBAL-DEFINE and &SCOPED-DEFINE directives are identical but these directives are used differently." which is not very helpful.
I found one source that says that &SCOPED-DEFINE variables remain defined until the compiler finishes the current file, while &GLOBAL-DEFINE variables remain defined until the compiler finishes the current program. However, I don't quite follow what that means. This source also states that &SCOPED-DEFINE variables are propagated down the include stack, but not up it (I assume &GLOBAL-DEFINE is bidirectional?). I have tried to create a simple example but no matter what, I can't figure out the difference.
File 1: test1.p
{test2.i}
File 2: test2.i
&GLOBAL-DEFINE HELLO "Hello world!"
{test3.i}
File 3: test3.i
MESSAGE {&HELLO} VIEW-AS ALERT-BOX.
Whether or not HELLO is &SCOPED-DEFINE or &GLOBAL-DEFINE, it's still defined in test3.i. So what exactly is the difference?

You said you presume that global propagates upwards, which is what I found here when looking for an answer. However, your example only tests downward propagation. Perhaps you need this...
File 1: test1.p
{test2.i}
MESSAGE {&HELLO} VIEW-AS ALERT-BOX.
MESSAGE "{&GOODBYE}" VIEW-AS ALERT-BOX.
File 2: test2.i
&GLOBAL-DEFINE HELLO "Hello world!"
&SCOPED-DEFINE GOODBYE Goodbye-cruel-world
{test3.i}
File 3: test3.i
MESSAGE {&HELLO} VIEW-AS ALERT-BOX.
MESSAGE "{&GOODBYE}" VIEW-AS ALERT-BOX.
I would expect test1.p to show you Hello world! followed by an empty message, whereas test3.p would show you both messages in full. However I've not tried it so I may be wrong.

Related

cobol "hello world" get error when using visual code with terminal

So I'm starting to learn COBOL, tried my first "hello world" program, and got an error that I can't solve.
this is the code:
*hello
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
ENVIRONMENT DIVISION.
PROCEDURE DIVISION.
DISPLAY 'HELLO'.
STOP RUN.
I'm using vs code with extensions that talk with gnucobol(openCobol), did that with the help of this video (using windows). When I'm running the code, I get this message:
hello.cbl:1: error: PROGRAM-ID header missing
I've tried to copy the code from a few other sources that have an example code but still got this message.
I would appreciate any help.
It might help you to see what your code looks like in a purpose-made IDE for COBOL. Here is a direct copy & paste of your code in OpenCobolIDE:
The main issue is the position of the code on lines 2 through 5. COBOL generally will require your code to be very specifically formatted, as you can see from the red lines above, there are limits to where data should exist. This can be changed, but I recommend keeping with it as this may increase your comfort in working with the legacy code that runs the world. Here is an example of how this should be written. I will share both a picture and the code itself so you can see where it is in relation to those red lines.
Code with errors still to show as an example:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
PROCEDURE DIVISION.
*this is a comment
*this is not a comment
MAIN-PROCEDURE.
DISPLAY "Hello world!"
DISPLAY "This display line is too long, creating the same issue but on the other side"
*STOP RUN has an error because the above line was cut off inappropriately
STOP RUN.
END PROGRAM HELLO.
Working code with no errors:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
*this is a comment
MAIN-PROCEDURE.
DISPLAY "Hello world!"
STOP RUN.
END PROGRAM HELLO.
Definitely check out OpenCobolIDE which is a great free IDE for getting started with COBOL and getting the basics down! It helped me to get a better handle on formatting when I was new.

What does if (print) means in Perl?

I got the following code
if (print msgsnd($ipc_msg_to_mppt_id, $msg, 0)) {
what is the purpose of print here? What is returns?
Documentation says it returns true if successful. But how can printing be unsuccessfull?
Printing isn't necessary as simple as dumping the output to a console. It could also be redirected to a file or some other kind of pipe. If it's redirected to a file you don't have write access to, then printing to that file will fail. If it's piped into another program and the latter program terminates, then writing to it will cause a broken pipe error.
As a general principle, I/O operations are outside the control of your program, so you should always assume that they can fail. Reading to or writing from a file, the console, or any kind of socket or pipe, can always, without warning, fail. So if you want your program to do something about it, you'll need to check the return value of functions like print.

How do I (optionally) pipe text into a Turtle shell script?

I am working on a Turtle based shell script. I'd like it to be able to, optionally, accept piped in text in place of an argument. After reading the docs and playing with the stdin Turtle function to no avail, I realize I'm stuck.
Is something like this supported by Turtle? If so, how do I do it?
I think the answer is 'no' it is not supported by Turtle. But it is supported by Haskell. Here's how I did it:
import System.IO
main :: IO ()
main = do
gotPipedInText <- hReady stdin
case gotPipedInText of
True -> do
message <- getContents
putStr message
False -> putStr "nothing piped in\n"
The key insight for me is that I need to check for the existence of the piped in text before trying to read it, as reading an 'empty' handle blocks while waiting for input, but reading a 'full' handle gets the existing content.

Erratic Behavior in console for logging versus print messages

I am finding seemingly random ordering of print versus python logging outputs to the pydev console.
So for example here is my code:
import logging
logging.warning('Watch out!') # will print a message to the console
print "foo"
logging.info('I told you so') # will not print anything
logging.warning("Event:")
logging.warning("told ya")
Note the print line around "foo". What is bizzare is each time I run this the order of output with respect to foo changes! That is foo appears one time at the top another time in the middle another time at the end and so forth.
This is unfortunate in a less toy context when I want to know the sequence in which these output events occured in the code (e.g. when tracing/logging etc). Any suggestions as to what might be going on? Latest pydev, python 2.7.6
Thanks!
p.s. As a side-effect (probably) this is making an eclipse tool out there "grep console" behave oddly). But the problem I describe above is independent of that.

Help me understand this Perl statement with <<'ESQ'

substr($obj_strptime,index($strptime,"sub")+6,0) = <<'ESQ';
shift; # package
....
....
ESQ
What is this ESQ and what is it doing here? Please help me understand these statements.
It marks the end of a here-doc section.
EOF is more traditional than ESQ though.
This construct is known as a here-doc (because you're getting standard input from a document here rather than an external document on the file system somewhere).
It basically reads everything from the next line up to but excluding an end marker line, and uses that as standard input to the program or command that you're running. The end marker line is controlled by the text following the <<.
As an example, in bash (which I'm more familiar with than Perl), the command:
cat <<EOF
hello
goodbye
EOF
will run cat and then send two lines to its standard input (the hello and goodbye lines). Perl also has this feature though the syntax is slightly different (as you would expect, given it's a different language). Still, it's close enough for the explanation to still hold.
Wikipedia has an entry for this which you probably would have found had you known it was called a here-doc, but otherwise it would be rather hard to figure it out.
You can basically use any suitable marker. For example, if one of your input lines was EOF, you couldn't really use that as a marker since the standard input would be terminated prematurely:
cat <<EOF
This section contains the line ...
EOF
but then has more stuff
and this line following is the real ...
EOF
In that case, you could use DONE (or anything else that doesn't appear in the text on its own line).
There are other options such as using quotes around the marker (so the indentation can look better) and the use of single or double quotes to control variable substitution.
If you go to the perlop page and search for <<EOF, it will hopefully all become clear.