[USflag] The American Programmer [USflag]
Home Programming Books for Computer Professionals Privacy Terms
           Home   > Programming   > Manuals   > Perl    > Useful things

Useful things you can do with Perl

Books on Perl
Perl Manuals / Tutorials

 

1. Execute a system command.

The example shown is a DOS command. Change the command when running on a Unix or other operating system.

The command executes, and its output is displayed on the screen.

# execute a system command.

print `dir c:\perl\my_programs\*.pl`;

This example captures the return code of the command. You can examine it to see if the command worked.

# $x captures return code

$x = system('dir c:\perl\my_programs\*.pl');#(example for windows)

$x = system('ls ');#(example for unix)

print "return code is $x \n";

If you want to capture the output of the command in a file and then possibly read the file in your program, you can use the > operator in the command. The file you specify will be created if it doesnít exist, and replaced if it does exist.

# > places output of command in a file

$x = system('dir c:\perl\my_programs\*.pl > c:\perl\my_programs\output.txt');

# display the file

$x = system('type c:\perl\my_programs\output.txt');

 

 

2. Dropping leading or trailing spaces.

#trim leading blanks

$text = " My Dog has fleas ";

$text =~ s/^\s+//;

print "dropped leading blanks: $text\n";

#trim trailing blanks

$text = " My Dog has fleas ";

$text =~ s/\s+$//;

print "dropped trailing blanks: $text xxx \n";

 

3. Dropping all spaces from a string.

# drop all spaces

$text = "My Dog has fleas";

$text =~ s/\s+//g;#this worked

print "drop all spaces: $text\n";

4. Replacing a text string by another.

# replace text string

$text = "My Dog has fleas";

$text =~ s/Dog/cat/;

print "replaced string: $text\n";

 

4. Translating a text string. All occurrences of one character are replaced by another.

# translate text string

$text = "My Dog has fleas";

$text =~ tr/o/u/; #dog becomes dug

print "replaced string: $text\n";

# translate more than one character

$text = "My Dog has fleas";

$text =~ tr/oea/uie/; #o becomes u, e -> i, a -> e

print "replaced string: $text\n";

 

5. Splitting up a string at specific column positions.

#break at column positions

$text = "My Dog has fleas";

$a = substr($text,0,3);

$b = substr($text,3,4);

$c = substr($text,7,4);

$d = substr($text,11,4);#i know it's short by 1

$e = substr($text,21,4);# no such data

print "break at column positions

\n";print "a $a \n";

print "b $b \n";

print "c $c \n";

print "d $d \n";

print "e $e \n";

 

6. Splitting a string into words. This will treat leading spaces as a word. You can easily correct that. See the second part of the example.

#split into words

# this treats leading spaces as a word

$text = " My Dog has fleas";

# the + means 1 or more spaces

($a, $b, $c, $d, $e, $f) = split(/ +/,$text);

print "splitting at spaces (without dropping leading spaces)\n";

print "a $a \n";

print "b $b \n";

print "c $c \n";

print "d $d \n";

print "e $e \n";

print "f $f \n";

# you may want to drop leading spaces first

$text =~ s/^\s+//;#dropping leading spaces

($a, $b, $c, $d, $e, $f) = split(/ +/,$text);

print "splitting at spaces (after dropping leading spaces)\n";

print "a $a \n";

print "b $b \n";

print "c $c \n";

print "d $d \n";

print "e $e \n";

print "f $f \n";

 

7. Concatenating two strings with intervening spaces.

#concatenate with spaces

$a = "first";

$b = "second";

$v = "$a $b";

print "concatenate with spaces $v \n";

 

8. Concatenating two strings without intervening spaces.

#concatenate without spaces

$a = "first";

$b = "second";

$v = $a . $b;

print "concatenate without spaces $v \n";

Top of Page



















[Books Computer]

Home Books for Computer Professionals Privacy Terms |
Site Map and Site Search Programming Manuals and Tutorials The REXX Files Top of Page |