Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Script to convert HBA WWNs to lowercase and add ":"[Updated]

by GrandFather (Saint)
on Dec 28, 2011 at 23:33 UTC ( [id://945436]=note: print w/replies, xml ) Need Help??


in reply to Script to convert HBA WWNs to lowercase and add ":"[Updated]

If you find yourself writing essentially the same code multiple times consider using a subroutine. Consider:

#!/usr/bin/perl use warnings; use strict; my $mode = fetch('Enter mode', 1); while (my $raw_wwn = fetch($mode eq 'a' ? "wwn (no ':')" : "wwn with ' +:'")) { if ($mode eq 'a') { if ($raw_wwn =~ /[^0-9a-fA-F]{16}$/) { print "Invalid Length Or Incorrect Format\n"; } else { print join (":", unpack ("(a2)*", lc ($raw_wwn))), "\n"; } next; } if ($raw_wwn =~ /[^:a-fA-F0-9]{23}$/) { print "Invalid Length Or Incorrect Format\n"; next; } $raw_wwn =~ s/://g; print "$raw_wwn\n"; } sub fetch { my ($prompt, $key) = @_; $prompt .= ' or q to quit' if $prompt; while (1) { print <<KEY if $key; Enter a for lowercase and colons. b to do it the other way around. q to quit KEY print "$prompt: " if $prompt; my $answer = lc <>; chomp $answer; exit if $answer eq 'q'; return $answer if $key && $answer =~ /^[ab]$/; return $answer if ! $key; print qq{"$answer" is not a valid mode.\n\n}; } }

In addition to drawing the prompting, input code and initial validation into one place, the code above uses fewer variables and those it does use are declared in the smallest sensible scope. The code also uses "early exits" (the return statements and next statements) to avoid extra levels of indentation and to (hopefully) make the logic flow clearer. Trivial cases (like the quit handling) are dealt with first.

Note the use of consistent indentation. Although I tend to write using this indentation style anyway, my editor is fairly smart about indentation and I use Perl::Tidy as a final pass over the code to clean up anything I've missed. Clear consistent indentation and use of white space helps a lot in making scripts understandable.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Script to convert HBA WWNs to lowercase and add ":"[Updated]
by perl514 (Pilgrim) on Dec 29, 2011 at 11:25 UTC

    Respected GrandFather,

    Thank you once again for the updates. I will surely follow up on the valuable suggestions you have provided. As usual, your suggestions are loaded with stuff that's completely new to me, and some stuff seems quite dense to absorb at first go for a newbie like me. But without guidance of you great folks here at PerlMonks, this would be a very difficult journey. You took time out of the Chrismas Vacations to reply to my query, this fact itself speaks volumes of the ever helping nature of the PerlMonks.

    I ran the code you provided and found that certain outputs were different. But thats ok, I will work through these.

    Basically, I had put the validation in the IF loop and put the actual execution in the ELSE loop because, if I dont do that and enter 10:00:00:00:c9:ab:cd:ef in prompt "a" where a user is supposed to enter WWN without ":", then I get "10::0:0::00::0:0::c9::a:b::cd::e:f". Notice the "::". I wanted the script to check in the beginning itself if the input contains ":", or if input contains non hex characters or if the input characters are less or more than the number of WWN elements.

    Similarly in option "b", if user enters a non hex WWN or a WWN that has anything else than Hex or ":", I wanted to throw an error message which is again why I have entered the checking part in the IF loop and the actual execution in the ELSE Loop

    I never thought that programming in Perl will be so much fun...heck, now its sort of getting a little addictive, I keep thinking of ways to make my script better. Actually, its not a big deal for you guys to write scripts like these, but for me, this script means a lot. :)

    Perlpetually Indebted To PerlMonks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found