in reply to Getting "un initialized value" error though variable is initialized. Please help.

Hi Corion, Thank you !! I set $wwn to 0 and that worked !!. Now I am not getting the error message. Here is the script now:
#!/usr/bin/perl use warnings; use strict; my $wwn = 0; until ( $wwn eq "q") { print "Enter the wwn: "; chomp ($wwn=<STDIN>); my @wwn = unpack ("(a2)*", lc($wwn)); @wwn = join (":", @wwn); print "@wwn\n"; }
And here is the output:
C:\Users\Documents\perl\practice>perl wwn.pl Enter the wwn: 10000000ABCDEFAB 10:00:00:00:ab:cd:ef:ab Enter the wwn: 10000000C9D2EEFG 10:00:00:00:c9:d2:ee:fg Enter the wwn: 500604AB12CDEFCD 50:06:04:ab:12:cd:ef:cd Enter the wwn: q q C:\Users\ugrankar\Documents\perl\practice>
Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

Replies are listed 'Best First'.
Re^2: Getting "un initialized value" error though variable is initialized. Please help.
by Anonymous Monk on Dec 17, 2011 at 16:26 UTC

    You can also use this alternative loop style that will always run at least once:

    my $wwn; do { print "Enter the wwn: "; chomp ($wwn=<STDIN>); my @wwn = unpack ("(a2)*", lc($wwn)); @wwn = join (":", @wwn); print "@wwn\n"; } until ($wwn eq "q");

    Also, it's confusing that you have @wwn and $wwn. You should consider renaming one.

      ++ for the do { ...; } loop, annonymonk, but not so much for
      "it's confusing that you have @wwn and $wwn."

      Respectfully disagree: where an array is the child of a string (or vice versa), having both use the same base_name makes all the sense in the world to me... and from the original post, it's pretty clear that "wwn" is a meaningful name to the OP (ie, not a case of naming stuff $var1, $var2, @arr3, %h4 or the like, which, of course, would warrant advice to select meaninful names like $ip, $mac, @wwm... (sorry, no hash example in the OP).

        Well, you're just defending it because it happens to contain your initials =P

        Anyway, I'd rename the scalar to $input as that makes sense to me. That is just my opinion, though. There are other small problems such as @wwn = join (":", @wwn); (empty an array and assign a scalar value into an array's first slot) -- that is, use a previously-defined array as a scalar variable -- but I'm not to split hairs over it as the OP did not ask to review his code.