loop break condition

loop break condition

am 22.08.2011 07:45:42 von anant mittal

--000e0cd2dbe8f95c5b04ab119660
Content-Type: text/plain; charset=ISO-8859-1

hello!
i want to input some numbers via in while loop.And loop should be
broken if any nonnumeric character is entered.So how it can be checked.

.........................
..........................
my @ln;
my $i=0;
print"Give line numbers you want to put into array.\n";
while(1){
$ln[$i]=;
chomp $ln[$i];
if($ln[$i] =~ /\D/){
print"Its not numeric value.\n";
$i--;
last;
}
$i++;
}
..............................

One more query:-
HOW TO REMOVE ANY ELEMENT FROM AN ARRAY.
I mean if i have allocated till arr[5]. Now I want to remove the value
arr[4] and arr[5]. So how it can be done so that $#arr would tell 3.

--000e0cd2dbe8f95c5b04ab119660--

Re: loop break condition

am 22.08.2011 09:40:05 von Alan Haggai Alavi

Hello Anant,

> i want to input some numbers via in while loop.And loop should be
> broken if any nonnumeric character is entered.So how it can be checked.
>
> ........................
> .........................
> my @ln;
> my $i=0;
> print"Give line numbers you want to put into array.\n";
> while(1){
> $ln[$i]=;
> chomp $ln[$i];
> if($ln[$i] =~ /\D/){
> print"Its not numeric value.\n";
> $i--;
> last;
> }
> $i++;
> }
> .............................

In the above program, `$i` is mostly useless. The length of the array can
easily be found out by using `scalar @ln`; Perl's `push` function can be used
for pushing values into the array. There is no need for an index. The program
can be rewritten as:

use strict;
use warnings;

my @numbers;
print "Enter numbers:\n";
while (1) {
chomp( my $number = );
if ( $number =~ /\D/ ) {
print "$number is not a numeric value.\n";
last;
}
else {
push @numbers, $number;
}
}


> One more query:-
> HOW TO REMOVE ANY ELEMENT FROM AN ARRAY.
> I mean if i have allocated till arr[5]. Now I want to remove the value
> arr[4] and arr[5]. So how it can be done so that $#arr would tell 3.

You can use the `splice` function (read: `perldoc -f splice`) for this:

splice @array, 4, 2;


Regards,
Alan Haggai Alavi.
--
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 22.08.2011 10:47:41 von timothy adigun

--0016e659f26cc5eff104ab14212d
Content-Type: text/plain; charset=ISO-8859-1

Hi Anant,

One more query:-
> HOW TO REMOVE ANY ELEMENT FROM AN ARRAY.
> I mean if i have allocated till arr[5]. Now I want to remove the value
> arr[4] and arr[5]. So how it can be done so that $#arr would tell 3.
>

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

my @arr=qw( home father son sun mother drinks);
$#arr=3; # by assigning the the length of array to 3
# one has reduced the array element
# you can also increase the length by assigning an upper
number
# to $, however the element will be empty.
print $#arr; # print 3

@arr=qw( home father son sun mother drinks);
for(my $i=0;$i<=1;$i++){
pop @arr; # you can also use function "pop" to reduce the
# element of array, here using a for loop to
# iterate twice with pop we removed the last
# two array element
}
print $#arr; # print 3
__END__

Read on these functions=> splice,pop,push,shift and unshift. using perldoc
-f "pop"

Timothy,
Regards.

--0016e659f26cc5eff104ab14212d--

Re: loop break condition

am 22.08.2011 11:03:42 von Shlomi Fish

Hi Alan,

On Mon, 22 Aug 2011 13:10:05 +0530
Alan Haggai Alavi wrote:

> Hello Anant,
>=20
> > i want to input some numbers via in while loop.And loop should =
be
> > broken if any nonnumeric character is entered.So how it can be checked.
> >=20
> > ........................
> > .........................
> > my @ln;
> > my $i=3D0;
> > print"Give line numbers you want to put into array.\n";
> > while(1){
> > $ln[$i]=3D;
> > chomp $ln[$i];
> > if($ln[$i] =3D~ /\D/){
> > print"Its not numeric value.\n";
> > $i--;
> > last;
> > }
> > $i++;
> > }
> > .............................
>=20
> In the above program, `$i` is mostly useless. The length of the array can=
=20
> easily be found out by using `scalar @ln`; Perl's `push` function can be =
used=20
> for pushing values into the array. There is no need for an index. The pro=
gram=20
> can be rewritten as:
>=20
> use strict;
> use warnings;
>=20
> my @numbers;
> print "Enter numbers:\n";
> while (1) {
> chomp( my $number =3D );
> if ( $number =3D~ /\D/ ) {
> print "$number is not a numeric value.\n";
> last;
> }
> else {
> push @numbers, $number;
> }
> }

It's a good idea to always use "last LABEL;" instead of "last;" (as well as
"next LABEL;" etc. in case more loops are added in between.

So the program becomes:

[CODE]
use strict;
use warnings;

my @numbers;
print "Enter numbers:\n";
STDIN_LOOP:
while (1) {
chomp( my $number =3D );
if ( $number =3D~ /\D/ ) {
print "$number is not a numeric value.\n";
last STDIN_LOOP;
}
else {
push @numbers, $number;
}
}
[/CODE]

See:

http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labels

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
What Makes Software Apps High Quality - http://shlom.in/sw-quality

