[31777] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3040 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 22 14:09:22 2010

Date: Thu, 22 Jul 2010 11:09:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 22 Jul 2010     Volume: 11 Number: 3040

Today's topics:
    Re: Need help/advice to improve script <rvtol+usenet@xs4all.nl>
    Re: Need help/advice to improve script <jurgenex@hotmail.com>
    Re: Need help/advice to improve script <sopan.shewale@gmail.com>
    Re: Need help/advice to improve script <sopan.shewale@gmail.com>
    Re: Need help/advice to improve script <sopan.shewale@gmail.com>
    Re: Need help/advice to improve script <m@rtij.nl.invlalid>
    Re: Need help/advice to improve script <sopan.shewale@gmail.com>
        Speed of reading some MB of data using qx(...) <w.c.humann@arcor.de>
    Re: Speed of reading some MB of data using qx(...) <uri@StemSystems.com>
        Use of uninitialised value, how to avoid? <justin.1007@purestblue.com>
    Re: Use of uninitialised value, how to avoid? (Jens Thoms Toerring)
    Re: Use of uninitialised value, how to avoid? sln@netherlands.com
    Re: Use of uninitialised value, how to avoid? <NoSpamPleaseButThisIsValid3@gmx.net>
    Re: Use of uninitialised value, how to avoid? sln@netherlands.com
    Re: Use of uninitialised value, how to avoid? <justin.1007@purestblue.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Thu, 22 Jul 2010 10:10:14 +0200
From: "Dr.Ruud" <rvtol+usenet@xs4all.nl>
Subject: Re: Need help/advice to improve script
Message-Id: <4c47fce6$0$22935$e4fe514c@news.xs4all.nl>

sopan.shewale@gmail.com wrote:
> Appreciate every one for responding to this question.
> 
> The solution with split is also good idea. I am sticking to following
> solution.
> 
> ------------------------
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my $data = {};
> 
> for (<DATA>) {

Bad, as you have been told several times.


>     chomp;
>     my $line = $_;

That $line should be set in the while-line starting the loop.


>     if ( defined $line ) {
>         if ( $line =~ /^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?::([^:]*)
> (?::([^:]*)(?::(.*))?)?)?)?$/ )

Butt ugly.


>  {
> 
>             $data->{$1}->{pass} = $2;
>                #   ^^ $1 will never be '' or undef...
>                #   in that case there is issue with password line
>             $data->{$1}->{emails} = $3 || '';
>             $data->{$1}->{flag} =
>               ( ( defined $4 ) && ( $4 == 0 ) ) ? 0 : ( $4 || '' );
>                #                we are assumming $4 as 0 or 1
>             $data->{$1}->{pass_change} = $5 || '';
>             $data->{$1}->{flag_change} = $6 || '';

Silly '||' usage. Copy/paste coding style.


>         }
>     }
> 
> }
> 
> use Data::Dumper;

Put that line in the top, because that is where it is.



> print Data::Dumper->Dump( [$data] );
> 
> __DATA__
> AllPresent:hjliEO35kCgwI:all@example.com:1:23232:24324
> LastMissing:CyL92g3OKi.jM:lastmissing@example.com:1:2323
> FlagZero:CyL92g3OKi.jM:flagzero@example.com:0:23232
> OnlyFlag:ZuqpLZ7AxHBvw:onlyflag@example.com:0
> RestMissing:ZuqpLZ7AxHBvw:missing@example.com
> -----------------------------------------------------------
> 
> I got the help on regex from Peter Thoeny (http://twiki.org)
> The part of this code is checked-in into TWiki repository
> thanks every one for helping

You are publishing bad quality code. I expect you
to clearly mark it as such, so visitors know what they are getting.


#!/usr/bin/perl -wl
use strict;

use Data::Dumper;

my @cols = qw/ pass emails flag pass_change flag_change /;
my %data;

while ( <DATA> ) {
     next if /^\s*(?:$|#)/;  # skip empty and comment lines
     chomp;
     my @line = split /:/;
     my $key = shift @line or die "Bad data at line $.\n";
     @{ $data{ $key } }{ @cols } = @line;
     $data{ $key }{ flag } ||= 0;  # or use //=
}

print Dumper \%data;


__DATA__
AllPresent:hjliEO35kCgwI:all@example.com:1:23232:24324
LastMissing:CyL92g3OKi.jM:lastmissing@example.com:1:2323
FlagZero:CyL92g3OKi.jM:flagzero@example.com:0:23232
OnlyFlag:ZuqpLZ7AxHBvw:onlyflag@example.com:0
RestMissing:ZuqpLZ7AxHBvw:missing@example.com

0 :
#EOF


-- 
Ruud


------------------------------

Date: Thu, 22 Jul 2010 01:20:32 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Need help/advice to improve script
Message-Id: <hhvf46pvh5jk4jh853rhpafmbjn78msuac@4ax.com>

"sopan.shewale@gmail.com" <sopan.shewale@gmail.com> wrote:
>Appreciate every one for responding to this question.
>
>The solution with split is also good idea. I am sticking to following
>solution.
[...]
>for (<DATA>) {

Why are you sticking with a for(<>) loop? It doesn't have any advantage
over a while() loop or slurping the file into a variable.

>    chomp;
>    my $line = $_;

Why are you sticking to the separate assignment?

>    if ( defined $line ) {

Why are you sticking to the useless defined() test?

>        if ( $line =~ /^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?::([^:]*)
>(?::([^:]*)(?::(.*))?)?)?)?$/ )

Why are you sticking to the complex RE when a split() is so much easier?

jue


------------------------------

Date: Thu, 22 Jul 2010 02:03:14 -0700 (PDT)
From: "sopan.shewale@gmail.com" <sopan.shewale@gmail.com>
Subject: Re: Need help/advice to improve script
Message-Id: <ca8834af-7c3b-443b-85f4-223f28d2600b@i18g2000pro.googlegroups.com>


On Jul 22, 1:10=A0pm, "Dr.Ruud" <rvtol+use...@xs4all.nl> wrote:
> sopan.shew...@gmail.com wrote:
> > Appreciate every one for responding to this question.
>
> > The solution with split is also good idea. I am sticking to following
> > solution.
>
> > ------------------------
>
> > #!/usr/bin/perl
> > use strict;
> > use warnings;
>
> > my $data =3D {};
>
> > for (<DATA>) {
>
> Bad, as you have been told several times.
>

[sopan] - yup, bad. correcting it.
          The one posting here in groups is just logic. In actual code
at TWiki, its while loop.
          The data comes from the file from filesystem, opened with
standard functions/else thrown error with modules like Error::Simple.


> > =A0 =A0 chomp;
> > =A0 =A0 my $line =3D $_;
>
> That $line should be set in the while-line starting the loop.
>
[sopan] - yes, agree.

> > =A0 =A0 if ( defined $line ) {
> > =A0 =A0 =A0 =A0 if ( $line =3D~ /^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?:=
:([^:]*)
> > (?::([^:]*)(?::(.*))?)?)?)?$/ )
>
> Butt ugly.
>

[sopan] - yes, its ugly. decided to move to split solution (the
original code of my package had similar lines for three field password
file since long time. But no complains).

> > =A0{
>
> > =A0 =A0 =A0 =A0 =A0 =A0 $data->{$1}->{pass} =3D $2;
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =A0 ^^ $1 will never be '' or undef...
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =A0 in that case there is issue with p=
assword line
> > =A0 =A0 =A0 =A0 =A0 =A0 $data->{$1}->{emails} =3D $3 || '';
> > =A0 =A0 =A0 =A0 =A0 =A0 $data->{$1}->{flag} =3D
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 ( ( defined $4 ) && ( $4 =3D=3D 0 ) ) ? 0 :=
 ( $4 || '' );
> > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0we are =
assumming $4 as 0 or 1
> > =A0 =A0 =A0 =A0 =A0 =A0 $data->{$1}->{pass_change} =3D $5 || '';
> > =A0 =A0 =A0 =A0 =A0 =A0 $data->{$1}->{flag_change} =3D $6 || '';
>
> Silly '||' usage. Copy/paste coding style.
>
[sopan] - yes,it was copy+paste :(. Moving to better style. Thank you,

> > =A0 =A0 =A0 =A0 }
> > =A0 =A0 }
>
> > }
>
> > use Data::Dumper;
>
> Put that line in the top, because that is where it is.
>
[sopan] - it was just for testing purposes. These lines are not
present in code.But yes, in forums also, whatever i pasted, i should
have pasted better and agree to have this line at the upper part of
code. my mistake.

> > print Data::Dumper->Dump( [$data] );
>
> > __DATA__
> > AllPresent:hjliEO35kCgwI:a...@example.com:1:23232:24324
> > LastMissing:CyL92g3OKi.jM:lastmiss...@example.com:1:2323
> > FlagZero:CyL92g3OKi.jM:flagz...@example.com:0:23232
> > OnlyFlag:ZuqpLZ7AxHBvw:onlyf...@example.com:0
> > RestMissing:ZuqpLZ7AxHBvw:miss...@example.com
> > -----------------------------------------------------------
>
> > I got the help on regex from Peter Thoeny (http://twiki.org)
> > The part of this code is checked-in into TWiki repository
> > thanks every one for helping
>
> You are publishing bad quality code. I expect you
> to clearly mark it as such, so visitors know what they are getting.
>
> #!/usr/bin/perl -wl
> use strict;
>
> use Data::Dumper;
>
> my @cols =3D qw/ pass emails flag pass_change flag_change /;
> my %data;
>
> while ( <DATA> ) {
> =A0 =A0 =A0next if /^\s*(?:$|#)/; =A0# skip empty and comment lines
> =A0 =A0 =A0chomp;
> =A0 =A0 =A0my @line =3D split /:/;
> =A0 =A0 =A0my $key =3D shift @line or die "Bad data at line $.\n";
> =A0 =A0 =A0@{ $data{ $key } }{ @cols } =3D @line;
> =A0 =A0 =A0$data{ $key }{ flag } ||=3D 0; =A0# or use //=3D
>
> }
>
> print Dumper \%data;
>
> __DATA__
> AllPresent:hjliEO35kCgwI:a...@example.com:1:23232:24324
> LastMissing:CyL92g3OKi.jM:lastmiss...@example.com:1:2323
> FlagZero:CyL92g3OKi.jM:flagz...@example.com:0:23232
> OnlyFlag:ZuqpLZ7AxHBvw:onlyf...@example.com:0
> RestMissing:ZuqpLZ7AxHBvw:miss...@example.com
>
> 0 :
> #EOF
>
> --
> Ruud

[sopan] - thank you so much Ruud. this was the best version.



------------------------------

Date: Thu, 22 Jul 2010 02:07:56 -0700 (PDT)
From: "sopan.shewale@gmail.com" <sopan.shewale@gmail.com>
Subject: Re: Need help/advice to improve script
Message-Id: <67e9a427-5132-441f-85ac-90713b9c2798@w15g2000pro.googlegroups.com>

On Jul 22, 1:20=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> "sopan.shew...@gmail.com" <sopan.shew...@gmail.com> wrote:
> >Appreciate every one for responding to this question.
>
> >The solution with split is also good idea. I am sticking to following
> >solution.
> [...]
> >for (<DATA>) {
>
> Why are you sticking with a for(<>) loop? It doesn't have any advantage
> over a while() loop or slurping the file into a variable.
>

[sopan] - in actual code which goes in package has while. The posted
here was just part of code. Its while, agree with you to have while
instead of for. thank you,

> > =A0 =A0chomp;
> > =A0 =A0my $line =3D $_;
>
> Why are you sticking to the separate assignment?
>

[sopan] -yes, makes sense to have my $line =3D $_; chomp $line;

> > =A0 =A0if ( defined $line )
>
> Why are you sticking to the useless defined() test?
>

[sopan] agree, not required to have "defined $line"

> > =A0 =A0 =A0 =A0if ( $line =3D~ /^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?::=
([^:]*)
> >(?::([^:]*)(?::(.*))?)?)?)?$/ )
>
> Why are you sticking to the complex RE when a split() is so much easier?
>
[sopan] moving to split based stuff. agree its bad looking
expression.



> jue


[sopan] Thanks  jue


------------------------------

Date: Thu, 22 Jul 2010 02:12:46 -0700 (PDT)
From: "sopan.shewale@gmail.com" <sopan.shewale@gmail.com>
Subject: Re: Need help/advice to improve script
Message-Id: <5848fbe3-f466-4ad4-882f-727fe285a2a6@z34g2000pro.googlegroups.com>

On Jul 22, 10:43=A0am, "Uri Guttman" <u...@StemSystems.com> wrote:
> >>>>> "ssc" =3D=3D sopan shewale@gmail com <sopan.shew...@gmail.com> writ=
es:
>
> =A0 ssc> Appreciate every one for responding to this question.
> =A0 ssc> The solution with split is also good idea. I am sticking to foll=
owing
> =A0 ssc> solution.
>
> why would you stick with this complex solution over the much simpler and
> faster split?
>

[sopan] - moving with split, thank you :)

> =A0 ssc> =A0 =A0 if ( defined $line ) {
> =A0 ssc> =A0 =A0 =A0 =A0 if ( $line =3D~ /^([^:]*):([^:]*):([^:]*)(?::([^=
:]*)(?::([^:]*)
> =A0 ssc> (?::([^:]*)(?::(.*))?)?)?)?$/ )
>
> ok, here is a test. explain what that line is doing in 30 words or
> less. if you can't explain a single line of code like that, it is overly
> complex code. note that the ? and : chars both play two different roles
> (and many dupes of them). that is NOT a good solution. just my strong
> opinion.
>


[sopan] - thanks uri for strong advice. [1] difficult to explain [2]
30 or less words-just impossible. Actually similar code has been with
the package for long times - but no complain. i will move to split
which is clean solution.


> uri
>
> --
> Uri Guttman =A0------ =A0u...@stemsystems.com =A0-------- =A0http://www.s=
ysarch.com--
> ----- =A0Perl Code Review , Architecture, Development, Training, Support =
------
> --------- =A0Gourmet Hot Cocoa Mix =A0---- =A0http://bestfriendscocoa.com=
---------



------------------------------

Date: Thu, 22 Jul 2010 14:34:03 +0200
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: Need help/advice to improve script
Message-Id: <reqlh7-i09.ln1@news.rtij.nl>

On Thu, 22 Jul 2010 02:07:56 -0700, sopan.shewale@gmail.com wrote:

> On Jul 22, 1:20 pm, Jürgen Exner <jurge...@hotmail.com> wrote:
>> "sopan.shew...@gmail.com" <sopan.shew...@gmail.com> wrote:
>> >Appreciate every one for responding to this question.
>>
>> >The solution with split is also good idea. I am sticking to following
>> >solution.
>> [...]
>> >for (<DATA>) {
>>
>> Why are you sticking with a for(<>) loop? It doesn't have any advantage
>> over a while() loop or slurping the file into a variable.
>>
>>
> [sopan] - in actual code which goes in package has while. The posted
> here was just part of code. Its while, agree with you to have while
> instead of for. thank you,
> 
>> >    chomp;
>> >    my $line = $_;
>>
>> Why are you sticking to the separate assignment?
>>
>>
> [sopan] -yes, makes sense to have my $line = $_; chomp $line;
> 
>> >    if ( defined $line )
>>
>> Why are you sticking to the useless defined() test?
>>
>>
> [sopan] agree, not required to have "defined $line"
> 
>> >        if ( $line =~
>> >        /^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?::([^:]*)
>> >(?::([^:]*)(?::(.*))?)?)?)?$/ )
>>
>> Why are you sticking to the complex RE when a split() is so much
>> easier?
>>
> [sopan] moving to split based stuff. agree its bad looking expression.
> 
> 

Again, it can be as easy as this:

#!/usr/bin/perl
use strict;
use warnings;

my %data;

while (my $line = <DATA>) {

    # chop of newline
    chomp $line;

    # split into fields and stuff into hash, using a hash slice
    @{$data{$user}}{qw/pass email flag passchange flagchange/} =
	split /:/, $line, 6;
}

OK, you get undef for every field not defined, but that usually is what 
you want.

M4


------------------------------

Date: Thu, 22 Jul 2010 06:35:34 -0700 (PDT)
From: "sopan.shewale@gmail.com" <sopan.shewale@gmail.com>
Subject: Re: Need help/advice to improve script
Message-Id: <b8042dae-a901-4583-9b8c-182c606f0857@n8g2000prh.googlegroups.com>

On Jul 22, 5:34=A0pm, Martijn Lievaart <m...@rtij.nl.invlalid> wrote:
> On Thu, 22 Jul 2010 02:07:56 -0700, sopan.shew...@gmail.com wrote:
> > On Jul 22, 1:20=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> >> "sopan.shew...@gmail.com" <sopan.shew...@gmail.com> wrote:
> >> >Appreciate every one for responding to this question.
>
> >> >The solution with split is also good idea. I am sticking to following
> >> >solution.
> >> [...]
> >> >for (<DATA>) {
>
> >> Why are you sticking with a for(<>) loop? It doesn't have any advantag=
e
> >> over a while() loop or slurping the file into a variable.
>
> > [sopan] - in actual code which goes in package has while. The posted
> > here was just part of code. Its while, agree with you to have while
> > instead of for. thank you,
>
> >> > =A0 =A0chomp;
> >> > =A0 =A0my $line =3D $_;
>
> >> Why are you sticking to the separate assignment?
>
> > [sopan] -yes, makes sense to have my $line =3D $_; chomp $line;
>
> >> > =A0 =A0if ( defined $line )
>
> >> Why are you sticking to the useless defined() test?
>
> > [sopan] agree, not required to have "defined $line"
>
> >> > =A0 =A0 =A0 =A0if ( $line =3D~
> >> > =A0 =A0 =A0 =A0/^([^:]*):([^:]*):([^:]*)(?::([^:]*)(?::([^:]*)
> >> >(?::([^:]*)(?::(.*))?)?)?)?$/ )
>
> >> Why are you sticking to the complex RE when a split() is so much
> >> easier?
>
> > [sopan] moving to split based stuff. agree its bad looking expression.
>
> Again, it can be as easy as this:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my %data;
>
> while (my $line =3D <DATA>) {
>
> =A0 =A0 # chop of newline
> =A0 =A0 chomp $line;
>
> =A0 =A0 # split into fields and stuff into hash, using a hash slice
> =A0 =A0 @{$data{$user}}{qw/pass email flag passchange flagchange/} =3D
> =A0 =A0 =A0 =A0 split /:/, $line, 6;
>
> }
>
> OK, you get undef for every field not defined, but that usually is what
> you want.
>
> M4

This definitely works - but wanted to take care of flag to have 0 if
its not defined. Not sure where and how many places %data is used in
the whole package.

Thank you


------------------------------

Date: Thu, 22 Jul 2010 06:02:32 -0700 (PDT)
From: Wolfram Humann <w.c.humann@arcor.de>
Subject: Speed of reading some MB of data using qx(...)
Message-Id: <f2c4d842-75af-4543-9f0d-3a1502622d70@g35g2000yqa.googlegroups.com>

I have a program that processes PDF files by converting them to
Postscript, read the ps and do something with it. I use pdftops (from
xpdf) for the pdf->ps conversion and retrieve the result like this:

$ps_text = qx( pdftops $infile - );

On win32 using strawberry perl (tried 5.10 and 5.12) this takes much
more time than I expected so I did a test and first converted the PDF
to Postscript, then read the Postscript (about 12 MB) like this (cat
on win32 provided by cygwin):

perl -E" $t = qx(cat psfile.ps); say length $t "

This takes about 16 seconds on win32 but only <1 seconds on Linux. I
was afraid that this might be a 'binmode' problem so I also tried
this:

perl -E" open $in,'cat psfile.ps |'; binmode $in; local $/; $t=<$in>;
say length $t "

But the effect is the same: fast on linux, slow on win32. Besides
bashing win32 :-) and ideas for reason and (possibly) cure?

Wolfram


------------------------------

Date: Thu, 22 Jul 2010 12:39:42 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Speed of reading some MB of data using qx(...)
Message-Id: <87zkxjii7l.fsf@quad.sysarch.com>

>>>>> "WH" == Wolfram Humann <w.c.humann@arcor.de> writes:

  WH> perl -E" $t = qx(cat psfile.ps); say length $t "

  WH> This takes about 16 seconds on win32 but only <1 seconds on Linux. I
  WH> was afraid that this might be a 'binmode' problem so I also tried
  WH> this:

  WH> perl -E" open $in,'cat psfile.ps |'; binmode $in; local $/; $t=<$in>;
  WH> say length $t "

  WH> But the effect is the same: fast on linux, slow on win32. Besides
  WH> bashing win32 :-) and ideas for reason and (possibly) cure?

