Error uninitialized value

Discussion in 'Perl Misc' started by Wijnand Nijs, Aug 20, 2009.

  1. Wijnand Nijs

    Wijnand Nijs Guest

    Hello,

    I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?

    Thanks and regards...
    Wijnand


    Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

    14 # get form params
    15 #-----------------
    16
    17 $Year = param("Year");
    18 $Month = param("Month");
    19
    20
    21 # convert localtime
    22 #-------------------
    23
    24 $HourOffset = 0;
    25
    26 $time = time;
    27 ($local_monthday,$local_month,$local_year) = (localtime($time+($HourOffset*3600)))[3,4,5];
    28 $local_month = $local_month + 1;
    29 $local_year = $local_year + 1900;
    30
    31
    32 # check form params
    33 #-------------------
    34
    35 if ($Year eq "") {
    36 $Year = $local_year;
    37 };
    Wijnand Nijs, Aug 20, 2009
    #1
    1. Advertising

  2. body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } .standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } .code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } .codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Wijnand Nijs wrote: Hello,

    I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?

    Thanks and regards...
    Wijnand

    Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

    14 # get form params
    15 #-----------------
    16
    17 $Year  = param("Year");
    18 $Month = param("Month");
    19
    20
    21 # convert localtime
    22 #-------------------
    23
    24 $HourOffset = 0;
    25
    26 $time = time;
    27 ($local_monthday,$local_month,$local_year) = (localtime($time+($HourOffset*3600)))[3,4,5];
    28 $local_month = $local_month + 1;
    29 $local_year  = $local_year  + 1900;
    30
    31
    32 # check form params
    33 #-------------------
    34 35 if ($Year eq "") {
    36   $Year    = $local_year;
    37 };
    $Year ||= $local_year;


    --
    Andrew DeFaria
    Is there another word for synonym?
    Andrew DeFaria, Aug 20, 2009
    #2
    1. Advertising

  3. Wijnand Nijs <> wrote:
    > I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?


    > Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.


    > 14 # get form params
    > 15 #-----------------
    > 16
    > 17 $Year = param("Year");


    > 35 if ($Year eq "") {
    > 36 $Year = $local_year;
    > 37 };


    IIRC if "Year" hasn't been filled in in the form you seem to be
    getting this then param("Year') is an undefined value, not an
    empty string. So all you have to do is to apply the 'defined'
    operator instead of comparing to "". Or write it all in a single
    line:

    $Year = param("Year") || $local_year;

    This deals with the case that param("Year") is not defined as well
    as that it's an empty string.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 20, 2009
    #3
  4. Wijnand Nijs

    Wijnand Nijs Guest

    Jens Thoms Toerring schreef:
    > Wijnand Nijs <> wrote:
    >> I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?

    >
    >> Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

    >
    >> 14 # get form params
    >> 15 #-----------------
    >> 16
    >> 17 $Year = param("Year");

    >
    >> 35 if ($Year eq "") {
    >> 36 $Year = $local_year;
    >> 37 };

    >
    > IIRC if "Year" hasn't been filled in in the form you seem to be
    > getting this then param("Year') is an undefined value, not an
    > empty string. So all you have to do is to apply the 'defined'
    > operator instead of comparing to "". Or write it all in a single
    > line:
    >
    > $Year = param("Year") || $local_year;
    >
    > This deals with the case that param("Year") is not defined as well
    > as that it's an empty string.
    > Regards, Jens


    Thanks!!!

    But what to do with:

    Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.

    in:

    109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;

    ???

    Wijnand
    Wijnand Nijs, Aug 20, 2009
    #4
  5. body { font: Helvetica, Arial, sans-serif; } p { font: Helvetica, Arial, sans-serif; } .standout { font-family: verdana, arial, sans-serif; font-size: 12px; color: #993333; line-height: 13px; font-weight: bold; margin-bottom: 10px; } .code { border-top: 1px solid #ddd; border-left: 1px solid #ddd; border-right: 2px solid #000; border-bottom: 2px solid #000; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: #ffffea; color: black; -moz-border-radius: 10px; } .codedark { border-top: 10px solid #03f; border-left: 1px solid #ddd; border-right: 2px solid grey; border-bottom: 2px solid grey; padding: 10px; margin-top: 5px; margin-left: 5%; margin-right: 5%; background: black; color: yellow; -moz-border-radius: 10px; } #code { color: black; font-size: 14px; font-family: courier; padding-left: 5px; } #line-number { color: #804000; font-family: Arial; font-size: 14px; padding-right: 5px; border-right: 1px dotted #804000; } blockquote[type=cite] { padding: 0em .5em .5em .5em !important; border-right: 2px solid blue !important; border-left: 2px solid blue !important; } blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid maroon !important; border-left: 2px solid maroon !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid teal !important; border-left: 2px solid teal !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid purple !important; border-left: 2px solid purple !important; } blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] blockquote[type=cite] { border-right: 2px solid green !important; border-left: 2px solid green !important; } Wijnand Nijs wrote: Thanks!!!

    But what to do with:

    Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.

    in:

    109  $SmallTable{int($dateday)} =~ s/^&lt;BR&gt;&lt;HR NOSHADE WIDTH=25%&gt;//;

    ???
    Is $dateday defined? Why would you use int for a hash key? Finally is $SmallTable defined with the int of $dateday as a hash key?


    --
    Andrew DeFaria
    I used to have a handle on life, then it broke.
    Andrew DeFaria, Aug 20, 2009
    #5
  6. Wijnand Nijs <> wrote:
    > But what to do with:


    > Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.


    > in:


    > 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;


    Don't know if Andrew came up with something useful (my newsreader
    doesn't display HTML messages). But what are '$dateday' and
    '%SmallTable'? Why do you need the int() on '$dateday'? Since
    you don't show any code I can't say more than Perl already did,
    i.e. that either '$dateday' or '$SmallTable{int($dateday)}' is
    not defined (those are the only variables on that line). Why this
    is the case can only be explained when you show how you set up
    '%SmallTable' and '$dateday'.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 20, 2009
    #6
  7. Wijnand Nijs

    Nathan Keel Guest

    Wijnand Nijs wrote:

    > Jens Thoms Toerring schreef:
    >> Wijnand Nijs <> wrote:
    >>> I get a lot of "uninitialized value" errors in my logfile. The
    >>> script is working fine but I don't like errors I don't understand.
    >>> Ok, if there are no form params, I understand that $Year en $Month
    >>> are empty. Is there a nice way to give $Year and $Month a value if
    >>> there are no form params without an error message in the log fiale?

    >>
    >>> Use of uninitialized value in string eq at
    >>> G:/cgi-bin/calendar/calendar.pl line 35.

    >>
    >>> 14 # get form params
    >>> 15 #-----------------
    >>> 16
    >>> 17 $Year = param("Year");

    >>
    >>> 35 if ($Year eq "") {
    >>> 36 $Year = $local_year;
    >>> 37 };

    >>
    >> IIRC if "Year" hasn't been filled in in the form you seem to be
    >> getting this then param("Year') is an undefined value, not an
    >> empty string. So all you have to do is to apply the 'defined'
    >> operator instead of comparing to "". Or write it all in a single
    >> line:
    >>
    >> $Year = param("Year") || $local_year;
    >>
    >> This deals with the case that param("Year") is not defined as well
    >> as that it's an empty string.
    >> Regards, Jens

    >
    > Thanks!!!
    >
    > But what to do with:
    >
    > Use of uninitialized value in substitution (s///) at
    > G:/cgi-bin/calendar/calendar.pl line 109.
    >
    > in:
    >
    > 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
    >
    > ???
    >
    > Wijnand


    Looks like you're trying to make a script someone else wrote work, or
    you'd know what and why the above is set to do whatever you think it's
    supposed to do. Going by the code you've posted, I'd recommend
    dropping the idea of using this calendar script and to use a better
    one.
    Nathan Keel, Aug 20, 2009
    #7
  8. Wijnand Nijs

    Wijnand Nijs Guest

    Nathan Keel schreef:
    > Wijnand Nijs wrote:
    >
    >> Jens Thoms Toerring schreef:
    >>> Wijnand Nijs <> wrote:
    >>>> I get a lot of "uninitialized value" errors in my logfile. The
    >>>> script is working fine but I don't like errors I don't understand.
    >>>> Ok, if there are no form params, I understand that $Year en $Month
    >>>> are empty. Is there a nice way to give $Year and $Month a value if
    >>>> there are no form params without an error message in the log fiale?
    >>>> Use of uninitialized value in string eq at
    >>>> G:/cgi-bin/calendar/calendar.pl line 35.
    >>>> 14 # get form params
    >>>> 15 #-----------------
    >>>> 16
    >>>> 17 $Year = param("Year");
    >>>> 35 if ($Year eq "") {
    >>>> 36 $Year = $local_year;
    >>>> 37 };
    >>> IIRC if "Year" hasn't been filled in in the form you seem to be
    >>> getting this then param("Year') is an undefined value, not an
    >>> empty string. So all you have to do is to apply the 'defined'
    >>> operator instead of comparing to "". Or write it all in a single
    >>> line:
    >>>
    >>> $Year = param("Year") || $local_year;
    >>>
    >>> This deals with the case that param("Year") is not defined as well
    >>> as that it's an empty string.
    >>> Regards, Jens

    >> Thanks!!!
    >>
    >> But what to do with:
    >>
    >> Use of uninitialized value in substitution (s///) at
    >> G:/cgi-bin/calendar/calendar.pl line 109.
    >>
    >> in:
    >>
    >> 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
    >>
    >> ???
    >>
    >> Wijnand

    >
    > Looks like you're trying to make a script someone else wrote work, or
    > you'd know what and why the above is set to do whatever you think it's
    > supposed to do. Going by the code you've posted, I'd recommend
    > dropping the idea of using this calendar script and to use a better
    > one.


    Hello Andrew, Jens and Nathan

    Yes it is a script of someone else. I can drop this script and look for another, but I am also trying to learn something (writing Perl scripts).

    I have a few files with (birthday- and other)events with a line format:

    00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm

    after collecting and sorting the data the script filters de events for that month to a hash:

    foreach $line (@sortedevents) {

    ($date,$event,$URL) = split (/\|/, $line);
    ($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;

    # collect the events for $dateday
    if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) && (int($datemonth) == int($Month))) {
    if ($URL) {
    $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A HREF=\"$URL\">$event</A>";
    } else {
    $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
    }
    }

    # drop the first HR
    $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
    }

    with this hash the calendar for that month is built.

    Thanks for your help, regards...
    Wijnand
    Wijnand Nijs, Aug 20, 2009
    #8
  9. Wijnand Nijs

    Jim Gibson Guest

    In article <4a8d9554$0$187$4all.nl>, Wijnand Nijs
    <> wrote:


    > I have a few files with (birthday- and other)events with a line format:
    >
    > 00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm
    >
    > after collecting and sorting the data the script filters de events for that
    > month to a hash:
    >
    > foreach $line (@sortedevents) {
    >
    > ($date,$event,$URL) = split (/\|/, $line);
    > ($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;


    You may have a line that does not match the above reqular expression,
    resulting in invalid values for $dateyear, etc. You should always check
    whether the RE has been successfully matched before using the values
    from the match.

    >
    > # collect the events for $dateday
    > if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) &&
    > (int($datemonth) == int($Month))) {


    This if statement expression may evaluate to false, ...

    > if ($URL) {
    > $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A
    > HREF=\"$URL\">$event</A>";
    > } else {
    > $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
    > }
    > }
    >
    > # drop the first HR
    > $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;


    .... but here you assume that it succeeded and put a value in
    $SmallTable. You should put this line somewhere else, where you are
    assured of having a value in $SmallTable{int($dateday)}, or at least
    test for a value before trying to modify it:

    if( $SmallTable{int($dateday)} ) {
    ...
    }

    > }
    >
    > with this hash the calendar for that month is built.


    A possibly better approach would be to push each event into an array,
    then use join to concatenate the events when you have finished parsing
    the file.

    --
    Jim Gibson
    Jim Gibson, Aug 20, 2009
    #9
  10. Wijnand Nijs

    Nathan Keel Guest

    Wijnand Nijs wrote:


    >>>
    >>> But what to do with:
    >>>
    >>> Use of uninitialized value in substitution (s///) at
    >>> G:/cgi-bin/calendar/calendar.pl line 109.


    That says it all.


    > # drop the first HR
    > $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;


    That is trying to blindly substitute.

    I would not recommend learning from a bad script, unless you just want
    to learn what to avoid. Trying to fix a bad script is possibly
    helpful, but more likely will confuse you.
    Nathan Keel, Aug 20, 2009
    #10
  11. (Jens Thoms Toerring) writes:
    > Wijnand Nijs <> wrote:
    >> I get a lot of "uninitialized value" errors in my logfile. The
    >> script is working fine but I don't like errors I don't
    >> understand. Ok, if there are no form params, I understand that
    >> $Year en $Month are empty. Is there a nice way to give $Year and
    >> $Month a value if there are no form params without an error message
    >> in the log fiale?

    >
    >> Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.

    >
    >> 14 # get form params
    >> 15 #-----------------
    >> 16
    >> 17 $Year = param("Year");

    >
    >> 35 if ($Year eq "") {
    >> 36 $Year = $local_year;
    >> 37 };

    >
    > IIRC if "Year" hasn't been filled in in the form you seem to be
    > getting this then param("Year') is an undefined value, not an
    > empty string. So all you have to do is to apply the 'defined'
    > operator instead of comparing to "". Or write it all in a single
    > line:
    >
    > $Year = param("Year") || $local_year;
    >
    > This deals with the case that param("Year") is not defined as well
    > as that it's an empty string.


    But be careful; this will set $Year to $local_year if param("Year")
    is either undef, the empty string, or 0. That shouldn't cause any
    visible problems for a year number (unless you're dealing with very
    old events or using 2-digit years), but it could bite you for values
    that could legitimately be 0.

    Perl 6 has a "defined-or" operator, spelled "//", that acts like
    "||" except that it tests its first operand for definedness rather
    than truth. So in Perl6 you could write:

    $Year = param("Year") // $local_year;

    Perl 5.10 has this as well, but I think it was introduced relatively
    recently (5.8.8 doesn't have it), so there are still plenty of Perl
    installations out there that don't support it.

    If you can't assume that the // operator will be available, you
    can do something like this:

    $Year = param("Year");
    $Year = $local_year if not defined $Year;

    Or like this:

    $Year = defined param("Year") ? param("Year") : $local_year;

    But note that the latter calls param("Year") twice.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Aug 20, 2009
    #11
  12. Keith Thompson <> wrote:
    > (Jens Thoms Toerring) writes:
    > Or write it all in a single line:
    > >
    > > $Year = param("Year") || $local_year;
    > >
    > > This deals with the case that param("Year") is not defined as well
    > > as that it's an empty string.


    > But be careful; this will set $Year to $local_year if param("Year")
    > is either undef, the empty string, or 0. That shouldn't cause any
    > visible problems for a year number (unless you're dealing with very
    > old events or using 2-digit years), but it could bite you for values
    > that could legitimately be 0.


    Thanks, good point! Hadn't considered it to be a possiblilty.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Aug 21, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sukhbir Dhillon
    Replies:
    1
    Views:
    6,246
    Joe Smith
    Apr 5, 2004
  2. BradK
    Replies:
    0
    Views:
    3,047
    BradK
    Apr 10, 2007
  3. geek
    Replies:
    21
    Views:
    210
    Sherm Pendley
    Mar 24, 2005
  4. geek
    Replies:
    2
    Views:
    148
    Brian McCauley
    Mar 23, 2005
  5. geek
    Replies:
    2
    Views:
    105
Loading...

Share This Page