Live as if you were to die tomorrow. Learn as if you were to live forever.
â€=94 http://en.wikiquote.org/wiki/Mohandas_Gandhi (Disputed)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 22.08.2011 11:13:48 von Alan Haggai Alavi

Hello Shlomi,

> It's a good idea to always use "last LABEL;" instead of "last;" (as well =
as
> "next LABEL;" etc. in case more loops are added in between.
> â‹=AE
> http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labels

Now I understand why it is always good to label loops that use `last`, `nex=
t`=20
or `redo`.

Thank you. :-)

Regards,
Alan Haggai Alavi.
=2D-=20
The difference makes the difference.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 22.08.2011 11:29:19 von Shlomi Fish

Hi Alan,

On Mon, 22 Aug 2011 14:43:48 +0530
Alan Haggai Alavi wrote:

> Hello Shlomi,
>=20
> > It's a good idea to always use "last LABEL;" instead of "last;" (as wel=
l as
> > "next LABEL;" etc. in case more loops are added in between.
> > â‹=AE
> > http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labels
>=20
> Now I understand why it is always good to label loops that use `last`, `n=
ext`=20
> or `redo`.
>=20
> Thank you. :-)
>=20

Nice. And you're welcome.

For more insights like this, read the rest of the
http://perl-begin.org/tutorials/bad-elements/ page, and also the book Perl =
Best
Practices by Damian Conway, from which I borrowed a lot of advice.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs

Xena the warrior princess can meet King David for breakfast and Julius Caes=
ar
for lunch. Without time travel.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 22.08.2011 14:07:49 von Shawn H Corey

On 11-08-22 05:03 AM, Shlomi Fish wrote:
> It's a good idea to always use "last LABEL;" instead of "last;" (as well as
> "next LABEL;" etc. in case more loops are added in between.

Good idea but try to choose meaningful names. Also, the else clause is
not needed.

[CODE]
use strict;
use warnings;

my @numbers = ();

print "Enter numbers, one per line:\n";

INPUT_NUMBERS_LOOP:
while (1) { # do forever

# get a line from STDIN
chomp( my $number = );

# validate if a number
if ( $number =~ /\D/ ) {
print "$number is not a numeric value.\n";
last INPUT_NUMBERS_LOOP;
}

# don't store empty lines
if( length( $number ) > 0 ){
push @numbers, $number;
}

}
[/CODE]


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

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.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 23.08.2011 01:37:05 von merlyn

>>>>> "anant" == anant mittal writes:

anant> $ln[$i]=;

I'd swear that lowercase "stdin" was deprecated already, but I can't
find any record of it in the deltas, and it still works in 5.12 (I don't
have 5.14 compiled here).

In any case, you should shift to the proper STDIN, as everyone else's
answer gave, even though they didn't explicitly call this out.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 23.08.2011 01:49:56 von Shawn H Corey

On 11-08-22 07:37 PM, Randal L. Schwartz wrote:
>>>>>> "anant" == anant mittal writes:
>
> anant> $ln[$i]=;
>
> I'd swear that lowercase "stdin" was deprecated already, but I can't
> find any record of it in the deltas, and it still works in 5.12 (I don't
> have 5.14 compiled here).
>
> In any case, you should shift to the proper STDIN, as everyone else's
> answer gave, even though they didn't explicitly call this out.
>

Yup, still works in 5.14.1


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

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.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 24.08.2011 17:32:30 von Emeka

--000e0cd313322fec6504ab420507
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable

Do we really need "goto" here?

Emeka

On Mon, Aug 22, 2011 at 10:03 AM, Shlomi Fish wrote=
:

> Hi Alan,
>
> On Mon, 22 Aug 2011 13:10:05 +0530
> Alan Haggai Alavi wrote:
>
> > Hello Anant,
> >
> > > i want to input some numbers via in while loop.And loop shoul=
d
> be
> > > broken if any nonnumeric character is entered.So how it can be checke=
d.
> > >
> > > ........................
> > > .........................
> > > my @ln;
> > > my $i=3D0;
> > > print"Give line numbers you want to put into array.\n";
> > > while(1){
> > > $ln[$i]=3D;
> > > chomp $ln[$i];
> > > if($ln[$i] =3D~ /\D/){
> > > print"Its not numeric value.\n";
> > > $i--;
> > > last;
> > > }
> > > $i++;
> > > }
> > > .............................
> >
> > In the above program, `$i` is mostly useless. The length of the array c=
an
> > easily be found out by using `scalar @ln`; Perl's `push` function can b=
e
> used
> > for pushing values into the array. There is no need for an index. The
> program
> > can be rewritten as:
> >
> > use strict;
> > use warnings;
> >
> > my @numbers;
> > print "Enter numbers:\n";
> > while (1) {
> > chomp( my $number =3D );
> > if ( $number =3D~ /\D/ ) {
> > print "$number is not a numeric value.\n";
> > last;
> > }
> > else {
> > push @numbers, $number;
> > }
> > }
>
> It's a good idea to always use "last LABEL;" instead of "last;" (as well =
as
> "next LABEL;" etc. in case more loops are added in between.
>
> So the program becomes:
>
> [CODE]
> use strict;
> use warnings;
>
> my @numbers;
> print "Enter numbers:\n";
> STDIN_LOOP:
> while (1) {
> chomp( my $number =3D );
> if ( $number =3D~ /\D/ ) {
> print "$number is not a numeric value.\n";
> last STDIN_LOOP;
> }
> else {
> push @numbers, $number;
> }
> }
> [/CODE]
>
> See:
>
> http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labels
>
> Regards,
>
> Shlomi Fish
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish http://www.shlomifish.org/
> What Makes Software Apps High Quality - http://shlom.in/sw-quality
>
> Live as if you were to die tomorrow. Learn as if you were to live forever=
..
> =97 http://en.wikiquote.org/wiki/Mohandas_Gandhi (Disputed)
>
> Please reply to list if it's a mailing list post - http://shlom.in/reply =
..
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--=20
*Satajanus Nig. Ltd


