[32169] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3434 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jul 6 14:09:21 2011

Date: Wed, 6 Jul 2011 11:09:06 -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           Wed, 6 Jul 2011     Volume: 11 Number: 3434

Today's topics:
    Re: emacs lisp text processing example (html5 figure/fi <StefanMandl@web.de>
        Meggage level/ Logging <401anil@gmail.com>
    Re: Meggage level/ Logging <bugbear@trim_papermule.co.uk_trim>
    Re: Meggage level/ Logging <josef.moellers@ts.fujitsu.com>
    Re: sort scientific notation value after alphabet <cartercc@gmail.com>
    Re: sort scientific notation value after alphabet <uri@StemSystems.com>
    Re: sort scientific notation value after alphabet <willem@turtle.stack.nl>
    Re: sort scientific notation value after alphabet <ralph@happydays.com>
    Re: sort scientific notation value after alphabet <uri@StemSystems.com>
        style?: how to convert to boolean: false,no -> 0, yes,  <georgeryoung@gmail.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <uri@StemSystems.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <tadmc@seesig.invalid>
    Re: style?: how to convert to boolean: false,no -> 0, y <mvdwege@mail.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <rweikusat@mssgmbh.com>
    Re: style?: how to convert to boolean: false,no -> 0, y (Randal L. Schwartz)
    Re: style?: how to convert to boolean: false,no -> 0, y <georgeryoung@gmail.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <uri@StemSystems.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <uri@StemSystems.com>
    Re: style?: how to convert to boolean: false,no -> 0, y <peter.tuente@materna.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 5 Jul 2011 14:30:13 -0700 (PDT)
From: "S.Mandl" <StefanMandl@web.de>
Subject: Re: emacs lisp text processing example (html5 figure/figcaption)
Message-Id: <c294ca65-b1d1-41ad-8d5a-d3cc4287a9ec@o13g2000yqj.googlegroups.com>

> haven't used XSLT, and don't know if there's one in emacs...
>
> it'd be nice if someone actually give a example...
>

Hi Xah, actually I have to correct myself. HTML is not XML. If it
were, you
could use a stylesheet like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="p[@class='cpt']">
  <figcaption>
    <xsl:value-of select="."/>
  </figcaption>
</xsl:template>

<xsl:template match="div[@class='img']">
  <figure>
    <xsl:apply-templates select="@*|node()"/>
  </figure>
</xsl:template>

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>


</xsl:stylesheet>

which applied to this document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<doc>
<h1>Just having fun</h1>with all the
<div class="img">
  <img src="cat1.jpg" alt="my cat" width="200" height="200"/>
  <img src="cat2.jpg" alt="my cat" width="200" height="200"/>
  <p class="cpt">my 2 cats</p>
</div>
cats here:
<h1>Just fooling around</h1>
<div class="img">
  <img src="jamie_cat.jpg" alt="jamie's cat" width="167" height="106"/
>
  <p class="cpt">jamie's cat! Her blog is <a href="http://example.com/
jamie/">http://example.com/jamie/</a></p>
</div>
</doc>

would yield:

<?xml version="1.0"?>
<doc>
<h1>Just having fun</h1>with all the
<figure class="img">
  <img src="cat1.jpg" alt="my cat" width="200" height="200"/>
  <img src="cat2.jpg" alt="my cat" width="200" height="200"/>
  <figcaption>my 2 cats</figcaption>
</figure>
cats here:
<h1>Just fooling around</h1>
<figure class="img">
  <img src="jamie_cat.jpg" alt="jamie's cat" width="167" height="106"/
>
  <figcaption>jamie's cat! Her blog is http://example.com/jamie/</figcaption>
</figure>
</doc>

But well, as you don't have XML as input ... there really was no point
to my remark.

Best,
Stefan


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

Date: Wed, 6 Jul 2011 01:07:47 -0700 (PDT)
From: Anil A Kumar <401anil@gmail.com>
Subject: Meggage level/ Logging
Message-Id: <e0b6b029-5e63-4aeb-9390-b865c2824be9@h12g2000pro.googlegroups.com>

Hi All,

I am new to PERL. I have worked on TCL. There we have 'Package Log' to
differentiate message levels.

I can give
----------------------------------------------
log::Puts info "Level INFO"
log::Puts debug "Level DEBUG"
-----------------------------------------------

Is there anything similar to this in PERL?

Thanks in advance!

Regards,
Anil A Kumar


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

Date: Wed, 06 Jul 2011 09:34:34 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: Meggage level/ Logging
Message-Id: <IN6dnQEr2K-Hg4nTnZ2dnUVZ7sudnZ2d@brightview.co.uk>

Anil A Kumar wrote:
> Hi All,
>
> I am new to PERL. I have worked on TCL. There we have 'Package Log' to
> differentiate message levels.
>
> I can give
> ----------------------------------------------
> log::Puts info "Level INFO"
> log::Puts debug "Level DEBUG"
> -----------------------------------------------
>
> Is there anything similar to this in PERL?

I've had good results using Log4J in Java,
and the underlying model is available in Perl

http://mschilli.github.com/log4perl/

   BugBear


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

Date: Wed, 06 Jul 2011 12:52:29 +0200
From: Josef Moellers <josef.moellers@ts.fujitsu.com>
Subject: Re: Meggage level/ Logging
Message-Id: <iv1epd$pto$1@nntp.fujitsu-siemens.com>

Am 6.7.2011 schrub Anil A Kumar:

> Hi All,
> 
> I am new to PERL. I have worked on TCL. There we have 'Package Log' to
> differentiate message levels.
> 
> I can give
> ----------------------------------------------
> log::Puts info "Level INFO"
> log::Puts debug "Level DEBUG"
> -----------------------------------------------
> 
> Is there anything similar to this in PERL?

The Sys::Syslog package on CPAN.

Josef
-- 
These are my personal views and not those of Fujitsu Technology Solutions!
Josef Möllers (Pinguinpfleger bei FTS)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html


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

Date: Tue, 5 Jul 2011 14:06:42 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <08db7360-bac9-429c-8344-fdf10783d96a@b21g2000yqc.googlegroups.com>

On Jul 5, 3:11=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> why declare vars you never use? make them undef or slice the results of t=
he split.

Force of habit. I use this idiom almost every day, and can do it on
autopilot. Many times, I can take a SQL select statement or a header
and using vi can quickly and easily turn a number of items into a
couple of lines of code that deconstruct lines in a data file and
(using a hash slice) stuff the entire line into a hash element. It's
much easier to do this than to think about what items I'll use, or not
use.

> why sprintf there? you don't format the strings so that would just be a s=
imple "$ident$cv".

Again, force of habit. Many times I will interpolate literal
characters into an identifier, and using sprintf() keeps me from
having to use braces to disambiguate variables.



> and this is all done for you and better in Sort::Maker.

Not familiar with Sort::Maker, but will definitely take a look at it.

CC.


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

Date: Tue, 05 Jul 2011 17:59:12 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <87y60cv173.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> On Jul 5, 3:11 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
  >> why declare vars you never use? make them undef or slice the results of the split.

  c> Force of habit. I use this idiom almost every day, and can do it on
  c> autopilot. Many times, I can take a SQL select statement or a
  c> header and using vi can quickly and easily turn a number of items
  c> into a couple of lines of code that deconstruct lines in a data
  c> file and (using a hash slice) stuff the entire line into a hash
  c> element. It's much easier to do this than to think about what items
  c> I'll use, or not use.

but it is still misleading. it is better to declare only vars you will
be using later.

  >> why sprintf there? you don't format the strings so that would just be a simple "$ident$cv".

  c> Again, force of habit. Many times I will interpolate literal
  c> characters into an identifier, and using sprintf() keeps me from
  c> having to use braces to disambiguate variables.

what ambiguity? "$ident$cv" has no braces. and if you do need to do
this:
	"${foo}_bar"

that is still simpler than an sprintf and a lot faster. use sprintf when
you need actual formatting of values. there are a few other reasons to
use it besides formatting but i won't get into them now.

  c> Not familiar with Sort::Maker, but will definitely take a look at it.

it can show you the generated code for all the sorts so you can use it
without the module if you cut/paste that code.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------


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

Date: Wed, 6 Jul 2011 12:17:10 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <slrnj18ki6.17sf.willem@turtle.stack.nl>

Uri Guttman wrote:
) but it is still misleading. it is better to declare only vars you will
) be using later.