you also bashed File::Slurp in a bug report you sent me. obviously it is
a winblows issue. one possibility is that winblows does poor process
context switching and piping between processes causes lots of
that. there may be other reasons but i stay away from redmond.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Thu, 22 Jul 2010 14:34:08 +0100
From: Justin C <justin.1007@purestblue.com>
Subject: Use of uninitialised value, how to avoid?
Message-Id: <gvtlh7-f19.ln1@zem.masonsmusic.co.uk>

I'm trying to avoid warnings in my Apache logs. Here's the code that's
causing the problem:

unless ($methods->{$param{method}}) {
    $param{method} = "default";
}

If the web-page in question is called without any parameters then
$param{method} is undef, in which case I want $param->{method} =
"default". 

When the page is called with parameters all is OK. 

What is a better way of doing the above to avoid "Use of uninitialized
value $param{"method"}" ?

   Justin.

-- 
Justin C, by the sea.


------------------------------

Date: 22 Jul 2010 14:20:55 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Use of uninitialised value, how to avoid?
Message-Id: <8ar2e7Fl65U2@mid.uni-berlin.de>

Justin C <justin.1007@purestblue.com> wrote:
> I'm trying to avoid warnings in my Apache logs. Here's the code that's
> causing the problem:

> unless ($methods->{$param{method}}) {
>     $param{method} = "default";
> }

