Scala, Java, Unix, MacOS tutorials (page 392)

Here's a sample Perl program that demonstrates how you can include (embed) data inside of your Perl program, right in there next to the source code.

This simple program takes the data after the special __END__ tag, and makes it available to your Perl source code.

#!/usr/bin/perl

while (<main::DATA>)
{
  print $_;
}

__END__
George Washington
Abraham Lincoln
John F. Kennedy

As you can see, you loop through the data with this line of code:

Here’s a reference page (cheat sheet) of Perl printf formatting options. Hopefully this list covers the most common Perl printf printing options you’ll run into, or will at least point you in the right direction.

Formatting strings

Here are several examples that show how to format strings with Perl and printf. I’ll use single quotes in all my printf examples to help demonstrate left- and right-justification.

[toc hidden:1]

Perl FAQ: How can I write a Perl script to read from a named pipe (FIFO file)?

Here’s some code from a Perl program where I open up a named pipe (a FIFO file), then read data from that file until the end of time ... or at least until someone kills this program.

Here's some Perl code that I just pulled from a working program that demonstrates how to read a human-readable configuration file in a Perl program:

In most languages there is an operator named the "ternary" operator that lets you write concise if/then statements. This makes for less verbose, which is generally a good thing. Perl also has a ternary operator, and I'll demonstrate it here.

General syntax of the ternary operator

The general syntax for Perl's ternary operator looks like this:

test-expression ? if-true-expression : if-false-expression

Let's take a look at a brief example to demonstrate this.

Here's a quick example program that demonstrates how to access environment variables from within your Perl programs:

# environment variables are held in the %ENV hash
foreach $key (sort keys %ENV)
{
  print "$key is $ENV{$key}\n";
}

Using this simple Perl foreach loop, here's a subset of the output this script prints on my MacBook Pro:

Summary: This tutorial shows a collection of Perl if, else, and else if examples.

Here are some examples of the Perl if/else syntax, including the “else if” syntax, which is really elsif. (I wrote this because after working with many different languages I can never remember the “else if” syntax for most languages, and elsif is pretty rare.)

The Perl if/else syntax

The Perl if/else syntax is standard, I don’t have any problems here:

In a previous tutorial I demonstrated how you can search a Perl list/array of strings for another string, or a regular expression using Perl's grep function. In this example I'll demonstrate how you can search a list of numbers very easily, also using Perl's grep function.

Perl substring FAQ: How do I extract a substring from a string?

Solution: Use the Perl substr function. The general substr syntax looks like this:

$my_substring = substr($full_string, $start_pos, $length);

Perl substring example (substr)

A more specific Perl substring example is shown in the following code, where I extract the $firstname and $lastname strings from the $name string:

Here's the complete source code for a Perl program I wrote a long time ago that reads a pipe-delimited data file that contains a list of URLs, and prints its output as an HTML list, using UL, LI, and A HREF tags.

Question: What is the command to change my Glassfish admin (administrator) password?

Answer: You can change the Glassfish admin password with this asadmin command:

asadmin change-admin-password

The Glassfish master password

Note that you can also change the Glassfish master password with this command:

asadmin change-master-password

 

Question: What is the command to start a Glassfish server?

Answer: I use the following command to start my Glassfish server:

asadmin start-domain domain1

Although this command refers to starting domain1, at least in my case, it also serves to start the entire Glassfish server. I don't know how this works for multiple domains, but my guess is that if you start any one of your domains, you will start up your Glassfish server.

JSP URI/URL FAQ: How do I get a URI or URL from a JSP (the request URI or request URL)?

I was just working with a JSP, and trying to remember how to get information that can be very helpful inside of a JSP, specifically how to determine the Context, URI, and URL from within a JSP.

To that end, here's the Java source code for a JSP I wrote that will display the Context, URI, and URL for this JSP when it is accessed from a browser:

Here's a copy of a Glassfish cheat sheet (reference page) that I always keep near me whenever I'm working with a Glassfish server. I like a lot of things about Glassfish, but trying to remember all of these commands for the Glassfish CLI is not one of my favorite things.

Here's my Glassfish command reference:

With Perl we work with strings and arrays (or lists) of strings all the time. I thought I'd take a few moments here to show the most common ways to create a Perl string array, and also to loop through (or iterate through) the list of strings I create.

How to create a Perl string array

When you first start to work with Perl, you might create a string array like the following example:

Perl substring FAQ: Can you demonstrate some Perl substring examples?

As a language, Perl is very good at text processing, including dealing with strings and substrings. In this article we'll take a look at some Perl substring manipulations we can perform.

For the purposes of our Perl substring tutorial, let's assume that we have a string defined like this:

Summary: How to sort a Perl string array (when you don't have to worry about whether the strings are all uppercase or lowercase).

Perl excels at a number of tasks, especially text-processing, and if you have an array of strings, and all of the elements in the array are either uppercase or lowercase, sorting your string array is very easy. In this tutorial I'll show how to sort an array of strings, and will also show how to sort your string array in reverse order.

Summary: A Perl for loop (foreach loop) example, showing how to loop over each element in a Perl array.

Many times you'll need to print all the elements of a Perl array, or perhaps the elements of the array that match some filtering criteria. I thought I'd share several examples of how to print the elements of a Perl array here.

Initial setup

In each of the methods shown below, assume that we have an array named @pizzas that has been defined like this:

A Perl FAQ is "How do you concatenate (merge) two or more strings in Perl?"

Use the "." operator

The short answer is that you use the . operator. Here's a simple example:

$name = "alvin" . " " . "alexander";

Of course I could have also done that like this:

$name = "alvin " . "alexander";

but I wanted to show an example with more than two strings.

Perl array length FAQ: "How can I determine the length of a Perl array?"

Solution

There are several different ways to determine the Perl array length, and I show those here.

Solution 1: Perl array length

The first way to determine the Perl array length is by simple assigning a scalar variable to the array, like this: