APM: Running a perl prog from a perl prog?

tmcd at panix.com tmcd at panix.com
Tue Oct 17 21:58:17 PDT 2006


On Tue, 17 Oct 2006, CaptNemo <CaptNemo at Austin.rr.com> wrote:
> 	elsif ( $#ARGV <= 4 ) {
> 		print "\n-----------------------------ERROR------------------------------\n";
> 		print "NOT ENOUGH ARGUMENTS\n";
> 		print "dbquery.pl usage: dbquery.pl DBDRIVER DB DBUSER DBPASSWORD DBTABLE
> \"CONDITION\"\n";
> 		print "NOTE: DBDIVER is usually \"mysql\" but it can be other things like
> \"oracle\".\n";
> 		print "      EXAMPLE: dbquery.pl mysql databas...\n";
> 		print "NOTE: The total CONDITION must be in quotes with the matching data
> in single quotes. \n";
> 		print "      EXAMPLE: \"product LIKE 'bicycle'\"\n";
> 		print "---------------------------END ERROR----------------------------\n\n";

In the "several ways to skin a cat" motif of Perl, I'll mention that I
like "here documents" for this.  I'd express the above as

    elsif ( @ARGV < 6 ) {
        print <<'SOMETAG';

-----------------------------ERROR------------------------------
NOT ENOUGH ARGUMENTS
dbquery.pl usage: dbquery.pl DBDRIVER DB DBUSER DBPASSWORD DBTABLE "CONDITION"
NOTE: DBDIVER is usually "mysql" but it can be other things like "oracle".
      EXAMPLE: dbquery.pl mysql database...
NOTE: The total CONDITION must be in quotes with the matching data in
single quotes.
      EXAMPLE: "product LIKE 'bicycle'"
---------------------------END ERROR----------------------------

SOMETAG
    }

The single quotes in <<'WHATEVER' means that Perl metacharacters ($,
`, @) won't be interpreted.  If it's just <<BLAHBLAH, then they are.

This format makes the text in the program look like the output.
I used Emacs's paragraph wrapping capability to wrap one long line,
which isn't so slick with code.  It's less clutter.

It's not indented at the same level as the code, which is unpleasant,
but that's the defect of its virtue.

You can have just a long string with embedded real newlines, like

        print '

-----------------------------ERROR------------------------------
NOT ENOUGH ARGUMENTS
dbquery.pl usage: dbquery.pl DBDRIVER DB DBUSER DBPASSWORD DBTABLE "CONDITION"
NOTE: DBDIVER is usually "mysql" but it can be other things like "oracle".
      EXAMPLE: dbquery.pl mysql database...
NOTE: The total CONDITION must be in quotes with the matching data in
single quotes.
      EXAMPLE: "product LIKE ' . "'bicycle'" . '"
---------------------------END ERROR----------------------------

';

But note that it has the same problem as the single-line print
statements: you need to worry about protecting the quotation marks
within the text from inadvertant matching.  The fact that I chose
'...' to wrap it led to
      EXAMPLE: "product LIKE ' . "'bicycle'" . '"

When I chose print "..." instead, it has the same "leaning toothpick
disease" of the original, like
      EXAMPLE: \"product LIKE 'bicycle'\"

-- 
Tim McDaniel; Reply-To: tmcd at panix.com


More information about the Austin mailing list