> If the web-page in question is called without any parameters then
> $param{method} is undef, in which case I want $param->{method} =
> "default". 

> When the page is called with parameters all is OK. 

> What is a better way of doing the above to avoid "Use of uninitialized
> value $param{"method"}" ?

In the 'unless' condition you use

$methods->{$param{method}}

so if $param{method} is undefined you may be asking for a hash
element with an undefined key. Shouldn't that line actually be

unless ($param{method}) {
    $param{method} = "default";
}

or just

$param{method} = "default" unless $param{method};

if you want to check if $param{method} is defined and assign a
default value otherwise?
                             Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de


------------------------------

Date: Thu, 22 Jul 2010 08:03:25 -0700
From: sln@netherlands.com
Subject: Re: Use of uninitialised value, how to avoid?
Message-Id: <0ang46dej86bhcmbg2vr28dpkbov98b9rc@4ax.com>

On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <justin.1007@purestblue.com> wrote:

>I'm trying to avoid warnings in my Apache logs. Here's the code that's
>causing the problem:
>
>unless ($methods->{$param{method}}) {
>    $param{method} = "default";
>}
>
>If the web-page in question is called without any parameters then
>$param{method} is undef, in which case I want $param->{method} =
>"default". 
>
>When the page is called with parameters all is OK. 
>
>What is a better way of doing the above to avoid "Use of uninitialized
>value $param{"method"}" ?
>
>   Justin.