Why?

SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Wed, 06 Jul 2011 10:14:24 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <7b95a$4e146dc7$ce534406$26463@news.eurofeeds.com>

Gutts is wrong. Declaring these vars can actually be a good thing.
Indeed, in the example given, declaring the unused vars provides
valuable information to the maintenance programmer about what
the other fields contain.
At the expense of a trivial amount of memory and computational overhead
this code becomes much more maintainable.

On 7/6/2011 8:17 AM, Willem wrote:
> Uri Guttman wrote:
> ) but it is still misleading. it is better to declare only vars you will
> ) be using later.
>
> Why?
>
> SaSW, Willem



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

Date: Wed, 06 Jul 2011 13:16:34 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <87d3hnl47h.fsf@quad.sysarch.com>

>>>>> "W" == Willem  <willem@turtle.stack.nl> writes:

  W> Uri Guttman wrote:
  W> ) but it is still misleading. it is better to declare only vars you will
  W> ) be using later.

  W> Why?

so you don't mislead the reader. showing that you know values will be
discarded says you understand the data coming in. declaring all the vars
and not using them says you are scared of throwing away data even though
you are. you are writing code to be read, not just for the computer to
run.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------


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

Date: Tue, 5 Jul 2011 18:39:35 -0700 (PDT)
From: gry <georgeryoung@gmail.com>
Subject: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <c50f3119-69df-42a6-b653-5d1717d765a9@q15g2000yqk.googlegroups.com>

