Kind Monks,
I'm trying to pass a string variable to a subroutine in my library for some gentle reformatting because the nasty characters my customers insist on typing into their descriptions occasionally makes the HTML page break.
For example, I want to exchange a tick mark (') with (& rsquo ;) (minus those parentheses of course and no spaces in the rsquo piece). Below is the main program, and below that is the library containing the subroutine. I've snipped out anything I thought irrelevant.
My print statements show the variable $attr_descr going in, getting some loving massage... but when I view the source on the rendered page, the magical substitutions have disappeared except within the subroutine itself.
I might have gone along with the idea that the "local" would confine my results to the sub itself, but I've used a very similar construct to format some numbers based on customer preferences (see the sub "commify"). That returns the $_ variable nicely but the "cleanup" does not return my variable.
Here is the top part of the View Source results:
attr_descr before Nonwovens testing a tick ' and an
at @ and a less than < and a double "<br><br>
attr_descr entering cleanup <br><br>
attr_descr exiting cleanup Nonwovens testing a tick ’
and an at @ and a less than < and a double "<br><br>
attr_descr after Nonwovens testing a tick ' and an
at @ and a less than < and a double "
I believe I am having a dumb blonde moment... anyone care to help me see the (most likely obvious) thing that I'm missing?
Thanks!
#!/usr/local/bin/perl5_8
# Cash Balance program; processes data from main menu program.
use strict;
use ncw_com_library; # contains common subs (commify, timeout, etc.)
use HTML::Template;
use Time::Local;
use DBI;
use CGI ':standard';
my $CGI = CGI->new;
# Clear buffers and set up web page (required)
$|=1;
# [some attributes code snipped out that I believe is irrelevant]
my ($attr_descr, $attr_exists, $sth_attr);
$dbh=DBI->connect("dbi:Oracle:".$databs,$userid,$passwd) ||
die "conn attr_sql";
$sth_attr = $dbh->prepare($attr_sel) ||
die "prep attr_sql";
$sth_attr ->execute || die "exec attr_sql";
while ( $attr_exists = $sth_attr->fetch )
{ $project = $attr_exists->[0];
$attr_descr = $attr_exists->[1];
print "attr_descr before $attr_descr<br>";
# Put in HTML-friendly characters for any odd characters
&ncw_com_library::cleanup($attr_descr);
print "attr_descr after $attr_descr<br>";
# [some more fetched fields snipped out]
}
# [snipped out other financial data loops for HTML:TEMPLATE;
# no issues there]
#################### Begin Section ##############################
# Pass parameters from @loop arrays to template; print report
# [snipped out irrelevant params below]
$template->param(
passdata => \@loop_data,
attr_descr => $attr_descr,
project => $project
);
print $template->output();
#++++++++++++++++++++ End Section ++++++++++++++++++++++++++++++
******************************************************************
# This is my library
******************************************************************
package ncw_com_library;
# Contains various common subroutines used by the WRS Reports
1;
use strict;
use DBI;
use Exporter ();
our @ISA = 'Exporter';
our @EXPORT;
use vars @EXPORT=qw/ $asofdt $auth_fail $message $projects
$proj_descr $rpt_dates $rpt_id $rpt_unavail $status /;
# [snipped out unrelated subroutines]
sub cleanup
# Replaces various characters with HTML-friendly characters
{
print "attr_descr entering cleanup $_<br>";
local $_ = shift @_;
1 while $_ =~ s/^(.*)(')(.*)/$1’$3/gm;
print "attr_descr exiting cleanup $_<br>";
return $_;
}
# This sub works with the financial data so I used it as the basis for
# the cleanup sub above.
sub commify
# Formats numbers to two decimal places, put in commas, make negs red)
{
local $_ = sprintf "%.2f", shift @_;
1 while $_ =~ s/^(-?\d+)(\d\d\d)/$1,$2/;
$_ =~ s/^(-)(.*)/\($2\)/;
{ if ( $_ =~ m/^\(.*/ )
{ $_ = "style=\"color:#B22222;\">" . $_ }
else { $_ = "style=\"color:black;\">" . $_ }
}
return $_;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.