For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > December 2004 > adding functionality









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author adding functionality
Christopher Spears

2004-12-16, 3:56 am

Here is the latest version of my script:

#!/usr/bin/perl -w
use strict;

my $Pixarport = 7498;
my $host = "lex";
my $Pixarfile = $Pixarport."@".$host;
my @input;
my $line;
my $line_number;
my $file_length;
my @features_array;
my @handles_array;
my $handles;
my $features;
my $sizeof;
my $counter = 0;
my $answer;
my $string;
my $userid = `whoami`;

@input = `cat clicenses_output`;
$file_length = scalar(@input);

print "Is ".$userid." your userid (Y or N)?";
$answer = <STDIN>;
if ($answer =~ /^[nN]$/) {
print "Enter your userid: ";
chomp($userid = <STDIN> );
}elsif ($answer =~ /^[yY]$/) {
print $userid." is your userid.\n";
}

print "Do you wish to:"."\n";
print "1) remove all of your floating licenses at
once"."\n";
print "2) remove them one at a time"."\n";
$answer = <STDIN>;

if ($answer == 1) {
foreach ($line_number = 0; $line_number <=
$file_length; $line_number++){
last if ($line_number == $file_length);
$line = $input[$line_number];
next unless $line =~ /\S/;
$line =~ s/^\s+//;
@features_array = split /\s/, $line ;
#split $line along whitespace and place into an array
LOOKING_USERS: if ($features_array[0] eq
"Users") {
($features = $features_array[2]) =~ s/://;
#assign second element into $features and remove
colon
$line_number++;
}
$line = $input[$line_number];
if ($line =~ /Users/) {
@features_array = split /\s/, $line;
#split $line along whitespace and place into an array
goto LOOKING_USERS;
}
next unless $line =~ /\S/;
$line =~ s/^\s+//;
@handles_array = split /\s/, $line; #split
$line along whitespace and place into an array
$sizeof = scalar(@handles_array);
if ($sizeof >= 9 && $handles_array[0] eq
$userid){
($handles = $handles_array[5]) =~ s/\),//;
#assign ninth element into $handles and remove ),
$string = join " ", @handles_array;
print $string."\n";
print "lmutil lmremove -c ".$Pixarfile."
-h ".$features." ".$host." ".$Pixarport."
".$handles."\n";
}
}
}

Basically, I'm trying to add some extra functionality
to the script. I want the user to be able to pick
between have the script automatically remove all of
the licenses for them or query them like so:

Remove such-and-such license (Y or N)?

What would be the best way of going about this?
JupiterHost.Net

2004-12-16, 8:56 am



Christopher Spears wrote:
> Here is the latest version of my script:
>
> #!/usr/bin/perl -w
> use strict;
>
> my $Pixarport = 7498;
> my $host = "lex";
> my $Pixarfile = $Pixarport."@".$host;
> my @input;
> my $line;
> my $line_number;
> my $file_length;
> my @features_array;
> my @handles_array;
> my $handles;
> my $features;
> my $sizeof;
> my $counter = 0;
> my $answer;
> my $string;
> my $userid = `whoami`;
>
> @input = `cat clicenses_output`;


Why not

my @input = `cat ...

here instead of my'ing it above and then assigning to it here?

> $file_length = scalar(@input);


Sama as above...

> print "Is ".$userid." your userid (Y or N)?";


Why not

print "Is $userid your userid (Y or N)?";

the "".$var."" is pointless and confusing...

> $answer = <STDIN>;
> if ($answer =~ /^[nN]$/) {
> print "Enter your userid: ";
> chomp($userid = <STDIN> );
> }elsif ($answer =~ /^[yY]$/) {
> print $userid." is your userid.\n";



same as above...

> }
>
> print "Do you wish to:"."\n";


same as above...

> print "1) remove all of your floating licenses at
> once"."\n";


same as above...

> print "2) remove them one at a time"."\n";



same as above...

You could even use a here doc:

print <<OPTION;
Do you wish to:
1) remove all ...
2) remove them ....
OPTION

> $answer = <STDIN>;


same as first "why not" above...

i know that wasn't your original question but it may be easier for you
if the code was more readable...

HTH :)

Lee.M - JupiterHost.Net
Charles K. Clarkson

2004-12-16, 3:59 pm

Christopher Spears <cspears2002@yahoo.com> wrote:

: Here is the latest version of my script:

I think you are using the wrong approach to parsing
this output. You're making it a lot more complicated
than it needs to be. I omitted the report and question
sections. Your report data is in %licenses.

Read 'perldsc' for info on printing from a hash of
arrays (HoA).

I use a sub routine to fetch the licenses. In that
sub I remove all lines except the ones starting with a
" and those containing a handle.

use strict;
use warnings;
use Data::Dumper 'Dumper';

my $name;
my %licenses;
foreach ( fetch_licenses() ) {
if ( /"([^"]+)/ ) {
$name = $1;

} elsif ( m|lex/\d+ (\d+)| ) {
push @{ $licenses{ $name } }, $1;
}
}

print Dumper \%licenses;


sub fetch_licenses {
my @raw_licenses = `cat clicenses_output`;

# keep only license names and handles
return grep { m/^\s+"/ or m|\(lex/\d+ \d+\)| } @raw_licenses;
}

__END__

Sometimes it necessary to scrap what you are doing
and just start again.

HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com