*

--000e0cd313322fec6504ab420507--

Re: loop break condition

am 24.08.2011 17:49:02 von Shlomi Fish

On Wed, 24 Aug 2011 16:32:30 +0100
Emeka wrote:

> Do we really need "goto" here?
>

Where do you see a "goto"? Perl 5 has a goto statement (see
http://perldoc.perl.org/functions/goto.html ) and it's pretty flexible, but we
did not use it here, and instead used "last LABEL" or "next LABEL" which
behave differently. And if you're worried about gotos, you shouldn't use even
non-labelled "last" and "next", or returns from the middle of subroutines, and
your CPU uses many jump instructions too, so there.

Also, please avoid top-posting.

Regards,

Shlomi Fish

> Emeka
>

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
http://www.shlomifish.org/humour/ways_to_do_it.html

Chuck Norris refactors 10 million lines of Perl code before lunch.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 24.08.2011 18:23:03 von Jim Gibson

On 8/24/11 Wed Aug 24, 2011 8:32 AM, "Emeka"
scribbled:

> Do we really need "goto" here?
>
> Emeka

No, we don't need a goto here. The statement 'last' is effectively a 'goto'
but with the restriction that it will only work inside a loop and only
transfer control out of the loop. This avoids many of the drawbacks of a
more general goto statement, which can be used anywhere and transfer control
to anywhere.

>
> On Mon, Aug 22, 2011 at 10:03 AM, Shlomi Fish wrote:
>
>> Hi Alan,
>>
>> On Mon, 22 Aug 2011 13:10:05 +0530
>> Alan Haggai Alavi wrote:
>>
>>> Hello Anant,
>>>
>>>> i want to input some numbers via in while loop.And loop should
>> be
>>>> broken if any nonnumeric character is entered.So how it can be checked.
>>>>

>>
>> It's a good idea to always use "last LABEL;" instead of "last;" (as well as
>> "next LABEL;" etc. in case more loops are added in between.
>>
>> So the program becomes:
>>
>> [CODE]
>> use strict;
>> use warnings;
>>
>> my @numbers;
>> print "Enter numbers:\n";
>> STDIN_LOOP:
>> while (1) {
>> chomp( my $number = );
>> if ( $number =~ /\D/ ) {
>> print "$number is not a numeric value.\n";
>> last STDIN_LOOP;
>> }
>> else {
>> push @numbers, $number;
>> }
>> }
>> [/CODE]



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 00:07:13 von Rob Dixon

On 22/08/2011 10:29, Shlomi Fish wrote:
> Hi Alan,
>
> On Mon, 22 Aug 2011 14:43:48 +0530
> Alan Haggai Alavi wrote:
>
>> Hello Shlomi,
>>
>>> It's a good idea to always use "last LABEL;" instead of "last;" (as w=
ell as
>>> "next LABEL;" etc. in case more loops are added in between.
>>> â‹=AE
>>> http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labe=
ls
>>
>> Now I understand why it is always good to label loops that use `last`,=
`next`
>> or `redo`.
>>
>> Thank you. :-)
>>
>
> Nice. And you're welcome.
>
> For more insights like this, read the rest of the
> http://perl-begin.org/tutorials/bad-elements/ page, and also the book P=
erl Best
> Practices by Damian Conway, from which I borrowed a lot of advice.

This recommendation worries me as, first of all, I can't remember seeing
labels used either in this list (except to exemplify the concept) or in
working code in the field. In my mind it belongs together with the
deprecation of $_ as a measure to defend against otherwise
poorly-written code.

Just as with pronouns in natural language, implicit operands in Perl
allow the meaning of a program to be more lucid. Yes, if we are not
careful we can sometimes write confusing English where it is unclear
what 'it' or 'he' refers to, but the solution is always to restructure
the sentence rather than simply use proper nouns everywhere.

Shlomi, do you honestly give a label to every last, next etc. that you
write in live code?

What worries me more is that, in continually referring to your own web
site, you give your opinion more weight than it deserves. The existence
of the site tells me only that you are interested in the Perl language
and like to write about it. Those unfamiliar with the origin and status
status of your writings will inevitably assume more credibility than
they deserve.

Rob

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 09:32:51 von Shlomi Fish

Hi Rob,

On Wed, 24 Aug 2011 23:07:13 +0100
Rob Dixon wrote:

