For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > code improvement using LABELS within subroutines????









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 code improvement using LABELS within subroutines????
DBSMITH@OhioHealth.com

2005-07-24, 8:29 pm

All,

I am wanting to improve my code b/c I am making 3 identical system calls.
So I thought a subroutine with that one call and LABELS for each condition
would improve it.
Is this possible b/c I am getting errors stating cannot find FPR.


sub finger
{

open (GPG, "gpg --fingerprint |") or die "unable to open pipe sys call
+ (1)... Broken? $!";

for (;<GPG>;)
{
EXP: if ( /(?i)expires/ )
{
y<-]>//d;
$expdate = +(split)[4];
print $expdate,"\n";
}

FPR: if (/talx/ig .. /expire/ig)
{
if (/key/i)
{
y<[Key,fingerprint,=]>//d;
y{ }//d; print FPR $_;
print "Now deleting key\n";
# call;
last;
}
}

KEY: if ( /(?i)pub/ and /(?i)talx/)
{
$keyid = (substr ($_,11,9));
}

} # END FOR LOOP

close (GPG) or die "unable to close pipe sys call (0)... Broken? $!";

} # END ROUTINE

&finger(goto EXP);








Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams
614-566-4145

Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

On Jul 21, DBSMITH@OhioHealth.com said:

> I am wanting to improve my code b/c I am making 3 identical system calls.
> So I thought a subroutine with that one call and LABELS for each condition
> would improve it.


> sub finger {
>
> open (GPG, "gpg --fingerprint |") or die "unable to open pipe sys call
> + (1)... Broken? $!";
>
> for (;<GPG>;) {
> EXP: if ( /(?i)expires/ ) { ... }
> FPR: if (/talx/ig .. /expire/ig) { ... }

KEY: if ( /(?i)pub/ and /(?i)talx/) { ... }
> } # END FOR LOOP
>
> close (GPG) or die "unable to close pipe sys call (0)... Broken? $!";
>
> } # END ROUTINE


That looks fine. There's really no *need* for the labels, though. You're
not using them, except below, where you're using them incorrectly:

> &finger(goto EXP);


I think you just want to write:

finger();

I haven't taken a guess at what function(goto XYZ) does, but if I had to,
I'd say it wouldn't end up calling the function at all, and just go to
XYZ, which might be a problem, given that (in your case) the label is
defined INSIDE a function.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
DBSMITH@OhioHealth.com

2005-07-24, 8:29 pm

ok thanks but that does not really help. Sorry.
your right about finger(), but based on my code I want to execute a certain
block label

if ( mary)
finger (mary)

if (jim)
finger(jim)

or

if (mary)
finger(goto mary)

if (jim)
finger(goto jim)


Any other ideas to avoid the same system call 3 times?





Jeff 'japhy'
Pinyan
<japhy@perlmonk.o To
rg> DBSMITH@OhioHealth.com
cc
07/21/2005 02:34 beginners@perl.org
PM Subject
Re: code improvement using LABELS
within subroutines????










On Jul 21, DBSMITH@OhioHealth.com said:

> I am wanting to improve my code b/c I am making 3 identical system calls.
> So I thought a subroutine with that one call and LABELS for each

condition
> would improve it.


> sub finger {
>
> open (GPG, "gpg --fingerprint |") or die "unable to open pipe sys call
> + (1)... Broken? $!";
>
> for (;<GPG>;) {
> EXP: if ( /(?i)expires/ ) { ... }
> FPR: if (/talx/ig .. /expire/ig) { ... }

KEY: if ( /(?i)pub/ and /(?i)talx/) { ... }
> } # END FOR LOOP
>
> close (GPG) or die "unable to close pipe sys call (0)... Broken? $!";
>
> } # END ROUTINE


That looks fine. There's really no *need* for the labels, though. You're
not using them, except below, where you're using them incorrectly:

> &finger(goto EXP);


I think you just want to write:

finger();

I haven't taken a guess at what function(goto XYZ) does, but if I had to,
I'd say it wouldn't end up calling the function at all, and just go to
XYZ, which might be a problem, given that (in your case) the label is
defined INSIDE a function.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart


Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

On Jul 21, DBSMITH@OhioHealth.com said:

> ok thanks but that does not really help. Sorry.
> your right about finger(), but based on my code I want to execute a certain
> block label
>
> if ( mary)
> finger (mary)
>
> if (jim)
> finger(jim)


Oh, then in that case:

finger($user);

# ...

sub finger {
my $who = shift;

open GPG, ...;

if ($who eq 'mary') {
# ...
}
elsif ($who eq 'jim') {
# ...
}

close GPG;
}

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Scott R. Godin

2005-07-24, 8:29 pm

Jeff 'japhy' Pinyan wrote:
> On Jul 21, DBSMITH@OhioHealth.com said:
>
>
>
> Oh, then in that case:
>
> finger($user);
>
> # ...
>
> sub finger {
> my $who = shift;
>
> open GPG, ...;
>
> if ($who eq 'mary') {
> # ...
> }
> elsif ($who eq 'jim') {
> # ...
> }
>
> close GPG;
> }
>


Then again, if you're using Perl 5.7.3 or later, you can use Switch:

use Switch;
use Carp;
finger($user);

sub finger
{
my $who = shift || croak "no user supplied";
open GPG, ...;
switch($who) {
case 'mary' { #... }
case 'jim' { #... }
case 'blah {
# multiple lines
# ...
}
}
close GPG;
}

perldoc Switch for details. :)
DBSMITH@OhioHealth.com

2005-07-24, 8:29 pm

thank you,

I will consider all of these.


derek




"Scott R. Godin"
<nospam@webdragon
.net> To
beginners@perl.org
07/22/2005 02:10 cc
PM
Subject
Re: code improvement using LABELS
within subroutines????










Jeff 'japhy' Pinyan wrote:
> On Jul 21, DBSMITH@OhioHealth.com said:
>
>
>
> Oh, then in that case:
>
> finger($user);
>
> # ...
>
> sub finger {
> my $who = shift;
>
> open GPG, ...;
>
> if ($who eq 'mary') {
> # ...
> }
> elsif ($who eq 'jim') {
> # ...
> }
>
> close GPG;
> }
>


Then again, if you're using Perl 5.7.3 or later, you can use Switch:

use Switch;
use Carp;
finger($user);

sub finger
{
my $who = shift || croak "no user supplied";
open GPG, ...;
switch($who) {
case 'mary' { #... }
case 'jim' { #... }
case 'blah {
# multiple lines
# ...
}
}
close GPG;
}

perldoc Switch for details. :)

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Sponsored Links







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

Copyright 2008 codecomments.com