I'm reading a config file that I want to be forgiving/robust, so
'false','no','never' should parse to 0 and 'true','yes','always' to
1.   I have the following, which works, but seems cumbersome and not
idiomatic perl:
    sub get_bool {
	my $val = $_[0];
	my $bool;
	if ($val =~ m/true|yes|always|1/i) {
	    $bool = 1; }
	elsif ($val =~ m/false|no|never|0/i) {
	    $bool = 0; }
	else{
	    $bool = 0; }
	return $bool;
    }
    open (my $suites_file, "<",  $file_name) or die "Can't open stress
suites file \"$file_name\": $!";
    while(<$suites_file>) {
	next if m"^\s*(#|$)";
        chomp;
	my ($test,$dbname,$duration_hours,$wipe_arg,$schema_dir,
$compat21_arg) = split;
	my $duration_seconds = 60 * 60 * $duration_hours;
	my $wipe_db = get_bool($wipe_arg);
	my $compat21 = get_bool($compat21_arg);
	push @tests, {test_name=>$test, db_name=>$dbname, duration_seconds=>
$duration_seconds, wipe_db=>$wipe_db, schema_dir=>$schema_dir,
compat21=>$compat21};
    }
    close($suites_file);
    return @tests;
}

How can I do this better?  I'm a perl newbie, so be gentle, but
thorough :-)


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

Date: Tue, 05 Jul 2011 22:14:04 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <87mxgsp34j.fsf@quad.sysarch.com>

>>>>> "g" == gry  <georgeryoung@gmail.com> writes:

  g> I'm reading a config file that I want to be forgiving/robust, so
  g> 'false','no','never' should parse to 0 and 'true','yes','always' to
  g> 1.   I have the following, which works, but seems cumbersome and not
  g> idiomatic perl:

