2009-05-24

PHP Modules: Finding Syntax Errors and Module Dependencies

In constructing a web application with more than 2 PHP source modules, I found that it is useful to have a quick and easy way of finding a syntax error that may exist in one (or more) of a list of included modules.

Getting a List of PHP Includes

There is a function in PHP for this, which we will get back to in a moment. For right now I wanted to address some things we can do at a Bash prompt to troubleshoot why a script may not be sending any output to the browser on STDOUT.

That's a very common situation when you're working on a large application with many modules. It becomes important in those situations to be meticulous about syntax checking each change in any code before making additional changes.

I've been using the 'Lint' mechanism provided by PHP to syntax check PHP source code. It works like this: 

$php -l <fileName>

Now, some of you might already be seeing the implications of this. Hows about: 

$for fn in \
      `grep require frmPlayerInformation_body.09.inc\
      | perl -ne '/\"([a-zA-Z0-9.]+)\"\;/ && print "$1\n";'` ;;
 do 
      echo $fn ;;
      php -l $fn ;;
      echo "########" ;;
 done

The implications for scripting this operation from the command line are clear.

get_included_files()

PHP provides a function to get a list of module dependencies for a running script. It's called

get_included_files()

… and here's the URL for the docs: http://us2.php.net/manual/en/function.get-included-files.php

Problems with this Approach

There is at least one serious deficiency inherent in using the require, require_once, include, and include_once directives as the sole indicators of module dependency;  that is that, simply, the modules which are required or "included" due to HTML element attribute values.

Examples include the ACTION attrubute value(s) of HTML FORM element(s), the SRC attribute values for SCRIPT elements. and so on. IFRAME and LINK elements are also among those that can cause a module dependency within the HTML, CSS, or Javascript layer(s) generated by PHP code of a site.

2009-05-14

Standard I/O Redirection in Perl

This is an excerpt from the Perl manuals at perlDoc.perl.org concerning redirection of STDOUT and STDERR in Perl:

Because backticks do not affect standard error, use shell file descriptor syntax (assuming the shell supports this) if you care to address this. To capture a command's STDERR and STDOUT together:

$output = `cmd 2>&1`;

To capture a command's STDOUT but discard its STDERR:

$output = `cmd 2>/dev/null`;

To capture a command's STDERR but discard its STDOUT (ordering is important here):

$output = `cmd 2>&1 1>/dev/null`;

To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out the old STDERR:

$output = `cmd 3>&1 1>&2 2>&3 3>&-`;

To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:

system("program args 1>program.stdout 2>program.stderr");

The STDIN filehandle used by the command is inherited from Perl's STDIN. For example:

open BLAM, "blam" || die "Can't open: $!";
    open STDIN, "<&BLAM";

    print `sort`;

will print the sorted contents of the file "blam".

The I/O Operators manual also has some interesting information about standard file I/O in Perl.

2009-05-13

Detecting active VNC server instances

I needed something to tell me what VNC server (vncserver()) instances were running, and on which DISPLAY ports, and with what resolution(s) (geometry), so I wrote one. Here is the HTMLized Perl source code:

#!/usr/bin/perl -w
######################################################################
## FILE:  findvncport.pl 

## DESC:  display information about VNC server instances
## VERS:  01
## DATE:  2009-05-13
## AUTH:  pdw@thesoftwaremaster.com
##############################################################################
##  USAGE NOTES:
##
##    This is a script to find out the connection port number and screen
##    geometry of any VNC server instances currently running on a host.  

##
##    This functionality is most useful to a user who needs to know what VNC
##    server instance(s) is(are) running, and on what port(s) - E.G.  at logon
##    (to a shell) time when a vncserver has been left running.
##
##    For convenience, the geometry of the X interface, the PID of Xvnc (the
##    actual X server invoked by vncserver), and such other info as we can
##    glean from the process listing on the host.
##

##    Note that this script only returns information about VNC instances
##    running under the UID of the user who runs the script.  The value of
##    the environment variable $USER is used to filter the process listing.
##    The user's $UID value is displayed, but is not used.
##
##    This program is designed to be run on a remote, ssh-accessible host
##    computer during the login and user account initialization process.
##
##  EXAMPLE:

##
##    I place this script in the .bash_login shell script that runs at user
##    login.
##
##
##  _TODO_: there is no buglist, yet, but there are some things need done to
##          this script right to begin with (i.e. the functionality is
##          marginal, and will almost certainly "break" the first time someone
##          tries to use it on some other platform or environment.  First on

##          the list of things to do must be: Fix the regular expression
##          parsing which finds the data in the process listing.  As the code
##          is now, these regex statements will break e.g. if the Xvnc command
##          line doesn't look like it expects. 2nd on the list has to be the
##          addtion of command line parameters to modify the output of the
##          script. :_TODO_
##
#############################################################################
##  REVISION HISTORY:

##
##     01: 2009-05-13 pdw
##         Initial version
##
#############################################################################
##
## Load up some environmental info...
$curruid = `echo \$UID`;

$curruser = `echo \$USER`;
@processes = `ps x -u \$USER`;
chomp($curruid);

chomp($curruser);

## @displays gets the data about whatever VNC displays are running
@displays = ();

while ($_ = shift(@processes)) {

 my $d = {};
 my $found = 0;
 chomp;

 next unless ($_);
  
 next unless (/^\s*(\d+).+Xvnc\s*(\:\d+)/)

  && ( $display = $2, $vncpid = $1 );

 # get desktop 

 m/-desktop(.+)-httpd/ 
  && ($desktop = $1);

 m/-geometry\s+(\d+x\d+)/
  && ( $geometry = $1 );

 m/-depth\s+(\d+)/
  && ( $depth = $1 );

 $d = {
  'Display'  => $display,
  'VNC PID'  => $vncpid,

  'Desktop'  => $desktop,
  'Geometry' => $geometry,

  'Depth'    => $depth,
 };
 push(@displays, $d);

}

## Display what we found out 
print "VNC server info for USER \'$curruser\':\n";
if (scalar(@displays)) {

 foreach my $disp (@displays) {
  foreach my $k (keys(%$disp))

  {
   print"  $k: \t".$disp->{$k}."\n";

  }
 }
} else {
 print "
  VNC server not found in process listing.

";
}

2009-05-06

ls2csv.pl

#!/usr/bin/perl -w
#
# ls2csv.pl - show a listing of the PHP file
#            (*.inc and *.php) 
#             as a CSV table; no table headings included in 
#             output; no params defined
#
sub create_listing($) 
{
  my $xtn = shift;
  my @files = `ls -ghoG *.$xtn`;
  foreach my $f (@files) {
    chomp($f);
    $f =~ s/^\s*[-rw]+\s+\d\s+(.+)/$1/;
    $f =~ s/ /\",\"/g;
    $f =~ s/^(.)/\"$1/;
    $f =~ s/(.)$/$1\"/;
    print "$f\n";
  }
}

map { create_listing($_) } ("inc","php");