Depends on what you want:

# (undef,'') fail ; (0) pass
     unless (defined $param{method} and length $param{method}) {
         $param{method} = "default";
     }
# (undef,0,'') fail  
     $param{method} ||= "default";

# (undef) fail ; (0,'') pass
     $param{method} //= "default"; 

-sln


------------------------------

Date: Thu, 22 Jul 2010 17:04:39 +0200
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: Use of uninitialised value, how to avoid?
Message-Id: <4c485e08$0$7653$9b4e6d93@newsspool1.arcor-online.net>

On 22.07.2010 16:20, Jens Thoms Toerring wrote:
> Justin C <justin.1007@purestblue.com> wrote:
>> When the page is called with parameters all is OK. 
> 
>> What is a better way of doing the above to avoid "Use of uninitialized
>> value $param{"method"}" ?
> 
> In the 'unless' condition you use
> 
> $methods->{$param{method}}
> 
> so if $param{method} is undefined you may be asking for a hash
> element with an undefined key. Shouldn't that line actually be
> 
> unless ($param{method}) {
>     $param{method} = "default";
> }

Probably this is not a solution because this removes the check whether
there exists a key in %$methods. If you omit this check, the user could
specify any method, also non existing ones.

To get rid of the warning, one can use for example:

unless ($param{'method'} && $methods->{$param{'method'}}) { ... }

