23
Tue, Apr
0 New Articles

TechTip: Printing from a CL Program (the PRTLN Command)

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Generating a report from an IBM i CL program isn’t straightforward, because in CL there is no direct way to write to a spool file. This article provides the Print Line (PRTLN) command, which allows simple, direct printing from a CL or CLLE program, including page control and headings.

Sometimes it's just convenient to print directly from CL, and that's where the PRTLN command is handy. The CL language wasn’t designed to create reports—report writing is a strength of RPG. You can, of course, create Query/400 or QMQRY objects and then call them from a CL program to produce a report, but then you have extra objects to manage.

If you Google "ibm i printing from cl" you will find suggested techniques such as:

  • Using QSH and rfile
  • Writing to a temporary file with SQL followed by a CPYF to a printer
  • Using the C language printf function

Not everyone is comfortable with QSH and/or C language functions, and a native IBM i command that you can prompt, like PRTLN, is more desirable.

The PRTLN Command

The PRTLN command allows you to:

  • Print a line to a spool file with single, double, or triple spacing, or overprinting.
  • Define zero to nine heading lines that print on overflow.
  • Print a page number in a heading line.
  • Force a new page if you want break-handling.

Fully prompted, the PRTLN command looks like this:

TechTip: Printing from a CL Program (the PRTLN Command)  - Figure 1 

Figure 1: Fully prompted PRTLN command

Line text

Specifies the text to print on this line, or if defining a header line, specifies the text of that header line.

Line spacing

Specifies the spacing of the printed line or the header line. (S1: Space 1 line and print; S2: Space 2 lines and print; S3: Space 3 lines and print; S0: Overprint previous line.)

Defining heading line?

Y means this is a header line definition.

Header line number

Specifies which heading line is being defined, 1 through 9.

  • Heading lines can be defined in any sequence. For example, you can define heading 9, then define heading 1, then define heading 3, etc.
  • Heading lines always print in ordinal order, not the order in which they were defined.
  • A heading line that is not defined does not print.
  • A heading line may be defined with blank text and will print a blank heading line.
  • Once defined, a heading line cannot be undefined.
  • A heading line may be redefined at any time and will take effect at the next page break. Normally, you would force a page break after redefining a heading line.

Include page number

Y to print a page number in the rightmost 8 positions of this header line.

Non-printing control functions

These are operations that are not related to printing a line or defining headings.

  • *NEWPAGE forces a new page when the next line prints. (Page overflow and header printing is handled automatically, so you need to use this only if you have break-handling logic in your program or if you want to print totals or a message at the end of the report.)  
  • *CLOSE closes the print file. Normally, you do this at the end of the report, but you can use it to create a new spool file.

Formatting the Print Line

In a previous article, I showed how easily RPG can format print line columns using a data structure. CL doesn't have data structures, but they can be simulated using the STG(*DEFINED) and DEFVAR parameters of the DCL command. This is much simpler than building the print line through concatenation.

As an example, you can define a print line with three columns like this:

    DCL (&LINE)  (*CHAR) LEN(132)

    DCL (&LIB)   (*CHAR) STG(*DEFINED) LEN(10) DEFVAR(&LINE)

    DCL (&FILE)  (*CHAR) STG(*DEFINED) LEN(10) DEFVAR(&LINE 12)

    DCL (&MBR)   (*CHAR) STG(*DEFINED) LEN(10) DEFVAR(&LINE 33)

Then you just need to populate the line using the column names.

PRTLN Example

Here's a simple program to demonstrate the concepts. It prints a really simple report.

PGM

DCL   (&LC)    (*DEC) LEN(5 0) VALUE(1)

DCL   (&UNDER) (*CHAR) LEN(20) VALUE('____________________')

/* Define print line and columns */

DCL   (&LINE)  (*CHAR)  LEN(132)

DCL   (&COUNT) (*CHAR) STG(*DEFINED) LEN(5) DEFVAR(&LINE 5)

DCL   (&STAMP) (*CHAR) STG(*DEFINED) LEN(20) DEFVAR(&LINE 20)

/* Define heading 1 */

PRTLN  LINE('Really Simple Report') HEADING(Y) HEAD(1 Y)

/* Define heading 2 */

CHGVAR &COUNT 'COUNT'

CHGVAR &STAMP 'TIMESTAMP'

PRTLN  LINE(&LINE) HEADING(Y) HEAD(2)

/* Define heading 3, underscoring heading 2 */

CHGVAR &COUNT &UNDER

CHGVAR &STAMP &UNDER

PRTLN  LINE(&LINE) SPACE(S0) HEADING(Y) HEAD(3)

/* Print a report showing count and timestamp */

DOWHILE COND(&LC *LT 70)

    CHGVAR &COUNT %CHAR(&LC)

    RTVSYSVAL SYSVAL(QDATETIME) RTNVAR(&STAMP)

     PRTLN   LINE(&LINE)

    CHGVAR   VAR(&LC) VALUE(&LC + 1)

ENDDO

PRTLN CONTROL(*CLOSE)

ENDPGM

It produces a two-page report like this:

TechTip: Printing from a CL Program (the PRTLN Command)  - Figure 2 

TechTip: Printing from a CL Program (the PRTLN Command)  - Figure 3

Figure 2: The "Really Simple" report

The Code

The code behind the PRTLN command can be downloaded from here, or it can be inspected or downloaded from my GITHUB repository.

Following is an overview of the source members.

PRTLN.CMD

This is the source for the PRTLN command.

PRTLNCV.CLLE

This is the Validity Checking Program (VCP) for the PRTLN command. A VCP is optional and can be used to do parameter validity checking that is difficult or impossible in standard command definition source. When used, it receives the same parameters as the CCP and can pass back error messages to the command. It is used here to ensure that nonprinting CONTOL functions don't also try to print a line or define a heading.

PRTLNC.CLLE

This is the Command Processing Program (CPP). A CPP is called when there are no errors in the command. Here, it reformats the parameter from the command to pass to the PRT program.

PRT.RPGLE

This is an RPG/FREE program that does the heavy lifting. It saves heading lines in an array, takes care of opening and closing the print file, and prints headings on overflow.

It writes to the MYPRINT printer file, which is defined like this:

    CRTPRTF FILE(LENNONS1/MYPRT) DEVTYPE(*SCS)

            PAGESIZE(66 133) LPI(6) CPI(10)

             OVRFLW(60) CTLCHAR(*FCFC) CHLVAL((1 (6)))

             FONT(*CPI)

Adjust the overflow or top of form line to suit your needs.

PRT can also be called directly bypassing the PRTLN command, for example, from an RPG program. Full details of its two parameters and a sample RPG program can be found in my GITHUB repository.

PRTLNP.PNLGRP

This is the UIM help text for the PRTLN command. I created a skeleton using the IBM GENCMDDOC command and then edited it with the Code for IBM i extension to VS Code.

Conclusion

For something complicated, like payroll checks or month-end general ledger, don't do it from CL; use RPG. But for a simple report, the PRTLN command can be used to create it directly from a CL program.

 

Sam Lennon

Sam Lennon is an analyst, developer, consultant and IBM i geek. He started his programming career in 360 assembly language on IBM mainframes, but moved to the AS400 platform in 1991 and has been an AS400/iSeries/i5/IBM i advocate ever since.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: