[32170] in Perl-Users-Digest
Perl-Users Digest, Issue: 3435 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jul 7 11:09:25 2011
Date: Thu, 7 Jul 2011 08: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, 7 Jul 2011 Volume: 11 Number: 3435
Today's topics:
liberate <r@thevoid1.net>
Re: liberate <jurgenex@hotmail.com>
Re: sort scientific notation value after alphabet <cartercc@gmail.com>
Re: sort scientific notation value after alphabet <cartercc@gmail.com>
Re: sort scientific notation value after alphabet <willem@turtle.stack.nl>
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 <rweikusat@mssgmbh.com>
Re: sort scientific notation value after alphabet <jimsgibson@gmail.com>
Re: sort scientific notation value after alphabet <rweikusat@mssgmbh.com>
Re: sort scientific notation value after alphabet <cartercc@gmail.com>
Re: sort scientific notation value after alphabet <rweikusat@mssgmbh.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 sln@netherlands.com
Re: style?: how to convert to boolean: false,no -> 0, y sln@netherlands.com
Re: style?: how to convert to boolean: false,no -> 0, y <bugbear@trim_papermule.co.uk_trim>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 06 Jul 2011 18:23:29 -0600
From: robin <r@thevoid1.net>
Subject: liberate
Message-Id: <iv2ua8$sbc$1@speranza.aioe.org>
liberate as defense!!!
a lot
you should, you still can as defense.
------------------------------
Date: Wed, 06 Jul 2011 23:38:02 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: liberate
Message-Id: <nska171fpagfrqqim78kje0pqbpt5uu8ee@4ax.com>
robin <r@thevoid1.net> wrote:
>liberate as defense!!!
>
>a lot
>
>you should, you still can as defense.
I am getting an error message
Can't locate object method "a" via package "lot" (perhaps you
forgot to load "lot"?) at ....
Are you getting the same error message?
It really would have helped if you would have mentioned that in your
original post. Don't forget to always include observed behaviour and
expected behaviour with your code sample.
Otherwise we cannot tell what you were trying to do.
jue
------------------------------
Date: Wed, 6 Jul 2011 11:40:46 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <889dc077-32d2-4b48-b1d6-64f2c9a4c651@a7g2000vby.googlegroups.com>
On Jul 6, 10:14 am, Ralph Malph <ra...@happydays.com> wrote:
> 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.
Not only that, but when you automate the creation of data structures,
you don't know or care what the variables are!
If you use the header of a typical data file to create an array, then
iterate through the remainder of the lines and use the header array to
deconstruct each line and stuff the datums into a hash element, you
get all the variables for free.
When you deal with files with 80, 100, or more columns, you learn real
quick the most expedient way to deal with that number of columns.
Computer time and space is much, much cheaper than programmer time,
and if you can deal with this number of columns in three lines of
code, so much the better. In Perlish pseudocode:
open IN, '<', 'data.dat';
my @header = <IN>;
while (<IN>)
{
@line = split;
@hash{@header} = @line;
}
CC
------------------------------
Date: Wed, 6 Jul 2011 12:45:27 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <4880fe82-68cf-47dd-8350-d437533f6c11@a11g2000yqm.googlegroups.com>
On Jul 6, 1:16=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> 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.
I just posted something showing that you can give every datum a name
without thinking about it, without reading it, without putting it
explicitly in your code. If you are writing the code by hand, that's
one thing. But if you are automating your code, that's another thing.
I absolutely agree with you about writing readable code. However, you
should assume that your reader knows the language. After all, you
aren't writing for apprentices but for journeyman coders.
I would also point out that some tools make it very easy to do what I
did, namely, vi. If you have the names of the fields, you can create a
list of variable names in one line of ex and never even have to think
about the variable names. I occasionally see data files with over 100
columns, and /NEVER/ retype the names of the fields. It would be a lot
harder to stumble through the list of field names and decide which
ones to undef and which ones to keep.
This isn't an attack or insult or criticism of you, just a reminder
that TIMTOWTDI.
CC.
------------------------------
Date: Wed, 6 Jul 2011 20:25:11 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <slrnj19h57.1frs.willem@turtle.stack.nl>
Uri Guttman wrote:
)>>>>> "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.
It's only misleading to readers who subscribe to the belief that you
shouldn't declare values you don't use. In other words, you're begging the
question.
Showing that you know values will be discarded only says you know what the
program does. Declaring all the vars shows that you care about the
possibility that the program may be changed later, perhaps by a different
programmer.
Also, your use of 'scared' shows you're reasoning from emotion rather than
logic.
You are writing code to be maintained, not just read.
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 16:32:55 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <871uy3i1zc.fsf@quad.sysarch.com>
>>>>> "W" == Willem <willem@turtle.stack.nl> writes:
W> Uri Guttman wrote:
W> )>>>>> "W" == Willem <willem@turtle.stack.nl> writes:
W> )
W> ) W> Uri Guttman wrote:
W> ) W> ) but it is still misleading. it is better to declare only vars you will
W> ) W> ) be using later.
W> )
W> ) W> Why?
W> )
W> ) so you don't mislead the reader. showing that you know values will be
W> ) discarded says you understand the data coming in. declaring all the vars
W> ) and not using them says you are scared of throwing away data even though
W> ) you are. you are writing code to be read, not just for the computer to
W> ) run.
W> It's only misleading to readers who subscribe to the belief that you
W> shouldn't declare values you don't use. In other words, you're begging the
W> question.
nope. it is misleading as they may look for the variables later on and
not find them. that leads to questioning the coder's mind set as to why
they assigned variables and didn't use them. it isn't begging the
question, it is questioning the coder.
W> Showing that you know values will be discarded only says you know what the
W> program does. Declaring all the vars shows that you care about the
W> possibility that the program may be changed later, perhaps by a different
W> programmer.
then you add the variables to be assigned then. why not declare 10
million unused variables and maybe use them later on? sure it is extreme
but you won't have to type those names in again.
W> Also, your use of 'scared' shows you're reasoning from emotion
W> rather than logic. You are writing code to be maintained, not just
W> read.
then you need more emotion. emotions will tell you when things are coded
well or not. logic can't help with that. can logic tell you a particular
name works better than others? or comments are well written? those are
subjective parts of code that logic skips but humans (with emotions)
don't. you code for people, not computers. emotions count with (normal)
people.
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 21:54:54 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <slrnj19mde.1hgd.willem@turtle.stack.nl>
Uri Guttman wrote:
)>>>>> "W" == Willem <willem@turtle.stack.nl> writes:
) W> It's only misleading to readers who subscribe to the belief that you
) W> shouldn't declare values you don't use. In other words, you're begging the
) W> question.
)
) nope. it is misleading as they may look for the variables later on and
) not find them. that leads to questioning the coder's mind set as to why
) they assigned variables and didn't use them. it isn't begging the
) question, it is questioning the coder.
Why would they be looking for them?
) W> Showing that you know values will be discarded only says you know what the
) W> program does. Declaring all the vars shows that you care about the
) W> possibility that the program may be changed later, perhaps by a different
) W> programmer.
)
) then you add the variables to be assigned then. why not declare 10
) million unused variables and maybe use them later on? sure it is extreme
) but you won't have to type those names in again.
We're talking about assigning on splitting fields so your comment is
meaningless.
When you have to add the variables to be assigned you have to figure out
again which field is which. That's wasted effort.
) W> Also, your use of 'scared' shows you're reasoning from emotion
) W> rather than logic. You are writing code to be maintained, not just
) W> read.
)
) then you need more emotion. emotions will tell you when things are coded
) well or not.
Wrong.
It will only tell you when things are coded to your very subjective set of
baseless beliefs. There are objective measures for how well things are
coded, which are founded on actual arguments instead of gut feeling.
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 22:58:39 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <87sjqjkr5c.fsf@sapphire.mobileactivedefense.com>
Willem <willem@turtle.stack.nl> writes:
> Uri Guttman wrote:
> )>>>>> "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.
>
> It's only misleading to readers who subscribe to the belief that you
> shouldn't declare values you don't use. In other words, you're begging the
> question.
This makes absolutely no sense. It is 'misleading' because someone who
isn't already familiar with the code has to start with the assumption
that everything is meaningful until proven otherwise. This assumption
will turn out to be wrong after some amount of time has spent on
checking it for code text that actually has no meaning in the context
of the 'code set' it happens to be part of (eg, because it was just
generated as a side effect of the use of a feature of some other
software the person who 'caused' it happened to be using). There is no
way this meaningless code could mislead someone who happens to hold a
particular opinion about meaningless code after that someone has
gained the knowledge that the code in question is meaningless because
of this opinion.
A slightly modified statement, 'it is only misleading to people who
"subscribe to the belief" that a text whose content isn't already
known to them in sufficient detail to actually judge that likely has
a meaning', IOW, to people who have learnt to read, makes some logical
sense (although not more practical sense than "I wasn't effectively
blocking the emergency exit since no emergency occured[*]".
[*] Dedicated to people putting their bicycles in front of
the doors on commuter trains ...
------------------------------
Date: Wed, 06 Jul 2011 17:14:13 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <060720111714135713%jimsgibson@gmail.com>
In article <87sjqjkr5c.fsf@sapphire.mobileactivedefense.com>, Rainer
Weikusat <rweikusat@mssgmbh.com> wrote:
> Willem <willem@turtle.stack.nl> writes:
> > Uri Guttman wrote:
> > )>>>>> "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.
> >
> > It's only misleading to readers who subscribe to the belief that you
> > shouldn't declare values you don't use. In other words, you're begging the
> > question.
>
> This makes absolutely no sense.
It makes perfect sense. Uri is complaining that someone would declare a
variable that is not used. Uri wouldn't do this and assumes that nobody
else would either, so Uri expects every variable declared to be used
somewhere. Willem says no, you shouldn't make any such assumption.
> It is 'misleading' because someone who
> isn't already familiar with the code has to start with the assumption
> that everything is meaningful until proven otherwise.
That is a bad assumption unless _everybody_ is operating under the same
philosophy of never declaring a variable and assigning it a value in a
split statement unless the variable is actually used later. Since not
everybody holds to that philosophy, it is foolish to assume that every
declared variable is used. Anybody with that assumption is only
misleading him/herself.
> This assumption
> will turn out to be wrong after some amount of time has spent on
> checking it for code text that actually has no meaning in the context
> of the 'code set' it happens to be part of (eg, because it was just
> generated as a side effect of the use of a feature of some other
> software the person who 'caused' it happened to be using). There is no
> way this meaningless code could mislead someone who happens to hold a
> particular opinion about meaningless code after that someone has
> gained the knowledge that the code in question is meaningless because
> of this opinion.
I am unable to parse the preceding two sentences. Sorry.
>
> A slightly modified statement, 'it is only misleading to people who
> "subscribe to the belief" that a text whose content isn't already
> known to them in sufficient detail to actually judge that likely has
> a meaning', IOW, to people who have learnt to read, makes some logical
> sense (although not more practical sense than "I wasn't effectively
> blocking the emergency exit since no emergency occured[*]".
I don't see the analogy. Blocking an exit is a potential risk. There is
no risk in declaring and not using variables. There is a slight amount
of overhead. Those who would not do such a thing may misinterpret why
it was done, but that is their own fault, not the fault of the coder.
Personally, I would rather see this:
my( $first, $last, $address, $city, $state, $zip, $email ) =
split(' ',$record);
than this:
my( $first, $last, undef. undef, undef, undef, $email ) =
split(' ',$record);
even if only those three fields are used in the program. Anytime I need
the additional fields, they will be there in the declared variables.
There are other examples of critics accusing others of "misleading"
them by using perfectly good constructs that they themselves would not
use, and expecting everybody else to follow their lead. For example,
the use of double quotes to delineate constant strings is often
criticized:
my $filename = "members.dat";
"You don't do any interpolation in this string so you should use single
quotes Since you used double quotes I expected to find some sort of
interpolation, so you misled me!" they will exclaim.
I don't agree with this sort of criticism.
--
Jim Gibson
------------------------------
Date: Thu, 07 Jul 2011 13:24:40 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <87d3hme0s7.fsf@sapphire.mobileactivedefense.com>
Jim Gibson <jimsgibson@gmail.com> writes:
> In article <87sjqjkr5c.fsf@sapphire.mobileactivedefense.com>, Rainer
> Weikusat <rweikusat@mssgmbh.com> wrote:
>
>> Willem <willem@turtle.stack.nl> writes:
>> > Uri Guttman wrote:
>> > )>>>>> "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.
>> >
>> > It's only misleading to readers who subscribe to the belief that you
>> > shouldn't declare values you don't use. In other words, you're begging the
>> > question.
>>
>> This makes absolutely no sense.
>
> It makes perfect sense. Uri is complaining that someone would declare a
> variable that is not used.
The specific statement was 'it is misleading'.
> Uri wouldn't do this and assumes that nobody else would either,
The first half of this sentence seems to be a somewhat reasonable
assumption, although it is not uncommon for people to complain about
things others do despite doing them themselves. But the second is
entirely your invention.
[...]
>> It is 'misleading' because someone who
>> isn't already familiar with the code has to start with the assumption
>> that everything is meaningful until proven otherwise.
>
> That is a bad assumption unless _everybody_ is operating under the same
> philosophy of never declaring a variable and assigning it a value in a
> split statement unless the variable is actually used later.
It is the only sensible assumption in face of a text whose meaning is
still unknown: It will have a meaning that needs to be determined. The
opposite assumption, namely, "it won't have a meaning" (so why read it)
is not an option for people who have to work with code they haven't
written themselves.
[...]
>> A slightly modified statement, 'it is only misleading to people who
>> "subscribe to the belief" that a text whose content isn't already
>> known to them in sufficient detail to actually judge that likely has
>> a meaning', IOW, to people who have learnt to read, makes some logical
>> sense (although not more practical sense than "I wasn't effectively
>> blocking the emergency exit since no emergency occured[*]".
>
> I don't see the analogy.
That's because there is no analogy.
------------------------------
Date: Thu, 7 Jul 2011 06:21:21 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <9eed69f4-a62c-4af8-8aa8-41035cfc6fd6@q1g2000vbj.googlegroups.com>
On Jul 6, 8:14=A0pm, Jim Gibson <jimsgib...@gmail.com> wrote:
> Personally, I would rather see this:
>
> =A0 my( $first, $last, $address, $city, $state, $zip, $email =A0) =3D
> =A0 =A0 split(' ',$record);
>
> than this:
>
> =A0 my( $first, $last, undef. undef, undef, undef, $email ) =3D
> =A0 =A0 split(' ',$record);
And who would want to see this?
my @line =3D split(' ', $_);
# then, later on, you have this
if ($line[98] =3D~ /G\d\d/) { ... )
elsif ($line[98] =3D~ /H\d\d/) { ... }
elsif ($line[54] !~ /\d{7}/) { ... }
else { print "Error for $line[54], $line[98], ID: $line[3]\n"; }
And yes, I have written such code, and still have some left that I
have to maintain. It's truly a nightmare. My strategy to maintaining
this sort of stuff is to name every field from the outset. Then, I
never have to think about whether I have named the variable or what
the name is (I use the column headings for the name, replacing spaces,
when they exist, by an underscore).
CC
------------------------------
Date: Thu, 07 Jul 2011 15:26:51 +0100
From: Rainer Weikusat <rweikusat@mssgmbh.com>
Subject: Re: sort scientific notation value after alphabet
Message-Id: <871uy2dv4k.fsf@sapphire.mobileactivedefense.com>
ccc31807 <cartercc@gmail.com> writes:
> On Jul 6, 8:14 pm, Jim Gibson <jimsgib...@gmail.com> wrote:
>> Personally, I would rather see this:
>>
>> my( $first, $last, $address, $city, $state, $zip, $email ) =
>> split(' ',$record);
>>
>> than this:
>>
>> my( $first, $last, undef. undef, undef, undef, $email ) =
>> split(' ',$record);
>
> And who would want to see this?
>
> my @line = split(' ', $_);
> # then, later on, you have this
> if ($line[98] =~ /G\d\d/) { ... )
> elsif ($line[98] =~ /H\d\d/) { ... }
> elsif ($line[54] !~ /\d{7}/) { ... }
> else { print "Error for $line[54], $line[98], ID: $line[3]\n"; }
>
> And yes, I have written such code, and still have some left that I
> have to maintain. It's truly a nightmare. My strategy to maintaining
> this sort of stuff is to name every field from the outset. Then, I
> never have to think about whether I have named the variable or what
> the name is
An alternative way for using names in such a scenario is to define
constants and use them for the array indices, eg
use constant FIRST => 0;
use constant LAST => 1;
use constant ADDRESS => 2;
use constant CITY => 3;
use constant STATE => 4;
use constant ZIP => 5;
use constant EMAIL => 6;
While this might mean somewhat more typing effort, it avoids the
runtime overhead of creating and initializing unused lexical
variables. If desired, the set of constants can also be restriced to
the fields which are actually used because the split-statement doesn't
need to have information about the number and order of fields
'hard-coded' in it anymore when the result is assigned to an array.
------------------------------
Date: Wed, 06 Jul 2011 14:28:28 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <877h7vjmb7.fsf@quad.sysarch.com>
>>>>> "PT" == Peter Tuente <peter.tuente@materna.de> writes:
PT> 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 ;
PT> Hi Uri,
PT> your solution above still returns 1 for e.g. $val == 'truefalse':
PT> It should read:
PT> return 1 if $val =~ /^(?:true|yes|always|1)$/i ;
yep. i forgot the ending anchor. i did use one in my oneliner test but
didn't edit it correctly here. anyhow the hash solution is the better
answer in several ways.
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:40:14 -0700
From: sln@netherlands.com
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <vnh9175iqk10empne8ab8jv4lfeq5f214p@4ax.com>
On Tue, 5 Jul 2011 18:39:35 -0700 (PDT), 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; }
> 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 :-)
If your 'bool' regex can only match one item, the below will work.
But, you should understand that this is your personal interpretation
of true/false, not Perls.
-sln
---------------
use strict;
use warnings;
my @samples = qw/
true
yes
always
1
false
no
never
0
/;
#
for my $item (@samples)
{
printf "%-10s = %s\n", $item, get_bool( $item );
}
printf "%-10s = %s\n", 'undef', get_bool();
#
sub get_bool {
return ($_[0] && $_[0] =~ /^\s*(?:true|yes|always|1)\s*$/i) || 0;
}
__END__
------------------------------
Date: Wed, 06 Jul 2011 14:32:36 -0700
From: sln@netherlands.com
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <81k917tkheiv5007p4ak3pg0qp4u5tm087@4ax.com>
On Wed, 06 Jul 2011 13:40:14 -0700, sln@netherlands.com wrote:
>On Tue, 5 Jul 2011 18:39:35 -0700 (PDT), gry <georgeryoung@gmail.com> wrote:
>
[snip]
>
>If your 'bool' regex can only match one item, the below will work.
Oh, I guess the regex will always return 1 or undef in scalar context
like:
return $_[0] && $_[0] =~ /^(\s*)(true|yes|always|1)(\s*)$/i || 0;
In list context however, it could return the number of capture buffer matches
like:
return $_[0] && ( () = $_[0] =~ /^(\s*)(true|yes|always|1)(\s*)$/i ) || 0;
Then the return value would be 3 or 0.
Newbies to regex sometimes use capture buffer syntax for just grouping,
especially in other languages/utilities.
-sln
------------------------------
Date: Thu, 07 Jul 2011 09:34:59 +0100
From: bugbear <bugbear@trim_papermule.co.uk_trim>
Subject: Re: style?: how to convert to boolean: false,no -> 0, yes, true -> 1
Message-Id: <PvOdnTbFkNUp8ojTnZ2dnUVZ8nOdnZ2d@brightview.co.uk>
gry 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; }
> else{
> $bool = 0; }
> return $bool;
> }
I note your "forgiving/robust" and suggest:
sub get_bool {
my ($val) = @_;
return 1 if($val =~ /true|yes|always|1/i);
return 0;
}
(typed, not tested)
This is equivalent to your code; you careful checking for the "false"
token is redundant, since you return "0" in your default clause.
If you intend to have some validation, your default clause might (well)
throw an exception, in which case checking for valid false token
would be needed.
BugBear
------------------------------
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 3435
***************************************