> On 22/08/2011 10:29, Shlomi Fish wrote:
> > Hi Alan,
> >
> > On Mon, 22 Aug 2011 14:43:48 +0530
> > Alan Haggai Alavi wrote:
> >
> >> Hello Shlomi,
> >>
> >>> It's a good idea to always use "last LABEL;" instead of "last;" (as w=
ell
> >>> as "next LABEL;" etc. in case more loops are added in between.
> >>> â‹=AE
> >>> http://perl-begin.org/tutorials/bad-elements/#flow-stmts-wit hout-labe=
ls
> >>
> >> Now I understand why it is always good to label loops that use `last`,
> >> `next` or `redo`.
> >>
> >> Thank you. :-)
> >>
> >
> > Nice. And you're welcome.
> >
> > For more insights like this, read the rest of the
> > http://perl-begin.org/tutorials/bad-elements/ page, and also the book P=
erl
> > Best Practices by Damian Conway, from which I borrowed a lot of advice.
>=20
> This recommendation worries me as, first of all, I can't remember seeing
> labels used either in this list (except to exemplify the concept) or in
> working code in the field. In my mind it belongs together with the
> deprecation of $_ as a measure to defend against otherwise
> poorly-written code.

I've used such labels for loops in all the posts to this list where I used
"last", "next" or "redo", and for all the code I write in production. It is
also recommended by Perl Best Practices.

I also tend to avoid using "$_" (except for map/grep/etc. where it is
required), because it can be clobbered and devastated too easily, which also
makes depending on it error prone.

>=20
> Just as with pronouns in natural language, implicit operands in Perl
> allow the meaning of a program to be more lucid. Yes, if we are not
> careful we can sometimes write confusing English where it is unclear
> what 'it' or 'he' refers to, but the solution is always to restructure
> the sentence rather than simply use proper nouns everywhere.
>=20

Well, an analogy between English and Perl can only go so far. When writing
code, it is more important to make sure the code is correct and remains
correct than that it would be slightly more lucid. =20

> Shlomi, do you honestly give a label to every last, next etc. that you
> write in live code?

