Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by perl514 (Pilgrim)
on Dec 17, 2011 at 13:29 UTC ( [id://944073]=perlquestion: print w/replies, xml ) Need Help??

perl514 has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks,

I have written a small script that 1) Takes the input and converts it to lower case and then 2) inserts ":" between every two characters.

I wrote this script so that I dont have to keep adding the ":" manually. These are HBA WWNs that we use to manage our SAN.

Here is the script:

#!/usr/bin/perl use warnings; use strict; until (our $wwn eq "q") { print "Enter the wwn: "; chomp ($wwn=<STDIN>); my @wwn = unpack ("(a2)*", lc($wwn)); @wwn = join (":", @wwn); print "@wwn\n"; }
Everytime I execute it, I get the error :  Use of uninitialized value $wwn in string eq at wwn.pl line 5. The scripts runs fine, just keeps giving this message. Here is a sample output:
C:\Users\Documents\perl\practice>perl wwn.pl Use of uninitialized value $wwn in string eq at wwn.pl line 5. Enter the wwn: 10000000ABCDEFGH 10:00:00:00:ab:cd:ef:gh Enter the wwn: 10000000C9A2B3CD 10:00:00:00:c9:a2:b3:cd Enter the wwn: q q C:\Users\Documents\perl\practice>

I tried using  while ($wwn ne "q") but still I get the same error message. Which I now see makes sense because both these say the same thing.

Please let me know where I am going wrong. Also, I just browsed the net and found the unpack function and have tried using it. The Perldoc has a very small article on in that doesn't tell me much. Any other pointers that explains unpack will really help me. I don't like using stuff that I don't understand clearly in a script, but its been many days, and I am not advancing much so getting impatient and used unpack without much understanding it. Kindly help me.

Update: I tried changing $wwn to "our" thinking that stating it to be "my" may be creating issues, but that also doesn't seem to be the case.
Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

Replies are listed 'Best First'.
Re: Getting "un initialized value" error though variable is initialized. Please help.
by Corion (Patriarch) on Dec 17, 2011 at 13:37 UTC
    until (our $wwn eq "q")

    This is not line 8. Please do post the exact code that produces the exact error message.

    But while we are talking about this line - where does $wwn get the value prior to this comparison? What is the initial value of $wwn?

Re: Getting "un initialized value" error though variable is initialized. Please help.
by ww (Archbishop) on Dec 17, 2011 at 16:37 UTC
    Two small points:

    It's a "warning," not an "error."

    and

    Since "formatting" was initially an issue, suggest you consider using indentation -- in the interest of readability. This particular snippet isn't hard, of course, but as you create more complex code, white space can be a huge help.

Re: Getting "un initialized value" error though variable is initialized. Please help.
by Khen1950fx (Canon) on Dec 18, 2011 at 09:39 UTC
    Using Grandfather's elegant solution, I reversed the condition and built a simple until loop out of that.
    #!/usr/bin/perl -l use strict; use warnings; my $name = ''; until ( $name eq 'q' ) { print "Enter the wwn: "; chomp( $name = lc <STDIN> ); print join( ':', unpack( "(a2)*", $name ) ); next; }
Re: Getting "un initialized value" error though variable is initialized. Please help.
by perl514 (Pilgrim) on Dec 17, 2011 at 14:08 UTC
    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.

      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).

Re: Getting "un initialized value" error though variable is initialized. Please help.
by AnomalousMonk (Archbishop) on Dec 17, 2011 at 23:29 UTC
    ... Perldoc has a very small article on in that doesn't tell me much. Any other pointers that explains unpack will really help me. [emphasis added]

    I don't know if perlpacktut is the article to which you refer (a link for reference is always welcome), but it seems to me to be a very thorough discussion; I recommend it if you haven't already seen it.

Re: Getting "un initialized value" error though variable is initialized. Please help.
by perl514 (Pilgrim) on Dec 17, 2011 at 18:24 UTC

    Respected Monks,

    Thank you for your direction and guidance.

    Anonymous Monk, my intention of using $wwn and @wwn is not to confuse.I do understand that @wwn and $wwn are two seperate entities and that having the same name doesn't mean that they are related in any ways, unless ofcourse I say $wwn[0] or something like that which indicates that its a scalar context of @wwn. I used $wwn and @wwn because the name WWN stands for World Wide Name and this ways, I can make a mental note that both these refer to it, though inherently they are not related.

    ww, I will surely follow indentation.

    Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

      Overloading names in that way is a great way to introduce subtle bugs and make code hard to understand. If it's an array it likely contains more than one item so using a plural form of the name (@wwns) may make sense. Alternatively I'd use the slightly more generic variable name $name for the input variable.

      Oh, and don't use our as a substitute for my, especially as a temporary variable - it doesn't do what you think it does and is not at all appropriate. I'd tend to rewrite the thing as:

      #!/usr/bin/perl use warnings; use strict; while (1) { print "Enter the wwn: "; chomp (my $name = lc <STDIN>); last if $name eq "q"; print join (':', unpack ("(a2)*", $name)), "\n"; }

      Update: s/own/our/

      True laziness is hard work

        I quite agree with GrandFather.

        In fact, I think I would go a bit further and advise the use of more 'documentary' scalar variable names:
            chomp (my $raw_wwn = lc <STDIN>);
            ...
            my $fixed_wwn = join ':', unpack '(a2)*', $raw_wwn;
            do_something_with($fixed_wwn);

Re: Getting "un initialized value" error though variable is initialized. Please help.
by perl514 (Pilgrim) on Dec 18, 2011 at 09:59 UTC

    Respected Monks,

    Thank you for your pointers and the time you take to patiently explain things.

    On youtube video, I saw Peter J. Scott stating that Perl's one of biggest plus is the community, perlmonks being one of them. I have been posting consitently on the forums and from what I see, he is absolutely correct. You all are extremely helpful and very knowledgeable.I had done some additions to this script and uploaded it before I read the replies here. I will definitely do the updates accordingly.

    Thank you all.

    Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.
Re: Getting "un initialized value" error though variable is initialized. Please help.
by perl514 (Pilgrim) on Dec 17, 2011 at 13:49 UTC
    Hi Corion, Yessir you are right. I changed the formatting just before pasting the script here. My mistake. Sorry for that. Its on line 5. I have updated accordingly.
    Perl Version - (v5.14.2) MSWin32-x64-multi-thread on Windows 7 64 Bit.

Log In?
Username:
Password:

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

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

    No recent polls found