Something like

$param{method} ||= "default";
die "invalid method\n" unless $methods->{$param{method}};

might be better than overwriting wrong methods with the default. But
this of course depends.

Wolf



------------------------------

Date: Thu, 22 Jul 2010 08:33:28 -0700
From: sln@netherlands.com
Subject: Re: Use of uninitialised value, how to avoid?
Message-Id: <83pg46hsqatlbvirmtobcvhdqmhbm7hd5s@4ax.com>

On Thu, 22 Jul 2010 08:03:25 -0700, sln@netherlands.com wrote:

>On Thu, 22 Jul 2010 14:34:08 +0100, Justin C <justin.1007@purestblue.com> wrote:
>
>>I'm trying to avoid warnings in my Apache logs. Here's the code that's
>>causing the problem:
>>
>>unless ($methods->{$param{method}}) {
>>    $param{method} = "default";
>>}
>>
>>If the web-page in question is called without any parameters then
>>$param{method} is undef, in which case I want $param->{method} =
>>"default". 
>>
>>When the page is called with parameters all is OK. 
>>
>>What is a better way of doing the above to avoid "Use of uninitialized
>>value $param{"method"}" ?
>>
>>   Justin.
>
>Depends on what you want:
>
># (undef,'') fail ; (0) pass
>     unless (defined $param{method} and length $param{method}) {
>         $param{method} = "default";
>     }
># (undef,0,'') fail  
>     $param{method} ||= "default";
>
># (undef) fail ; (0,'') pass
>     $param{method} //= "default"; 
>

