Text Practice Mode
PERL and HTML coding (Part 1).
created Sep 12th 2014, 01:36 by Nehemiah Thomas
9
302 words
1 completed
0
Rating visible after 3 or more votes
00:00
#
# This translates a file, such a program or other text file, so that
# it can be displayed literally in HTML. It brackets the code in
# the <pre> and </pre> tags, expands tabs, and translates the characters
# which HTML treats as special so they will be displayed literally.
#
use strict;
#
# Expand tabs at 8 stops per tab.
#
sub expand {
my ($line) = @_;
my ($left, $right); # Parens needed so my applies to both.
while($line =~ /\t/) {
($left, $right) = split (/\t/, $line, 2);
my($tabamt) = 8 - length($left) % 8;
$line = $left . (" " x $tabamt) . $right;
}
return $line;
}
print "<pre>\n";
# Copy with changes.
while(my $line = <STDIN>) {
chomp $line;
$line = expand($line);
$line =~ s/&/&/g;
$line =~ s/</</g;
$line =~ s/>/>/g;
$line =~ s/"/"/g;
print "$line\n";
}
print "</pre>\n"; #
# One more approach is to use the array/hash conversion rules to
# build keyword parameters, with defaults.
#
use strict;
# Print a string one or more times under all sorts of controls.
sub barko {
# Check for correct pairing.
@_ % 2 == or
die "barko: Odd number of arguments.\n";
# Store the parms, with defaults.
my %parms = ( 'string' => 'snake', # String to print
'between' => '', # Place between chars.
'repeat' => 1, # Repeat this many times.
'cascade' => 0, # Move each line right this much more.
'blankafter' => 1, # Extra blank line afterwards.
@_);
# Now %parms is a list of keyword => value pairs as sent, using
# defaults for keys not sent.
# Add the between to the string.
my $str = substr($parms{'string'}, 1);
$str =~ s/(.)/$parms{'between'}$1/g;
$str = substr($parms{'string'}, 0, 1) . $str;
# Printin' time!
my $preamt = 0;
for(my $n = $parms{'repeat'}; $n--; ) {
print ((' ' x $preamt), "$str\n");
$preamt += $parms{'cascade'};
}
print "\n" if $parms{'blankafter'};
}
# Call with various options. These can be sent in any order.
barko;
barko(repeat => 3, string => 'BOZON', cascade => 1);
barko(between => ' ');
barko(between => '<->', repeat => 5);
barko(string => '** done **', blankafter => 0);
# This translates a file, such a program or other text file, so that
# it can be displayed literally in HTML. It brackets the code in
# the <pre> and </pre> tags, expands tabs, and translates the characters
# which HTML treats as special so they will be displayed literally.
#
use strict;
#
# Expand tabs at 8 stops per tab.
#
sub expand {
my ($line) = @_;
my ($left, $right); # Parens needed so my applies to both.
while($line =~ /\t/) {
($left, $right) = split (/\t/, $line, 2);
my($tabamt) = 8 - length($left) % 8;
$line = $left . (" " x $tabamt) . $right;
}
return $line;
}
print "<pre>\n";
# Copy with changes.
while(my $line = <STDIN>) {
chomp $line;
$line = expand($line);
$line =~ s/&/&/g;
$line =~ s/</</g;
$line =~ s/>/>/g;
$line =~ s/"/"/g;
print "$line\n";
}
print "</pre>\n"; #
# One more approach is to use the array/hash conversion rules to
# build keyword parameters, with defaults.
#
use strict;
# Print a string one or more times under all sorts of controls.
sub barko {
# Check for correct pairing.
@_ % 2 == or
die "barko: Odd number of arguments.\n";
# Store the parms, with defaults.
my %parms = ( 'string' => 'snake', # String to print
'between' => '', # Place between chars.
'repeat' => 1, # Repeat this many times.
'cascade' => 0, # Move each line right this much more.
'blankafter' => 1, # Extra blank line afterwards.
@_);
# Now %parms is a list of keyword => value pairs as sent, using
# defaults for keys not sent.
# Add the between to the string.
my $str = substr($parms{'string'}, 1);
$str =~ s/(.)/$parms{'between'}$1/g;
$str = substr($parms{'string'}, 0, 1) . $str;
# Printin' time!
my $preamt = 0;
for(my $n = $parms{'repeat'}; $n--; ) {
print ((' ' x $preamt), "$str\n");
$preamt += $parms{'cascade'};
}
print "\n" if $parms{'blankafter'};
}
# Call with various options. These can be sent in any order.
barko;
barko(repeat => 3, string => 'BOZON', cascade => 1);
barko(between => ' ');
barko(between => '<->', repeat => 5);
barko(string => '** done **', blankafter => 0);
saving score / loading statistics ...