Yes, I do (I practise what I preach). Maybe I sometimes avoid labelling loo=
ps
in some one-off scripts, but all my CPAN code (at least those that I've wri=
tten
since I've read PBP) does not contain implicit flow-control jumps.=20

>=20
> What worries me more is that, in continually referring to your own web
> site, you give your opinion more weight than it deserves. The existence
> of the site tells me only that you are interested in the Perl language
> and like to write about it. Those unfamiliar with the origin and status
> status of your writings will inevitably assume more credibility than
> they deserve.

Well, one of the reasons I've created http://perl-begin.org/ is so I would =
be
able to refer people to the various resources there, in order to educate th=
em.
This is more convenient and more effective than repeating the same wordy
texts times and again. I don't see how my site is any less credible than
other resources you can find online and in printed books, which were written
by human beings just like me. Most of the pages on Perl-Begin mention my na=
me
as the webmaster at the bottom, and I also think the information I'm giving
there is sound, and not misleading in any way.

Also see what I've written previously about it here:

http://www.nntp.perl.org/group/perl.beginners/2011/05/msg117 240.html

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

Satan condemned Hitler for a million years of writing XSLT.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 19:08:55 von merlyn

>>>>> "Shlomi" == Shlomi Fish writes:

Shlomi> I also tend to avoid using "$_" (except for map/grep/etc. where
Shlomi> it is required), because it can be clobbered and devastated too
Shlomi> easily, which also makes depending on it error prone.

Well, that's interesting. I've *never* accidentally clobbered
$_. Nearly everything that uses $_ localizes it immediately, except for
the implicit assignment to $_ during a while-reading-filehandle loop, or
being the target of a s/old/new/ operation, which I consider something
to learn properly in the first few hours of Perl because it happens so
often.

Can you give an example from your old code where you "accidentally"
clobbered $_?

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 19:51:50 von Shlomi Fish

On Thu, 25 Aug 2011 10:08:55 -0700
merlyn@stonehenge.com (Randal L. Schwartz) wrote:

> >>>>> "Shlomi" == Shlomi Fish writes:
>=20
> Shlomi> I also tend to avoid using "$_" (except for map/grep/etc. where
> Shlomi> it is required), because it can be clobbered and devastated too
> Shlomi> easily, which also makes depending on it error prone.
>=20
> Well, that's interesting. I've *never* accidentally clobbered
> $_. Nearly everything that uses $_ localizes it immediately, except for
> the implicit assignment to $_ during a while-reading-filehandle loop, or
> being the target of a s/old/new/ operation, which I consider something
> to learn properly in the first few hours of Perl because it happens so
> often.
>=20
> Can you give an example from your old code where you "accidentally"
> clobbered $_?
>

Well, I believe I've always avoided using an implicit $_ as preventative
measure (out of thinking I know better) and so cannot present such a case
first-hand. However, see:

http://www.forum2.org/gaal/perl/Pitfall/slide001.html - â€=9CA $_ Gotch=
aâ€=9D.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg

The cool thing about Vim is â€=94 you find something interesting with e=
very typo.
â€=94 Suâ€=90Shee on Freenodeâ€=99s #perl .

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 20:41:34 von merlyn

>>>>> "Shlomi" == Shlomi Fish writes:

Shlomi> Well, I believe I've always avoided using an implicit $_ as prevent=
ative
Shlomi> measure (out of thinking I know better) and so cannot present such =
a case
Shlomi> first-hand. However, see:

Shlomi> http://www.forum2.org/gaal/perl/Pitfall/slide001.html - â€=9CA =
$_
Shlomi> Gotchaâ€=9D.

Ahh, so the break was *someone else's* code, not the coder's fault.

At that level, anything can go wrong... we can't code completely
defensively presuming everything else is idiotic.

So I'm still wondering why you have excessive paranoia about *your* code
accidentally breaking $_. How did *you* get burned "accidentally" as
you claim?

If not, please stop suggesting that other people change their behavior
when using $_ *as designed* is a perfectly acceptable practice. It
makes Perl seem scarier than it is. (A lot of those PBPs are like that,
but I won't open that can of worms here.)

print "Just another Perl hacker,"; # the original

--=20
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 25.08.2011 21:36:45 von Shlomi Fish

Hi Randal,

On Thu, 25 Aug 2011 11:41:34 -0700
merlyn@stonehenge.com (Randal L. Schwartz) wrote:

> >>>>> "Shlomi" == Shlomi Fish writes:
>=20
> Shlomi> Well, I believe I've always avoided using an implicit $_ as
> Shlomi> preventative measure (out of thinking I know better) and so cannot
> Shlomi> present such a case first-hand. However, see:
>=20
> Shlomi> http://www.forum2.org/gaal/perl/Pitfall/slide001.html - â€=9C=
A $_
> Shlomi> Gotchaâ€=9D.
>=20
> Ahh, so the break was *someone else's* code, not the coder's fault.
>=20

Well, it affected the coder, because they were dependant on using $_.

> At that level, anything can go wrong... we can't code completely
> defensively presuming everything else is idiotic.

Well, coding defensively is a good idea within reason.=20

> So I'm still wondering why you have excessive paranoia about *your* code
> accidentally breaking $_. How did *you* get burned "accidentally" as
> you claim?
>=20

Where do you see that you claim that I got burned accidentally?

> If not, please stop suggesting that other people change their behavior
> when using $_ *as designed* is a perfectly acceptable practice. It
> makes Perl seem scarier than it is. (A lot of those PBPs are like that,
> but I won't open that can of worms here.)

If you want to use $_ so be it, but it can easily introduce subtle errors i=
nto
your code, because $_ is so easy to modify and clobber. So I would recommend
against these, and still think it's a good idea.

>=20
> print "Just another Perl hacker,"; # the original
>=20

Well, the text here may not be displayed on the shell (or displayed on the =
same
line as the following prompt.). :-)

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
The Case for File Swapping - http://shlom.in/file-swap

COBOL is the old Java.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 02:20:26 von Rob Dixon

On 25/08/2011 20:36, Shlomi Fish wrote:
>
> If you want to use $_ so be it, but it can easily introduce subtle errors into
> your code, because $_ is so easy to modify and clobber. So I would recommend
> against these, and still think it's a good idea.

Please substantiate this assertion. I believe your recommendation
deserves to be put in the bin of ill-conceived dogma.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 19:12:28 von Jim Gibson

On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon"
scribbled:

> On 25/08/2011 20:36, Shlomi Fish wrote:
>>
>> If you want to use $_ so be it, but it can easily introduce subtle errors
>> into
>> your code, because $_ is so easy to modify and clobber. So I would recommend
>> against these, and still think it's a good idea.
>
> Please substantiate this assertion. I believe your recommendation
> deserves to be put in the bin of ill-conceived dogma.

Shlomi gave a link to a case where somebody had to find a subtle error
caused by $_ being clobbered. I would only disagree with Shlomi's use of the
term "easily". I hope we can all agree that $_ can be overwritten
inadvertently, but it does not happen very often.

Nevertheless, beginning Perl programmers should be cautioned that it can
happen, and they should be encouraged to use explicitly-named variables for
complex loops and blocks and especially for any block that calls a
subroutine or system function.

We need to respect the people who post here seeking help. They need to be
told about those Perl issues that are not obvious, such as $_ being
overwritten. They do not need to be told "never use $_ by default because it
can be overwritten". It is up to each programmer to choose whether or not to
use $_ as a default variable and risk it getting clobbered by mistake. They
do need the information on which to base this decision.

They also need to be encouraged to use "best practices". However, what
exactly best practices are is open to debate.



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 19:23:33 von Brandon McCaig

On Wed, Aug 24, 2011 at 6:07 PM, Rob Dixon wrote:
> On 22/08/2011 10:29, Shlomi Fish wrote:
> Just as with pronouns in natural language, implicit operands in Perl
> allow the meaning of a program to be more lucid. Yes, if we are not
> careful we can sometimes write confusing English where it is unclear
> what 'it' or 'he' refers to, but the solution is always to restructure
> the sentence rather than simply use proper nouns everywhere.
>
> Shlomi, do you honestly give a label to every last, next etc. that you
> write in live code?

Personally I think that it's easier to read without the labels. I
think that any programmer that added an inner loop and didn't refactor
the corresponding 'next', 'last', or 'redo' statement should be given
up on. :P Whenever you modify code you can potentially change the
meaning. You need to understand the implications of that. Which is why
small subroutines with limited scope are preferable to big, large,
everything-is-global programs. Personally I /rarely/ encounter nested
loops with any 'next', 'last', or equivalent statements anywhere
within. So I think that always using labels makes the majority of code
more difficult to understand than the few cases where you do want to
jump from an inner loop. If you do find yourself writing code that is
hard to follow then you probably need to modularize it more; _that's_
defensive programming. :)


--
Brandon McCaig
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 21:08:31 von Rob Dixon

On 26/08/2011 18:12, Jim Gibson wrote:
> On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon" >> On 25/08/2011 20:36, Shlomi Fish wrote:
>>>
>>> If you want to use $_ so be it, but it can easily introduce
>>> subtle errors into your code, because $_ is so easy to modify and
>>> clobber. So I would recommend against these, and still think it's
>>> a good idea.
>>
>> Please substantiate this assertion. I believe your recommendation
>> deserves to be put in the bin of ill-conceived dogma.
>
> Shlomi gave a link to a case where somebody had to find a subtle error
> caused by $_ being clobbered. I would only disagree with Shlomi's use of the
> term "easily". I hope we can all agree that $_ can be overwritten
> inadvertently, but it does not happen very often.
>
> Nevertheless, beginning Perl programmers should be cautioned that it can
> happen, and they should be encouraged to use explicitly-named variables for
> complex loops and blocks and especially for any block that calls a
> subroutine or system function.
>
> We need to respect the people who post here seeking help. They need to be
> told about those Perl issues that are not obvious, such as $_ being
> overwritten. They do not need to be told "never use $_ by default because it
> can be overwritten". It is up to each programmer to choose whether or not to
> use $_ as a default variable and risk it getting clobbered by mistake. They
> do need the information on which to base this decision.
>
> They also need to be encouraged to use "best practices". However, what
> exactly best practices are is open to debate.

I believe the fragility of $_ is apocryphal, and Shlomi's insistence on
avoiding it is akin to remembering always to close the fridge door to
keep the elephants out. If nothing else, using named variables
everywhere adds noise to a program and makes it frustrating to read.

As Randal says, $_ is localized everywhere it is used implicitly by a
loop, including foreach, map and grep as well as the List::Util
functions, so I believe that corrupting it is rarely a problem in
practice. The significant exception is while loops, which do not
preserve $_ and so could be said to be prone to unexpected errors.

If I was to set rules, it would be that $_ should never be modified
either explicitly or by the condition in a while loop. The latter most
commonly appears in a read loop

while (<$fh>) {
:
}

which is more safely written as

while (my $record = <$fh>) {
:
}

especially now that the semantics of such a read loop now correctly
check the definedness of the data read, and is equivalent to

while (defined(my $record = <$fh>)) {
print $record;
}

But I would be sad to rule against niceties like

not /^#/ and print for <$fh>;

As an aside, the same applies to the common warning against using the
global values $a and $b 'because they are used by sort'. Once again,
sort localizes these variables within the comparison code so there is
very little chance of inadvertently modifying their values.

Overly-careful warnings can have the opposite of the desired effect,
especially on beginner programmers, and make it seem like the language
is rife with pitfalls and gotchas, especially when these apply to
ubiquitous core concepts like $_. I hope people will think twice about
the ideas that they are conveying.

Cheers all,

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 21:22:43 von Shawn H Corey

On 11-08-26 03:08 PM, Rob Dixon wrote:
> As an aside, the same applies to the common warning against using the
> global values $a and $b 'because they are used by sort'. Once again,
> sort localizes these variables within the comparison code so there is
> very little chance of inadvertently modifying their values.

The warning about $a and $b is not because Perl will get confused, it's
because the people reading it will. And unless it's part of a
well-known mathematical formula, one shouldn't use single-letter
variables. Use a more meaningful name instead.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

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.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 26.08.2011 21:25:58 von Shlomi Fish

On Fri, 26 Aug 2011 20:08:31 +0100
Rob Dixon wrote:

> On 26/08/2011 18:12, Jim Gibson wrote:
> > On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon" > > scribbled:
> >> On 25/08/2011 20:36, Shlomi Fish wrote:
> >>>
> >>> If you want to use $_ so be it, but it can easily introduce
> >>> subtle errors into your code, because $_ is so easy to modify and
> >>> clobber. So I would recommend against these, and still think it's
> >>> a good idea.
> >>
> >> Please substantiate this assertion. I believe your recommendation
> >> deserves to be put in the bin of ill-conceived dogma.
> >
> > Shlomi gave a link to a case where somebody had to find a subtle error
> > caused by $_ being clobbered. I would only disagree with Shlomi's use o=
f the
> > term "easily". I hope we can all agree that $_ can be overwritten
> > inadvertently, but it does not happen very often.
> >
> > Nevertheless, beginning Perl programmers should be cautioned that it can
> > happen, and they should be encouraged to use explicitly-named variables=
for
> > complex loops and blocks and especially for any block that calls a
> > subroutine or system function.
> >
> > We need to respect the people who post here seeking help. They need to =
be
> > told about those Perl issues that are not obvious, such as $_ being
> > overwritten. They do not need to be told "never use $_ by default becau=
se it
> > can be overwritten". It is up to each programmer to choose whether or n=
ot to
> > use $_ as a default variable and risk it getting clobbered by mistake. =
They
> > do need the information on which to base this decision.
> >
> > They also need to be encouraged to use "best practices". However, what
> > exactly best practices are is open to debate.
>=20
> I believe the fragility of $_ is apocryphal, and Shlomi's insistence on
> avoiding it is akin to remembering always to close the fridge door to
> keep the elephants out. If nothing else, using named variables
> everywhere adds noise to a program and makes it frustrating to read.
>=20
> As Randal says, $_ is localized everywhere it is used implicitly by a
> loop, including foreach, map and grep as well as the List::Util
> functions, so I believe that corrupting it is rarely a problem in
> practice. The significant exception is while loops, which do not
> preserve $_ and so could be said to be prone to unexpected errors.
>=20
> If I was to set rules, it would be that $_ should never be modified
> either explicitly or by the condition in a while loop. The latter most
> commonly appears in a read loop
>=20
> while (<$fh>) {
> :
> }
>=20
> which is more safely written as
>=20
> while (my $record =3D <$fh>) {
> :
> }
>=20
> especially now that the semantics of such a read loop now correctly
> check the definedness of the data read, and is equivalent to
>=20
> while (defined(my $record =3D <$fh>)) {
> print $record;
> }
>=20
> But I would be sad to rule against niceties like
>=20
> not /^#/ and print for <$fh>;
>=20
> As an aside, the same applies to the common warning against using the
> global values $a and $b 'because they are used by sort'. Once again,
> sort localizes these variables within the comparison code so there is
> very little chance of inadvertently modifying their values.
>=20

The problem starts to happen when you try to declare $a and $b using my. Th=
is
program:

[CODE]
#!/usr/bin/perl

use strict;
use warnings;

my $a =3D 5;
my $b =3D 6;

print map { "$_\n" } sort { $a <=3D> $b } (9,100,5,6,70,3,4,98,28,27);
[/CODE]

Yields this error:

[CODE]
Can't use "my $a" in sort comparison at test.pl line 9.
[/CODE]

This is perl-5.14.1 - previous versions of Perl may behave more
erratically when executing this. $a and $b are built-ins plain and simple, =
and
should be treated as such, due to their use by perldoc -f sort and other
functions from List::Util, List::MoreUtils, etc.

Another reason not to use them except for those cases is because "a" and "b"
are not very meaningful and indicative identifiers.

Regards,

Shlomi Fish

> Overly-careful warnings can have the opposite of the desired effect,
> especially on beginner programmers, and make it seem like the language
> is rife with pitfalls and gotchas, especially when these apply to
> ubiquitous core concepts like $_. I hope people will think twice about
> the ideas that they are conveying.
>=20
> Cheers all,
>=20
> Rob
>=20



--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl

Sophie: Letâ€=99s suppose you have a table with 2^n cupsâ€=A6
Jack: Wait a second! Is â€=98nâ€=99 a natural number?

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 27.08.2011 11:56:19 von Rob Dixon

On 26/08/2011 18:12, Jim Gibson wrote:
> On 8/25/11 Thu Aug 25, 2011 5:20 PM, "Rob Dixon"
> scribbled:
>
>> On 25/08/2011 20:36, Shlomi Fish wrote:
>>>
>>> If you want to use $_ so be it, but it can easily introduce subtle errors
>>> into
>>> your code, because $_ is so easy to modify and clobber. So I would recommend
>>> against these, and still think it's a good idea.
>>
>> Please substantiate this assertion. I believe your recommendation
>> deserves to be put in the bin of ill-conceived dogma.
>
> Shlomi gave a link to a case where somebody had to find a subtle error
> caused by $_ being clobbered. I would only disagree with Shlomi's use of the
> term "easily". I hope we can all agree that $_ can be overwritten
> inadvertently, but it does not happen very often.
>
> Nevertheless, beginning Perl programmers should be cautioned that it can
> happen, and they should be encouraged to use explicitly-named variables for
> complex loops and blocks and especially for any block that calls a
> subroutine or system function.
>
> We need to respect the people who post here seeking help. They need to be
> told about those Perl issues that are not obvious, such as $_ being
> overwritten. They do not need to be told "never use $_ by default because it
> can be overwritten". It is up to each programmer to choose whether or not to
> use $_ as a default variable and risk it getting clobbered by mistake. They
> do need the information on which to base this decision.
>
> They also need to be encouraged to use "best practices". However, what
> exactly best practices are is open to debate.
>
>
>


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 27.08.2011 19:39:57 von derykus

On Aug 26, 12:25=A0pm, shlo...@shlomifish.org (Shlomi Fish) wrote:
> On Fri, 26 Aug 2011 20:08:31 +0100
>
> ...
>
> The problem starts to happen when you try to declare $a and $b using my. =
This
> program:
>
> [CODE]
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $a =3D 5;
> my $b =3D 6;
>
> print map { "$_\n" } sort { $a <=3D> $b } (9,100,5,6,70,3,4,98,28,27);
> [/CODE]
>
> Yields this error:
>
> [CODE]
> Can't use "my $a" in sort comparison at test.pl line 9.
> [/CODE]
>
> This is perl-5.14.1 - previous versions of Perl may behave more
> erratically when executing this. $a and $b are built-ins plain and simple=
, and
> should be treated as such, due to their use by perldoc -f sort and other
> functions from List::Util, List::MoreUtils, etc.
>
> Another reason not to use them except for those cases is because "a" and =
"b"
> are not very meaningful and indicative identifiers.
>

Their use elsewhere should definitely ring a cautionary
bell. Here 'use diagnostics qw/-verbose/ explains the
problem well:

erbose diagnostic



> > Overly-careful warnings can have the opposite of the desired effect,
> > especially on beginner programmers, and make it seem like the language
> > is rife with pitfalls and gotchas, especially when these apply to
> > ubiquitous core concepts like $_. I hope people will think twice about
> > the ideas that they are conveying.
>
> > Cheers all,
>
> > Rob
>
> --
> ------------------------------------------------------------ -----
> Shlomi Fish =A0 =A0 =A0http://www.shlomifish.org/
> Why I Love Perl -http://shlom.in/joy-of-perl
>
> Sophie: Let=92s suppose you have a table with 2^n cups=85
> Jack: Wait a second! Is =91n=92 a natural number?
>
> Please reply to list if it's a mailing list post -http://shlom.in/reply.


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 27.08.2011 20:01:15 von derykus

On Aug 26, 12:25=A0pm, shlo...@shlomifish.org (Shlomi Fish) wrote:
> ...
> The problem starts to happen when you try to declare $a and $b using my. =
This
> program:
>
> [CODE]
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $a =3D 5;
> my $b =3D 6;
>
> print map { "$_\n" } sort { $a <=3D> $b } (9,100,5,6,70,3,4,98,28,27);
> [/CODE]
>
> Yields this error:
>
> [CODE]
> Can't use "my $a" in sort comparison at test.pl line 9.
> [/CODE]
>
> This is perl-5.14.1 - previous versions of Perl may behave more
> erratically when executing this. $a and $b are built-ins plain and simple=
, and
> should be treated as such, due to their use by perldoc -f sort and other
> functions from List::Util, List::MoreUtils, etc.

The use of $a,$b elsewhere should definitely ring an
alarm. Remembering: "use diagnostics qw/-verbose/
though makes the problem/solution very clear:

(F) The global variables $a and $b are reserved for sort
comparisons. You mentioned $a or $b in the same
line as the <=3D> or cmp operator, and the variable had
earlier been declared as a lexical variable. Either
qualify the sort variable with the package name, or
rename the lexical variable.

Rob's follow-on remarks:
------------------------------
> > Overly-careful warnings can have the opposite of the
desired effect,
> > especially on beginner programmers, and make it seem
like the language
> > is rife with pitfalls and gotchas, especially when these
apply to
> > ubiquitous core concepts like $_. I hope people will think
twice about
> > the ideas that they are conveying.

I tend to agree here that a cautionary -- rather
than dogmatic -- tone is the better choice.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 27.08.2011 20:16:50 von rvtol+usenet

On 2011-08-26 21:08, Rob Dixon wrote:

> But I would be sad to rule against niceties like
>
> not /^#/ and print for <$fh>;

Which can also be written as

/^#/ or print for <$fh>;


I would like 'lazy' syntax like:

<$fh> x {/^#/ or print};

to process a list.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 28.08.2011 10:45:15 von rvtol+usenet

On 2011-08-26 19:23, Brandon McCaig wrote:

> Personally I think that it's easier to read without the labels. I
> think that any programmer that added an inner loop and didn't refactor
> the corresponding 'next', 'last', or 'redo' statement should be given
> up on. :P

> Whenever you modify code you can potentially change the
> meaning. You need to understand the implications of that. Which is why
> small subroutines with limited scope are preferable to big, large,
> everything-is-global programs.

> Personally I /rarely/ encounter nested
> loops with any 'next', 'last', or equivalent statements anywhere
> within. So I think that always using labels makes the majority of code
> more difficult to understand than the few cases where you do want to
> jump from an inner loop.

> If you do find yourself writing code that is
> hard to follow then you probably need to modularize it more; _that's_
> defensive programming. :)

Hear, hear. But to get properly read, use paragraphs, with a blank line
in between. :)

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 28.08.2011 20:48:19 von Mike McClain