So perhaps:

use strict;
use warnings;

my %param;
my $methods = {
      default => sub {print "Default handler method ..\n"},
   } ;
$param{method} //= "default" ;
exists $methods->{ $param{method} }
  and  $methods->{ $param{method} }()
  or   die "Cannot find $param{method}() method" ;

-sln



------------------------------

Date: Thu, 22 Jul 2010 16:37:16 +0100
From: Justin C <justin.1007@purestblue.com>
Subject: Re: Use of uninitialised value, how to avoid?
Message-Id: <c65mh7-2rc.ln1@zem.masonsmusic.co.uk>

On 2010-07-22, Justin C <justin.1007@purestblue.com> wrote:
> I'm trying to avoid warnings in my Apache logs. Here's the code that's
> causing the problem:
>
> unless ($methods->{$param{method}}) {
>     $param{method} = "default";
> }
>
> If the web-page in question is called without any parameters then
> $param{method} is undef, in which case I want $param->{method} =
> "default". 
>
> When the page is called with parameters all is OK. 
>
> What is a better way of doing the above to avoid "Use of uninitialized
> value $param{"method"}" ?

Thanks to Jens and Wolf. Between you both you spotted a flaw in my code
and provided the work-around. 

And sln, I think the last two of your suggestions were 5.10 solutions,
these are operators I've yet to explore, though I can see, from your
explanation, what each is doing. I'll investigate them further. 

   Justin.

-- 
Justin C, by the sea.


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3040
***************************************


home help back first fref pref prev next nref lref last post