Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

esn.pl

by defyance (Curate)
on Aug 26, 2002 at 03:05 UTC ( [id://192758]=sourcecode: print w/replies, xml ) Need Help??
Category: Utilities
Author/Contact Info DeFyance -=at=- comcast.net defyance
Description: A simple esn converter. Its pretty crappy code, I am posting it here, in hopes that someone can come up with a better/more efficient/prettier way to do this..
#!/usr/bin/perl -w
use strict;

    #Lets convert that ESN
    print "\n\n", "*" x22, "\n";
    print "ESN Converter v.01", "\n";
    print "*" x22, "\n";
    print "Please enter ESN:";
    chomp( my $hexnum = <STDIN> );

    push (my @hexnum, $hexnum);
    @hexnum = split ( ' ', $hexnum );

if ( $hexnum eq "" ) {
 
    print "Invalid Input!\n";
    print "Press <ENTER> to continue";<>; 
     
}
 
else {
 
  go(@hexnum);

}

sub go {

  if ( length( $hexnum[0] ) == 8 ) {
      my $mfg   = hex( substr( $hexnum, 0, 2 ) );
      my $serno = hex( substr( $hexnum, 2, 6 ) );
      printf( "\nDecimal:  %d %d\nHex:  %s\n\n", $mfg, $serno, $hexnum
+ );
      print "Press <ENTER> to continue";<>;
  }
  elsif ( length( $hexnum[0] ) == 3 ) {
 
      printf( "\nDecimal:  %3d %8d\n", $hexnum[0], $hexnum[1] );
      printf( "Hex:  %2X%6X\n\n",      $hexnum[0], $hexnum[1] );
      print "Press <ENTER> to continue";<>;
  }

 else {
 
      die "Invalid Input!\n";
      print "Press <ENTER> to continue";<>;
  }
 
}
Replies are listed 'Best First'.
Re: esn.pl
by graff (Chancellor) on Aug 27, 2002 at 06:03 UTC
    Well, there are a number of things wrong here, so since you asked for improvements...
    • you're putting the same entry into @hexnum two times, in two different ways, and the latter is simply negating the former:
      push (my @hexnum, $hexnum); @hexnum = split ( ' ', $hexnum );
      One or the other of these will do, but don't do both. (It seems like the latter is better given the current "user interface" -- especially if you give the user a clue that the script will accept multiple esn's on one line of input, separated by spaces -- in that case, just use:
      $_ = <STDIN>; @hexnum = split;
    • You pass @hexnum to the "go" sub, but the sub never checks its parameter list -- it just looks at the global @hexnum; you can do it either way, but it makes no sense doing it both ways at once. (Most folks would recommend passing parameters -- else, why have a subroutine at all?
      sub go { my @esns = @_; ... }
    • Your checks on the input are not going to be effective for a variety of unusable strings that the user might type in.

    And of course the whole thing can be done more compactly and simply:

    use English; if (@ARGV and $ARGV[0] eq "-v") { shift; # crude method for option handling (cf. Getopt::Std) print STDERR "ESN Converter v.02\n"; # report version when asked } # (otherwise, keep quiet about that) unless (@ARGV and -r $ARGV[0]) { # if no file name, go interactive print STDERR "Usage: $0 [esn_list.filename]\n"; $eof = ( $OSNAME =~ /.n.x$/ ) ? "^D" : "^Z"; $prompt = "Please enter an ESN string (or $eof to exit): "; print STDERR $prompt; } while (<>) { s/\s+//g; # start by eliminating all whitespace unless ( /^([0-9a-f]{2})([0-9a-f]{6})$/ ) { print STDERR "skipped input string $_ : not a valid ESN\n"; next; # only accept 8-digit hex strings } ($mfg,$serno) = (hex $1, hex $2); printf( "Hex: %s == Decimal: %d %d\n",$_,$mfg,$serno); } continue { print STDERR $prompt if $prompt; }
    This adds the ability to accept an input file that contains one or more ESN's (one per line), since this might be more effective for some users than having to type ESNs interactively -- but interactive type-ins are still possible as well, with the ability to type in more than one ESN on a given run.

    Printing prompts, version and error messages to STDERR makes it easy to redirect valid results to a file, since that might be a useful feature.

    Other approaches are possible and could be better, but this seems close to your original intent.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://192758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-19 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found