On Sat, Aug 27, 2011 at 08:16:50PM +0200, Dr.Ruud wrote:
> I would like 'lazy' syntax like:
>
> <$fh> x {/^#/ or print};
>
> to process a list.

When I tried your code like so:
open my $fh, '<', $file or die qq(Unable to open $file: $! );
<$fh> x {/^\s*#/ or print};
close $fh or die qq(Unable to close $file: $! );

I get these errors:
Useless use of repeat (x) in void context at ./test line 48.
Use of uninitialized value in pattern match (m//) at ./test line 48,
<$fh> line 1.
Use of uninitialized value in print at ./test line 48, <$fh> line 1.
Odd number of elements in anonymous hash at ./test line 48, <$fh> line 1.
Out of memory!

I'm using Perl 5.8.8.
How can that line be used?
Thanks,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 29.08.2011 16:27:52 von Rob Dixon

On 28/08/2011 19:48, Mike McClain wrote:
> On Sat, Aug 27, 2011 at 08:16:50PM +0200, Dr.Ruud wrote:
>> I would like 'lazy' syntax like:
>>
>> <$fh> x {/^#/ or print};
>>
>> to process a list.
>
> When I tried your code like so:
> open my $fh, '<', $file or die qq(Unable to open $file: $! );
> <$fh> x {/^\s*#/ or print};
> close $fh or die qq(Unable to close $file: $! );
>
> I get these errors:
> Useless use of repeat (x) in void context at ./test line 48.
> Use of uninitialized value in pattern match (m//) at ./test line 48,
> <$fh> line 1.
> Use of uninitialized value in print at ./test line 48,<$fh> line 1.
> Odd number of elements in anonymous hash at ./test line 48,<$fh> line 1.
> Out of memory!

Hey Mike. Ruud was just proposing a "wouldn't it be nice if" syntax. It
isn't valid Perl!

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: loop break condition

am 30.08.2011 17:32:48 von Mike McClain

On Mon, Aug 29, 2011 at 03:27:52PM +0100, Rob Dixon wrote:
>
> Hey Mike. Ruud was just proposing a "wouldn't it be nice if" syntax. It
> isn't valid Perl!
>
> Rob

Well that explains the errors.
I've gotten so many good ideas from his posts it didn't dawn on me
to doubt the validity of this one. Just figured I'd misapplied it.

Thanks for the elucidation,
Mike (with egg on my face again)
--
Unregistered Linutik since 1997.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/