
Question on Unless and Until
--------------060407010707090801060104
Content-Type: text/plain;
charset="US-ASCII";
format="flowed"
Content-Disposition: inline
Hi All
I a newbie to perl. Reading through some of its basics on if constructs
, while constructs and so i also found a the unless and until constructs.
let me take the if construct ,
basically it is like this : if (<condition>) { .....}
now in the above construct if the condition evaluates to *true* only
then does the body of the loop enclosed in { } is executed. SO what i
understand is that the evaluation of truth in the condition is important
for the code in the if block to execute. Please correct me if i am wrong
in this regard.
Now coming to Unless and Until , The construct for this too is some
thing like this:
unless (<condition>) {..........} and until (<condition>) {.....}
Now i wanted to know when does the body of these constructs would
execute , Is it when the condition evaluates to true or false ?
I know it is very basic but would help me in continuing with my further
study.
Thanks
Jatin
--------------060407010707090801060104--
Re: Question on Unless and Until
At 9:53 AM +0530 8/30/10, Jatin Davey wrote:
> Hi All
>
>I a newbie to perl. Reading through some of its basics on if
>constructs , while constructs and so i also found a the unless and
>until constructs.
>
>let me take the if construct ,
>
>basically it is like this : if (<condition>) { .....}
>
>now in the above construct if the condition evaluates to *true* only
>then does the body of the loop enclosed in { } is executed. SO
>what i understand is that the evaluation of truth in the condition
>is important for the code in the if block to execute. Please correct
>me if i am wrong in this regard.
You are correct, except where you call the statement(s) enclosed in
braces {} a "loop". The statements are only executed one time for an
'if' statement, and that only if the condition is true. Your use of
the term "block" to describe the statements enclosed by {} is correct.
>
>Now coming to Unless and Until , The construct for this too is some
>thing like this:
>
>unless (<condition>) {..........} and until (<condition>) {.....}
The unless construct above is correct. The 'until' construct is not.
'until' can only occur at the end of a block preceded by 'do' or at
the end of a statement as a modifier.
do {
...
} until( condition );
or
die unless(condition);
>
>Now i wanted to know when does the body of these constructs would
>execute , Is it when the condition evaluates to true or false ?
'unless(condition){...}' is the same as 'if( ! condition ) {...}'
The block is executed if the condition evaluates to false.
See the built-in Perl documentation available from a command-line as
'perldoc perlsyn'.
--
Jim Gibson
Jim [at] Gibson.org
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
Thanks Jim
I got the understanding from this sample code as well:
#!/usr/bin/perl
use warnings;
use strict;
print "Enter your age : ";
unless ((my $age = <STDIN>) < 18) {
print "you can vote \n";
}
Thanks
Jatin
On 8/30/2010 11:15 AM, Jim Gibson wrote:
> At 9:53 AM +0530 8/30/10, Jatin Davey wrote:
>> Hi All
>>
>> I a newbie to perl. Reading through some of its basics on if
>> constructs , while constructs and so i also found a the unless and
>> until constructs.
>>
>> let me take the if construct ,
>>
>> basically it is like this : if (<condition>) { .....}
>>
>> now in the above construct if the condition evaluates to *true* only
>> then does the body of the loop enclosed in { } is executed. SO what
>> i understand is that the evaluation of truth in the condition is
>> important for the code in the if block to execute. Please correct me
>> if i am wrong in this regard.
>
>
> You are correct, except where you call the statement(s) enclosed in
> braces {} a "loop". The statements are only executed one time for an
> 'if' statement, and that only if the condition is true. Your use of
> the term "block" to describe the statements enclosed by {} is correct.
>
>>
>> Now coming to Unless and Until , The construct for this too is some
>> thing like this:
>>
>> unless (<condition>) {..........} and until (<condition>) {.....}
>
> The unless construct above is correct. The 'until' construct is not.
> 'until' can only occur at the end of a block preceded by 'do' or at
> the end of a statement as a modifier.
>
> do {
> ...
> } until( condition );
>
> or
>
> die unless(condition);
>
>
>>
>> Now i wanted to know when does the body of these constructs would
>> execute , Is it when the condition evaluates to true or false ?
>
>
> 'unless(condition){...}' is the same as 'if( ! condition ) {...}'
>
> The block is executed if the condition evaluates to false.
>
> See the built-in Perl documentation available from a command-line as
> 'perldoc perlsyn'.
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
>>>>> "JG" == Jim Gibson <jimsgibson [at] gmail.com> writes:
JG> The unless construct above is correct. The 'until' construct is
JG> not. 'until' can only occur at the end of a block preceded by 'do' or
JG> at the end of a statement as a modifier.
JG> do {
JG> ...
JG> } until( condition );
until is just while( ! EXPR ). it can be used whereever while can which
means loop blocks or statement modifiers. perldoc perlsyn covers this.
but i never use until anyhow. i use unless a fair amount as i don't like
if ( ! EXPR). my current boss has asked me to stop using unless but i am
not stopping.
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Mon, Aug 30, 2010 at 2:25 AM, Uri Guttman <uri [at] stemsystems.com> wrote:
> but i never use until anyhow. i use unless a fair amount as i don't like
> if ( ! EXPR). my current boss has asked me to stop using unless but i am
> not stopping.
It seems silly to use unless, but never use until. :) I think both
make the code more clean and readable, at least for native English
speakers.
--
Brandon McCaig <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence.org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wednesday 01 September 2010 18:06:37 Brandon McCaig wrote:
> On Mon, Aug 30, 2010 at 2:25 AM, Uri Guttman <uri [at] stemsystems.com> wrote:
> > but i never use until anyhow. i use unless a fair amount as i don't like
> > if ( ! EXPR). my current boss has asked me to stop using unless but i am
> > not stopping.
>
> It seems silly to use unless, but never use until. :) I think both
> make the code more clean and readable, at least for native English
> speakers.
Well, I can testify that unless makes the code more confusing for me as a n=
on-
native speaker of English. Whenever I encounter an unless, I have to say to=
myself: "OK, this translates to if (not COND()) which means this and that."=
=2E
Maybe it's because Hebrew does not have a commonly used word for "Unless" (=
we
have "Ilmal=C3=A9" but it's not in common use.) and we say "If not" instead=
=2E So I
never "unless" in my code - only "if (!)".
Regards,
Shlomi Fish
=2D-
=2D--------------------------------------------------------- -------
Shlomi Fish http://www.shlomifish.org/
Original Riddles - http://www.shlomifish.org/puzzles/
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wed, Sep 1, 2010 at 11:06, Brandon McCaig <bamccaig [at] gmail.com> wrote:
> On Mon, Aug 30, 2010 at 2:25 AM, Uri Guttman <uri [at] stemsystems.com> wrote:
>> but i never use until anyhow. i use unless a fair amount as i don't like
>> if ( ! EXPR). my current boss has asked me to stop using unless but i am
>> not stopping.
>
> It seems silly to use unless, but never use until. :) I think both
> make the code more clean and readable, at least for native English
> speakers.
snip
I will occasionally use until, but it rarely seems to be needed. I
use unless all of the time.
One reason is that you often have one variable you are testing a
while/until loop:
while ($continue) {}
until ($stop) {}
It seems natural to set the variable to true when you start and false
when you want to stop. Of course, the counter argument is that
until ($found) {}
makes more sense than
while ($notfound) {}
But I guess the habit of just adding not to the variable name is too
ingrained in me at this point.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wed, Sep 1, 2010 at 11:12, Shlomi Fish <shlomif [at] iglu.org.il> wrote:
> On Wednesday 01 September 2010 18:06:37 Brandon McCaig wrote:
>> On Mon, Aug 30, 2010 at 2:25 AM, Uri Guttman <uri [at] stemsystems.com> wrote=
:
>> > but i never use until anyhow. i use unless a fair amount as i don't li=
ke
>> > if ( ! EXPR). my current boss has asked me to stop using unless but i =
am
>> > not stopping.
>>
>> It seems silly to use unless, but never use until. :) I think both
>> make the code more clean and readable, at least for native English
>> speakers.
>
> Well, I can testify that unless makes the code more confusing for me as a=
non-
> native speaker of English. Whenever I encounter an unless, I have to say =
to
> myself: "OK, this translates to if (not COND()) which means this and that=
..".
> Maybe it's because Hebrew does not have a commonly used word for "Unless"=
(we
> have "Ilmal=C3=A9" but it's not in common use.) and we say "If not" inste=
ad. So I
> never "unless" in my code - only "if (!)".
snip
I don't care if you use unless or if, but please don't use if (!).
Say if (not) instead. The looser binding makes it easier to use and
the fact that it is three letters long (rather than one skinny
character long) makes it some much easier to see.
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On 10-09-01 11:37 AM, Chas. Owens wrote:
> I don't care if you use unless or if, but please don't use if (!).
> Say if (not) instead. The looser binding makes it easier to use and
> the fact that it is three letters long (rather than one skinny
> character long) makes it some much easier to see.
You do realize that you just made a good argument for unless. Of
course, if you want to make the purists cringe:
if( some_condition ){
# this space intentionally left blank
}else{
do_work();
}
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wed, Sep 1, 2010 at 12:12, Shawn H Corey <shawnhcorey [at] gmail.com> wrote:
> On 10-09-01 11:37 AM, Chas. Owens wrote:
>>
>> I don't care if you use unless or if, but please don't use if (!).
>> Say if (not) instead. =C2=A0The looser binding makes it easier to use an=
d
>> the fact that it is three letters long (rather than one skinny
>> character long) makes it some much easier to see.
>
> You do realize that you just made a good argument for unless.
snip
Of course I made a good argument for unless; I like unless more than
if(not), and I hate if(!). The unless statement is a beacon that
jumps out ands says "Hey, don't read this logic statement the way you
normally would!".
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wed, Sep 1, 2010 at 12:12 PM, Shawn H Corey <shawnhcorey [at] gmail.com> wrot=
e:
> Of course, if you want to make the purists cringe:
>
> if( some_condition ){
> =C2=A0# this space intentionally left blank
> }else{
> =C2=A0do_work();
> }
I actually have at least one colleague that does that (not at
"Castopulence"[1], but at my real j0rb). >_< It always makes me
/cringe/ when I stumble across it.
On an unrelated note, what is correct quoting etiquette when you only
want to quote part of a line? Above, I just reformatted the quote onto
a single line beginning at the start, but that was only easy because
the line was short enough. If it was too long, I know that my E-mail
"client" (Gmail) wouldn't have wrapped it properly (it would only have
the > on the first line and the rest would not appear quoted).
[1] My make-believe, future FLOSS company. :D
--
Brandon McCaig <bamccaig [at] gmail.com>
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software <http://www.castopulence.org/> <bamccaig [at] castopulence=
..org>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Question on Unless and Until
On Wed, Sep 01, 2010 at 01:00:21PM -0400, Brandon McCaig wrote:
> On an unrelated note, what is correct quoting etiquette when you only
> want to quote part of a line?
This is how I do it:
> Above, I just reformatted the quote onto
> a single line beginning at the start, but that was only easy because
> the line was short enough.
Yes, it takes a little more effort, but I think that a little more
effort on my part compared to less effort for hundreds of readers is a
sensible trade off.
I try not to reformat the original text if possible.
--
Paul Johnson - paul [at] pjcj.net
http://www.pjcj.net
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/