NikhilRangaraju has asked for the wisdom of the Perl Monks concerning the following question:

Here is the code that i wrote...
#!/usr/bin/perl use File::Basename; #--------------------------------------------------------------------- +--------- my $Product = 'LegalCraft'; my $Options = ''; my %Win32 = ( 'checkout' => Checkout, 'co' => Checkout, 'clean' => Clean, 'build' => Build ); my %Linux = ( 'checkout' => Checkout, 'co' => Checkout, 'clean' => Clean, 'build' => Build ); my %Platforms = ( 'win32' => \%Win32, 'linux' => \%Linux ); Dispatch(); #--------------------------------------------------------------------- +--------- sub Dispatch { if ( exists( $Platforms{ lc( $ENV{PLATFORM} ) } ) ) { print "\nProduct $Product - Platform $ENV{PLATFORM} supported!\n +"; } else { print "\nProduct $Product - Not implemented ". "for platform $ENV{PLATFORM}\n"; return; } my $FnTable = $Platforms{ lc( $ENV{PLATFORM} ) }; for ( @ARGV ) { if ( exists( $FnTable->{ lc( $_ ) } ) ) { print "\nProduct $Product - Platform $ENV{PLATFORM} - Functio +n $_\n"; $FnTable->{ lc( $_ ) }(); $Options = ''; } else { $Options .= " $_"; } } print "\n"; } sub ChProdDir { $_ = __FILE__; tr/\\/\//; chdir dirname $_; chdir '..'; } BEGIN { ChProdDir(); } sub Checkout { system "./cvs $Options checkout"; chdir '..'; system "cvs co $ENV{IMCOOPT} $Options viewer"; ChProdDir(); }
When i compiled it, "Product Legalcraft is not implemented for Platform " is shown as error and i couldnot understand what is the logic behind Diapatch() function.
  • Comment on Error with my perl script when i generate a script to cvs checkout
  • Download Code

Replies are listed 'Best First'.
Re: Error with my perl script when i generate a script to cvs checkout
by Corion (Patriarch) on Feb 20, 2006 at 12:37 UTC

    You seem to be changing a program without understanding it or understanding what it is supposed to do. This is a problematic and dangerous practice.

    The error message is output by the script itself, in the sub Dispatch, whenever it does not find a supported platform. If you had run your program with warnings enabled, like I suggested when you last posted, Perl would have told you that you used a variable which was not initialized. In this case, it is $ENV{PLATFORM}, most likely, because you did not set the PLATFORM environment variable.

    You could change Dispatch like this to make the program give you a more explicit error message:

    sub Dispatch { if ( exists( $Platforms{ lc( $ENV{PLATFORM} ) } ) ) { print "\nProduct $Product - Platform $ENV{PLATFORM} supported!\n +"; } elsif (defined $ENV{PLATFORM}) { print "\nProduct $Product - Not implemented ". "for platform $ENV{PLATFORM}\n"; return; } else { print "\nThe environment variable PLATFORM was not set.\n"; return; } my $FnTable = $Platforms{ lc( $ENV{PLATFORM} ) }; for ( @ARGV ) { if ( exists( $FnTable->{ lc( $_ ) } ) ) { print "\nProduct $Product - Platform $ENV{PLATFORM} - Functio +n $_\n"; $FnTable->{ lc( $_ ) }(); $Options = ''; } else { $Options .= " $_"; } } print "\n"; }

    The code also seems to be somewhat problematic or weird, because it calls the functions Checkout, Clean and Build when building the hashes; at least assuming you left out the definitions of these subroutines. Otherwise, if these are to be strings, you should put them into quotes to clearly mark them as strings.