learn to use return as flow control.

  g>     sub get_bool {
  g> 	my $val = $_[0];

don't index into @_ unless you need to. this is more standard:

	my( $val ) = @_ ;

  g> 	my $bool;

not needed with cleaner logic

  g> 	if ($val =~ m/true|yes|always|1/i) {
  g> 	    $bool = 1; }

odd formatting style. but irrelevent since we will fix it all up.

no need for the m in front of // if you use that delimiter. m is for
when you change delimiters.

that regex will also match on 'falsetrue' and all sorts of things which
contain those tokens. you need to anchor the regex to force only those
tokens to match:

	return 1 if $val =~ /^(?:true|yes|always|1)/i ;

and same with false:

	return 0 if $val =~ /^(?:false|no|never|0)/i ;

and now we fall through to defaulting 0

	return 0 ;


  g>     open (my $suites_file, "<",  $file_name) or die "Can't open stress
  g> suites file \"$file_name\": $!";

why use " inside "" strings? just use '$filename' instead and it is all
so much cleaner. and even better:

use File::Slurp ;

	my @lines = read_file( $suites_file ) ;

	foreach my $line ( @lines ) {

and use $line instead of the default $_ with the code below. no need to
close the file either.


  g>     while(<$suites_file>) {
  g> 	next if m"^\s*(#|$)";

again, why the m and alternate delimiters? you use alternate delimiters
only when the normal delimiter is in the string. / is not in that
string.


  g>         chomp;
  g> 	my ($test,$dbname,$duration_hours,$wipe_arg,$schema_dir,
  g> $compat21_arg) = split;
  g> 	my $duration_seconds = 60 * 60 * $duration_hours;
  g> 	my $wipe_db = get_bool($wipe_arg);
  g> 	my $compat21 = get_bool($compat21_arg);
  g> 	push @tests, {test_name=>$test, db_name=>$dbname, duration_seconds=>
  g> $duration_seconds, wipe_db=>$wipe_db, schema_dir=>$schema_dir,
  g> compat21=>$compat21};

please format that so it can be read!

	push @tests, {
		test_name	=> $test,
                db_name		=> $dbname,
                duration_seconds => $duration_seconds,
                wipe_db		=> $wipe_db,
                schema_dir	=> $schema_dir,
		compat21	=> $compat21,
	} ;

much nicer! easier to edit and modify as well. note i put a trailing
comma which is allowed in perl (few other langs allow that) which makes
it easy to add/delete/move lines like that.

finally, an even better way to validate the booleans would be with a
hash. something like this (untested):

my %valid_booles = (

	true	=> 1,
	yes	=> 1,
	always	=> 1,
        1	=> 1,
	false	=> 0,
	no	=> 0,
	never	=> 0,
        0	=> 0,
) ;

# note the lc to force the val to lower case

my $boole = %valid_booles{ lc $val } ;
$boole = 0 unless defined $boole ;
# that can be done as $boole //= 0 with recent perls. or even:
# my $boole = %valid_booles{ lc $val } // 0 ;

done!

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------


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

Date: Tue, 05 Jul 2011 22:58:30 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <slrnj17mtv.6cf.tadmc@tadbox.sbcglobal.net>

gry <georgeryoung@gmail.com> wrote:
> I'm reading a config file that I want to be forgiving/robust, so
> 'false','no','never' should parse to 0 and 'true','yes','always' to
> 1.   I have the following, which works, but seems cumbersome and not
> idiomatic perl:
>     sub get_bool {
> 	my $val = $_[0];
> 	my $bool;
> 	if ($val =~ m/true|yes|always|1/i) {
> 	    $bool = 1; }
> 	elsif ($val =~ m/false|no|never|0/i) {
> 	    $bool = 0; }


The whole elsif clause is not needed, if it were not there
you would still set $bool = 0 in the else clause anyway...


> 	else{
> 	    $bool = 0; }
> 	return $bool;
>     }


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Wed, 06 Jul 2011 07:23:59 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <8639ikouc0.fsf@gareth.avalon.lan>

"Uri Guttman" <uri@StemSystems.com> writes:

> finally, an even better way to validate the booleans would be with a
> hash. something like this (untested):
>
> my %valid_booles = (
>
> 	true	=> 1,
> 	yes	=> 1,
> 	always	=> 1,
>         1	=> 1,
> 	false	=> 0,
> 	no	=> 0,
> 	never	=> 0,
>         0	=> 0,
> ) ;
>
> # note the lc to force the val to lower case
>
> my $boole = %valid_booles{ lc $val } ;
> $boole = 0 unless defined $boole ;
> # that can be done as $boole //= 0 with recent perls. or even:
> # my $boole = %valid_booles{ lc $val } // 0 ;
>

In fact, the rule of thumb I use is "If you find yourself thinking of
more than one elsif clause on a single scalar test, maybe a hash will be
a better solution".

Mart

-- 
"We will need a longer wall when the revolution comes."
    --- AJS, quoting an uncertain source.


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

Date: Wed, 06 Jul 2011 13:25:33 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <87aacregua.fsf@sapphire.mobileactivedefense.com>

"Uri Guttman" <uri@StemSystems.com> writes:

[...]

> my %valid_booles = (
>
> 	true	=> 1,
> 	yes	=> 1,
> 	always	=> 1,
>         1	=> 1,
> 	false	=> 0,
> 	no	=> 0,
> 	never	=> 0,
>         0	=> 0,
> ) ;
>
> # note the lc to force the val to lower case
>
> my $boole = %valid_booles{ lc $val } ;
> $boole = 0 unless defined $boole ;

Since the cancel likely isn't going to be visible everywhere:
Technically, the claim that the code above is buggy is wrong because
it behaves identically to the original code in this respect.

In my opinion, it doesn't do what it should be doing but that's
something entirely different.


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

Date: Wed, 06 Jul 2011 06:01:31 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <867h7v36ms.fsf@red.stonehenge.com>

>>>>> "Rainer" == Rainer Weikusat <rweikusat@mssgmbh.com> writes:

Rainer> "Uri Guttman" <uri@StemSystems.com> writes:
Rainer> [...]

>> my %valid_booles = (
>> 
>> true	=> 1,
>> yes	=> 1,
>> always	=> 1,
>> 1	=> 1,
>> false	=> 0,
>> no	=> 0,
>> never	=> 0,
>> 0	=> 0,
>> ) ;
>> 
>> # note the lc to force the val to lower case
>> 
>> my $boole = %valid_booles{ lc $val } ;

Wrong syntax.

>> $boole = 0 unless defined $boole ;

Rainer> Since the cancel likely isn't going to be visible everywhere:
Rainer> Technically, the claim that the code above is buggy is wrong because
Rainer> it behaves identically to the original code in this respect.

Except the first accepted "byes" as yes, since the regex wasn't
anchored. This one avoids that mistake.

Rainer> In my opinion, it doesn't do what it should be doing but that's
Rainer> something entirely different.

Agreed.  If 0 is "everything else", just check for the "1" values.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


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

Date: Wed, 6 Jul 2011 07:44:58 -0700 (PDT)
From: gry <georgeryoung@gmail.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <61335629-1b89-4eb6-bcf8-bebad20cafeb@v7g2000vbk.googlegroups.com>

On Jul 6, 9:01=A0am, mer...@stonehenge.com (Randal L. Schwartz) wrote:
> >>>>> "Rainer" =3D=3D Rainer Weikusat <rweiku...@mssgmbh.com> writes:
>
> Rainer> "Uri Guttman" <u...@StemSystems.com> writes:
> Rainer> [...]
>
> >> my %valid_booles =3D (
>
> >> true =A0 =3D> 1,
> >> yes =A0 =A0=3D> 1,
> >> always =3D> 1,
> >> 1 =A0 =A0 =A0=3D> 1,
> >> false =A0=3D> 0,
> >> no =A0 =A0 =3D> 0,
> >> never =A0=3D> 0,
> >> 0 =A0 =A0 =A0=3D> 0,
> >> ) ;
>
> >> # note the lc to force the val to lower case
>
> >> my $boole =3D %valid_booles{ lc $val } ;
>
> Wrong syntax.
>
> >> $boole =3D 0 unless defined $boole ;
>
> Rainer> Since the cancel likely isn't going to be visible everywhere:
> Rainer> Technically, the claim that the code above is buggy is wrong beca=
use
> Rainer> it behaves identically to the original code in this respect.
>
> Except the first accepted "byes" as yes, since the regex wasn't
> anchored. This one avoids that mistake.
>
> Rainer> In my opinion, it doesn't do what it should be doing but that's
> Rainer> something entirely different.
>
> Agreed. =A0If 0 is "everything else", just check for the "1" values.
>
> --
> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 00=
95
> <mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
> Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
> Seehttp://methodsandmessages.posterous.com/for Smalltalk discussion

Thanks very much to you all!
Yes, I should have checked for not-a-valid-boolean-literal; I do now.
BTW, the reason for
   die "Can't open stress suites file \"$file_name\": $!";
is that I don't trust users not to use a file name like "file name",
which would make the die message confusing.
I didn't use the hash validity check here, but I'll definitely keep it
in mind.
My code looks much better now.  Thanks!


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

Date: Wed, 06 Jul 2011 13:18:26 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <878vsbl44d.fsf@quad.sysarch.com>

>>>>> "RW" == Rainer Weikusat <rweikusat@mssgmbh.com> writes:

  RW> "Uri Guttman" <uri@StemSystems.com> writes:
  RW> [...]

  >> my %valid_booles = (
  >> 
  >> true	=> 1,
  >> yes	=> 1,
  >> always	=> 1,
  >> 1	=> 1,
  >> false	=> 0,
  >> no	=> 0,
  >> never	=> 0,
  >> 0	=> 0,
  >> ) ;
  >> 
  >> # note the lc to force the val to lower case
  >> 
  >> my $boole = %valid_booles{ lc $val } ;
  >> $boole = 0 unless defined $boole ;

  RW> Since the cancel likely isn't going to be visible everywhere:
  RW> Technically, the claim that the code above is buggy is wrong because
  RW> it behaves identically to the original code in this respect.

cancel?

and how does that behave the same? try passing in 'FOOtrue' into both of
them.

  RW> In my opinion, it doesn't do what it should be doing but that's
  RW> something entirely different.

well, your opinion is wrong. the OP's code let in true values which
weren't in his list of allowed tokens.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------


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

Date: Wed, 06 Jul 2011 13:19:55 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <874o2zl41w.fsf@quad.sysarch.com>

>>>>> "g" == gry  <georgeryoung@gmail.com> writes:

  g> BTW, the reason for
  g>    die "Can't open stress suites file \"$file_name\": $!";
  g> is that I don't trust users not to use a file name like "file name",
  g> which would make the die message confusing.

no, you didn't get my comment. use single quotes around $file_name so
you don't need to escape the " chars which makes for harder to read
code.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
------------  Perl Developer Recruiting and Placement Services  -------------
-----  Perl Code Review, Architecture, Development, Training, Support -------


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

Date: Wed, 06 Jul 2011 19:59:44 +0200
From: Peter Tuente <peter.tuente@materna.de>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <iv27q8$b9j$1@pentheus.materna.de>

Uri Guttman schrieb:
> that regex will also match on 'falsetrue' and all sorts of things which
> contain those tokens. you need to anchor the regex to force only those
> tokens to match:
> 
> 	return 1 if $val =~ /^(?:true|yes|always|1)/i ;
> 
> and same with false:
> 
> 	return 0 if $val =~ /^(?:false|no|never|0)/i ;
> 
> and now we fall through to defaulting 0
> 
> 	return 0 ;

Hi Uri,

your solution above still returns 1 for e.g. $val == 'truefalse':
It should read:

	return 1 if $val =~ /^(?:true|yes|always|1)$/i ;

PiT


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

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 3434
***************************************


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