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

#!/fellow/monks.pl

I keep on getting "Use of uninitialized value in concatenation (.) or string at" errors while running my script. Funny enough, I "know" what the problem is, I'm just a bit confused on how to "solve" it. If it was one or two variables, no big deal -- I'll define them. But now I have a whole stack of them, and I'd like to define them all in one go. So far my attempts to do qw(var1,var2,var3) = (); didn't work. Any ideas?

Thanks

my ($txt_AnniversaryDate,$txt_BithDate,$txt_EmailAddr,$txt_FaxNo, $txt_MobileNo,$txt_Name,$txt_PhoneNo,$txt_PhysicalAddress, $txt_PhysicalCode,$txt_PhysicalCountry,$txt_PhysicalTown, $txt_PostalAddress,$txt_PostalCode,$txt_PostalCountry, $txt_PostalTown,$txt_Sex,$txt_Surname); qw(($txt_AnniversaryDate,$txt_BithDate,$txt_EmailAddr,$txt_FaxNo, $txt_MobileNo,$txt_Name,$txt_PhoneNo,$txt_PhysicalAddress, $txt_PhysicalCode,$txt_PhysicalCountry,$txt_PhysicalTown, $txt_PostalAddress,$txt_PostalCode,$txt_PostalCountry, $txt_PostalTown,$txt_Sex,$txt_Surname) = ();

Thanks!

#!/massyn.pl

Don't -- me without telling me why. If you asked a stupid question, I wouldn't down vote you.

Edit by thelenm: added some line breaks

Replies are listed 'Best First'.
Re: "Use of uninitialized value" with many variables
by The Mad Hatter (Priest) on Oct 06, 2003 at 02:11 UTC
    Slightly off topic (and not an answer to your question), you really should be using a hash for all of those. Whenver you see a common element (txt_ in this case) in a variable name, that should clue you in. Something like
    my %txt = ( AnniversaryDate => '', BithDate => '', EmailAddr => '', FaxNo => '', MobileNo => '', ... );
    would be better. If you use a hash, you can also assign to a hash key at any time, so just declare the hash my %txt = (); and then when you want to set a key/value pair, do $txt{key} = "value";
      Although I agree with your suggestion of using a hash in this instance, I would actually code it slightly differently.
      my %txt = map { $_ => '' } qw{AnniversaryDate BirthDate EmailAddr FaxNo MobileMo ... };

      When initializing hash keys all to the same value I've found this idiom helps me make fewer mistakes. It also means less repetitive typing. (For those of us who type slowly, that's important.)

      G. Wade
        my %txt; my @field = qw( AnniversaryDate BirthDate EmailAddr FaxNo MobileNo ... ); @txt{@field} = ('') x @field;
        Update: corrected thinko as per CombatSquirrel's reply.

        Makeshifts last the longest.

Re: "Use of uninitialized value" with many variables
by Zaxo (Archbishop) on Oct 06, 2003 at 02:16 UTC

    You can set all these defined and empty with this,

    my ($txt_AnniversaryDate, $txt_BithDate, $txt_EmailAddr, $txt_FaxNo, $txt_MobileNo, $txt_Name, $txt_PhoneNo, $txt_PhysicalAddress, $txt_PhysicalCode, $txt_PhysicalCountry, $txt_PhysicalTown, $txt_PostalAddress, $txt_PostalCode, $txt_PostalCountry, $txt_PostalTown, $txt_Sex, $txt_Surname) = ('') x 17;

    After Compline,
    Zaxo

      You don't even have to count how many there are of them:
      $_ = '' for my ( $txt_AnniversaryDate, $txt_BithDate, $txt_EmailAddr, $txt_FaxNo, $txt_MobileNo, $txt_Name, $txt_PhoneNo, $txt_PhysicalAddress, $txt_PhysicalCode, $txt_PhysicalCountry, $txt_PhysicalTown, $txt_PostalAddress, $txt_PostalCode, $txt_PostalCountry, $txt_PostalTown, $txt_Sex, $txt_Surname );
      (Trick borrowed from TheDamian.)

      UPDATE: I had a typo noticed by atcroft.

        Or use map in void context, which as of Perl 5.8.1 is no longer "costly" (inefficient). As of v5.8.1, map in void context doesn't create a list that just gets thrown away. Hense:

        map { $_ = "" } ($txt_AnniversaryDate, $txt_BithDate, $txt_EmailAddr, $txt_FaxNo, $txt_MobileNo, $txt_Name, $txt_PhoneNo, $txt_PhysicalAddress, $txt_PhysicalCode, $txt_PhysicalCountry, $txt_PhysicalTown, $txt_PostalAddress, $txt_PostalCode, $txt_PostalCountry, $txt_PostalTown, $txt_Sex, $txt_Surname );

        Anything but pretty.


        Dave


        "If I had my life to do over again, I'd be a plumber." -- Albert Einstein