[32872] in Perl-Users-Digest
Perl-Users Digest, Issue: 4150 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Feb 24 18:09:47 2014
Date: Mon, 24 Feb 2014 15:09:12 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Mon, 24 Feb 2014 Volume: 11 Number: 4150
Today's topics:
Re: cookie <john@castleamber.com>
Re: cookie <gravitalsun@hotmail.foo>
Re: cookie <gravitalsun@hotmail.foo>
Re: cookie <john@castleamber.com>
Re: cookie <sbryce@scottbryce.com>
Re: cookie <sbryce@scottbryce.com>
Re: cookie <sbryce@scottbryce.com>
Re: cookie <gravitalsun@hotmail.foo>
Re: non-working perl script under Windows 2008 <gamo@telecable.es>
Re: non-working perl script under Windows 2008 <janek_schleicher@yahoo.de>
Re: obj 8-2 <johnblack@nospam.com>
Re: obj 8-2 <rweikusat@mobileactivedefense.com>
Re: obj 8-2 <mach2@hushmail.com>
Re: obj 8-2 <johnblack@nospam.com>
Re: obj 8-2 <johnblack@nospam.com>
Re: obj 8-2 <kaz@kylheku.com>
Re: obj 8-2 <rweikusat@mobileactivedefense.com>
Re: obj 8-2 <kst-u@mib.org>
Re: obj 8-2 <rweikusat@mobileactivedefense.com>
Re: use strict; use warnings; <mach2@hushmail.com>
Re: use strict; use warnings; <janek_schleicher@yahoo.de>
Re: use strict; use warnings; <john@castleamber.com>
Re: use strict; use warnings; <mach2@hushmail.com>
Re: use strict; use warnings; <john@castleamber.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 24 Feb 2014 13:10:40 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: cookie
Message-Id: <87fvn872dr.fsf@castleamber.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
> Στις 24/2/2014 21:01, ο/η Scott Bryce έγραψε:
>> On 2/24/2014 11:06 AM, George Mpouras wrote:
>>> I want to send a cookie to users’ browser (using the CGI) after
>>> evaluating a form. The problem is that you can send a cookie only at
>>> the header and the forms can be only after start_html . any idea ?
>>
>>
>> This isn't a Perl question, but...
>>
>> Unless you are evaluating the form input on the client side, sending the
>> form and sending the cookie happen during two different transactions, so
>> there is no problem.
>>
>> 1) Send the form.
>> 2) Receive the user input.
>> 3) Evaluate the user input and send the cookie with the response.
>
>
> The only Perl question is how to write it in C better and compile it.
> Back to the subject now, you can not do what you said using the CGI
> from the same script. I am thinking of an ugly workaround but I 'ld
> like to avoid it.
1) is done by the user's browser.
You really mean you want to do all 3 from the same script?
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
Date: Mon, 24 Feb 2014 21:15:08 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: cookie
Message-Id: <leg5nu$2a46$1@news.ntua.gr>
Στις 24/2/2014 21:04, ο/η Bjoern Hoehrmann έγραψε:
> * George Mpouras wrote in comp.lang.perl.misc:
>> I want to send a cookie to users’ browser (using the CGI) after
>> evaluating a form. The problem is that you can send a cookie only at the
>> header and the forms can be only after start_html . any idea ?
>
> The Cookie either has to be set from the `<form action='...'>` or has to
> be set dynamically e.g. using JavaScript.
>
Sending the cookie from <form action ...> sounds interesting but I do
not think it is supported from Perl module CGI.
I want to process user selections before sending the cookie.
What I am thinking is at form action, to POST everything to an other
page (a script actually) which also send the cookie.
The ideal would be from the same script
submit form -> process user selections -> send the cookie
------------------------------
Date: Mon, 24 Feb 2014 21:16:37 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: cookie
Message-Id: <leg5qn$2a46$2@news.ntua.gr>
> 1) is done by the user's browser.
>
> You really mean you want to do all 3 from the same script?
>
that was the idea ...
------------------------------
Date: Mon, 24 Feb 2014 13:57:54 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: cookie
Message-Id: <87eh2si8ql.fsf@castleamber.com>
George Mpouras <gravitalsun@hotmail.foo> writes:
>> 1) is done by the user's browser.
>>
>> You really mean you want to do all 3 from the same script?
>>
>
> that was the idea ...
I think you want:
user/browser CGI
requests URL ------------------>
<------------------------------- page with FORM
fills in form
POSTs form -------------------->
analyzes data received
<------------------------------ cookie + HTML (if data OK)
?
If you want to fill in a form on someone else's site you can use
something like LWP::UserAgent. This is complicated (dealing processing
that can takes time, etc.) but not impossible.
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
Date: Mon, 24 Feb 2014 14:39:51 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: cookie
Message-Id: <530BBC27.2010604@scottbryce.com>
On 2/24/2014 12:10 PM, John Bokma wrote:
> George Mpouras <gravitalsun@hotmail.foo> writes:
>
>> Στις 24/2/2014 21:01, ο/η Scott Bryce έγραψε:
>>> 1) Send the form. 2) Receive the user input. 3) Evaluate the user
>>> input and send the cookie with the response.
> 1) is done by the user's browser.
I was thinking of sending the web page containing the form to the
browser in the first place before the user sees it and fills it out. The
OP seems to think he has to send the cookie at the same time.
------------------------------
Date: Mon, 24 Feb 2014 14:42:24 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: cookie
Message-Id: <legec5$vpl$2@dont-email.me>
On 2/24/2014 12:15 PM, George Mpouras wrote:
> Sending the cookie from <form action ...> sounds interesting but I do
> not think it is supported from Perl module CGI.
You do understand, don't you, that not all CGI functions have to use the
CGI module? Many people, myself included, use the CGI module to parse
the data coming to the script, but do no use it for anything that is
sent back to the browser.
------------------------------
Date: Mon, 24 Feb 2014 14:52:10 -0700
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: cookie
Message-Id: <legeuf$4iq$1@dont-email.me>
On 2/24/2014 12:16 PM, George Mpouras wrote:
>> You really mean you want to do all 3 from the same script?
>>
>
> that was the idea ...
So the script needs some way to determine whether it should be
processing input from your form, or sending the HTML containing the form
to the browser in the first place. A hidden input field can be used to
identify the form.
Parse the data input that is sent to the script.
If it does not appear to have been sent from the form, send the HTML
page containing the form.
If it does appear to have been sent from the form, validate the data.
If the data validates, process the data, and send a response along with
your cookie.
If the data does not validate, send an error message back to the browser
along with the form with the user's data pre-filled in.
And remember that the data being sent to the script could be coming from
anywhere and contain anything. It does not have to be coming from your
form. You want to look at it pretty carefully before you assume that it
is safe to process it.
------------------------------
Date: Tue, 25 Feb 2014 01:02:20 +0200
From: George Mpouras <gravitalsun@hotmail.foo>
Subject: Re: cookie
Message-Id: <legj1u$a4j$1@news.ntua.gr>
Στις 24/2/2014 23:39, ο/η Scott Bryce έγραψε:
> On 2/24/2014 12:10 PM, John Bokma wrote:
>> George Mpouras <gravitalsun@hotmail.foo> writes:
>>
>>> Στις 24/2/2014 21:01, ο/η Scott Bryce έγραψε:
>>>> 1) Send the form. 2) Receive the user input. 3) Evaluate the user
>>>> input and send the cookie with the response.
>
>> 1) is done by the user's browser.
>
>
> I was thinking of sending the web page containing the form to the
> browser in the first place before the user sees it and fills it out. The
> OP seems to think he has to send the cookie at the same time.
>
well here is the flow.
1) at the very start (before the header) I check if the user have the
cookie and if contain a valid data. In this case the user is redirected
immediately to the "final" page
2) In case there is no cookie, or if the cookie data are not valid, The
user must fill a form.
If the form data validated successfully, then a cookie must be sent at
user's browser , and redirected to the final page (here is the
difficulty is to send the cookie after the submission and validation)
thats all.
------------------------------
Date: Mon, 24 Feb 2014 21:16:46 +0100
From: gamo <gamo@telecable.es>
Subject: Re: non-working perl script under Windows 2008
Message-Id: <leg9bc$j2h$1@speranza.aioe.org>
El 24/02/14 13:16, dr.mtarver@gmail.com escribi:
> <p>Hello World</p>
> </body>
> <//html>
^^ this is a error
Good luck
--
http://www.telecable.es/personales/gamo/
------------------------------
Date: Mon, 24 Feb 2014 21:23:05 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: non-working perl script under Windows 2008
Message-Id: <bn1o1fFpldpU1@mid.individual.net>
Am 24.02.2014 21:16, schrieb gamo:
> El 24/02/14 13:16, dr.mtarver@gmail.com escribi:
>> <p>Hello World</p>
>> </body>
>> <//html>
> ^^ this is a error
that every Browser could handle :-)
Greetings,
Janek
------------------------------
Date: Mon, 24 Feb 2014 13:30:52 -0600
From: John Black <johnblack@nospam.com>
Subject: Re: obj 8-2
Message-Id: <MPG.2d7559e26a572d899897b7@news.eternal-september.org>
In article <NvGdnSjrEcjiFpbOnZ2dnUVZ_rKdnZ2d@supernews.com>, mach2@hushmail.com says...
> My first computer was a Timex Sinclair 1000. Then a VIC20 and then a
> Commodore 64. I used that Commie for 5 years straight. Loved it! Actually
> I really kind of miss it. I then got into the Amiga when that released.
> Would love to find a used Amiga 3000 on ebay for the hell of it.
> Commodore really had some great stuff for us kids at the time.
Commodore was way ahead of its time which is why the games you could play (I admit, they were
a big reason I chose C64) were better than any contemporary dedicated game console games and
rivaled real arcade games (surpassed in some cases).
John Black
------------------------------
Date: Mon, 24 Feb 2014 19:45:01 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: obj 8-2
Message-Id: <874n3o70si.fsf@sable.mobileactivedefense.com>
"Peter J. Holzer" <hjp-usenet3@hjp.at> writes:
> On 2014-02-22 21:05, $Bill <news@todbe.com> wrote:
>> On 2/22/2014 12:39, John Bokma wrote:
>>>
>>> So you don't recommend:
>>>
>>> open my $fh, '<', $filename
>>> or die "Can't open '$filename' for reading: $!";
>>>
>
> Actually I would recommend autodie here.
>
> But I think this is different in several aspects.
>
> 1) In the open or die idiom, the open is the important part, while
> the die is just error handling, so it is fitting that the die is
> hidden at the end of the line (or a continuation line in this case).
> But in $_ > $max and $max = $_ the assignment is central and should
> not be hidden.
It actually isn't (or "isn't really"): The 'central' part is the test
which is performed for each item.
>
> 2) open or die conveys the proper meaning as an English sentence,
> but $_ > $max and $max = $_ doesn't. In English and is used to
> combine similar things, so after reading $_ > $max and one would
> expect another comparison, not an assignment. Or vice versa, both
> subexpressions could do something and the whole statement could
> execute both (something like the comma operator).
Picking a few random sentences from the first English book I could lay
my hands on:
Grey drew a telegram from his coat pocket and handed it to Lean.
His guide led him into Yengee Lee's opium den and pointed
toward a vague figure huddled near one corner of the room.
The air was still and twenty degrees hotter than outside so he
hang his coat on a nail and losened his tie.
The first two are of the form 'X happened. Because of X, Y happened
afterwards', that is X had to happen in order to enable Y. The 3rd joins
to different properties of the same thing (the air), the 4th just states
that X happened after Y, although both are independent.
> In Perl it means something completely different, so the similarity to
> an English sentence is distracting (I don't have this problem with
> the && operator).
Has a short, dark-haired man been renting a room thereabouts in
the past week or two? And if so, was he the type who kept
strange hours? And did he pay in advance through at least
Sunday, then disappear with no forwarding address?
And this is a set of distinct conditions joined by as obvious 'a boolean
and operator' as there could possibly be. The main difference is not the
incompatible use of 'and' but that "Perl expressions" (like C
expressions and unlike some other languages, IIRC, Pascal, where
expressions return values and statements cause effects) can have side
effects while "English expressions" obviously can't.
Regardless of that, 'and' in Perl is something other than 'and' in
English and specifically, it is semantically equivalent to && except
that it has a lower precedence which means that
$x & 1 and $b = 3
is equivalent to
($x & 1) && ($b = 3)
while
$x & 1 && $b = 2
would mean
($x & 1 && $b) = 3
which is an error, IOW, one of the main characteristics of the 'and'
operator is that it can be used in this way.
------------------------------
Date: Mon, 24 Feb 2014 13:59:49 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: obj 8-2
Message-Id: <NvGdnSrrEcgoOZbOnZ2dnUVZ_rKdnZ2d@supernews.com>
On Mon, 24 Feb 2014 13:30:52 -0600, John Black wrote:
> In article <NvGdnSjrEcjiFpbOnZ2dnUVZ_rKdnZ2d@supernews.com>,
> mach2@hushmail.com says...
>> My first computer was a Timex Sinclair 1000. Then a VIC20 and then a
>> Commodore 64. I used that Commie for 5 years straight. Loved it!
>> Actually I really kind of miss it. I then got into the Amiga when that
>> released. Would love to find a used Amiga 3000 on ebay for the hell of
>> it. Commodore really had some great stuff for us kids at the time.
>
> Commodore was way ahead of its time which is why the games you could
> play (I admit, they were a big reason I chose C64) were better than any
> contemporary dedicated game console games and rivaled real arcade games
> (surpassed in some cases).
>
> John Black
What sticks out in my head is that I was I think 13 or 14 years old when
I bought a Commodore 64. I worked for the cash and $200 back then was
worth a lot more than it is now. I remember buying Donkey Kong from
AtariSoft for $44. $44 of 30 years ago $44. Worth every penny. It had all
the levels including the Pie Factory which I don't even see on today's
Nintendo devices and it's their game!
Would love to play the full Donkey Kong again. Worth finding a used
Commodore 64 just for that alone.
--
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org
------------------------------
Date: Mon, 24 Feb 2014 15:16:42 -0600
From: John Black <johnblack@nospam.com>
Subject: Re: obj 8-2
Message-Id: <MPG.2d7572b252a2294f9897b8@news.eternal-september.org>
In article <NvGdnSrrEcgoOZbOnZ2dnUVZ_rKdnZ2d@supernews.com>, mach2@hushmail.com says...
>
> On Mon, 24 Feb 2014 13:30:52 -0600, John Black wrote:
>
> > In article <NvGdnSjrEcjiFpbOnZ2dnUVZ_rKdnZ2d@supernews.com>,
> > mach2@hushmail.com says...
> >> My first computer was a Timex Sinclair 1000. Then a VIC20 and then a
> >> Commodore 64. I used that Commie for 5 years straight. Loved it!
> >> Actually I really kind of miss it. I then got into the Amiga when that
> >> released. Would love to find a used Amiga 3000 on ebay for the hell of
> >> it. Commodore really had some great stuff for us kids at the time.
> >
> > Commodore was way ahead of its time which is why the games you could
> > play (I admit, they were a big reason I chose C64) were better than any
> > contemporary dedicated game console games and rivaled real arcade games
> > (surpassed in some cases).
> >
> > John Black
>
> What sticks out in my head is that I was I think 13 or 14 years old when
> I bought a Commodore 64. I worked for the cash and $200 back then was
> worth a lot more than it is now. I remember buying Donkey Kong from
> AtariSoft for $44. $44 of 30 years ago $44. Worth every penny. It had all
> the levels including the Pie Factory which I don't even see on today's
> Nintendo devices and it's their game!
>
> Would love to play the full Donkey Kong again. Worth finding a used
> Commodore 64 just for that alone.
I loved that game too on C64. This was one game someone figured out you could insert a straw
into the side of the arcade machine and rack up free credits. I hate to admit it but I got
some free games that way. Until the Beverage store owner figured out there was no money in
the machine that we'd been playing on all week after middle school.
Any Donkey Kong fan must see the movie "King of Kong". Do not miss.
John Black
------------------------------
Date: Mon, 24 Feb 2014 15:28:33 -0600
From: John Black <johnblack@nospam.com>
Subject: Re: obj 8-2
Message-Id: <MPG.2d75757e741b2c979897b9@news.eternal-september.org>
In article <NvGdnSrrEcgoOZbOnZ2dnUVZ_rKdnZ2d@supernews.com>, mach2@hushmail.com says...
> Would love to play the full Donkey Kong again. Worth finding a used
> Commodore 64 just for that alone.
No need! There are C64 emulators for Windows that look and sound exactly like the real
machine. You can pretty much get all the old games. The emulator runs the binaries of the
games so they are guaranteed to behave identically to the real thing.
John Black
------------------------------
Date: Mon, 24 Feb 2014 21:40:38 +0000 (UTC)
From: Kaz Kylheku <kaz@kylheku.com>
Subject: Re: obj 8-2
Message-Id: <20140224133105.964@kylheku.com>
On 2014-02-22, Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
> 2) “open or die” conveys the proper meaning as an English sentence,
> but “$_ > $max and $max = $_” doesn't.
Who cares. Perl is the among the last things in programming that looks
like English. If you want Cobol, that's down the hall to the left.
And, incidentally, you're not quite right there.
There is a logical sequencing use of the conjuction of "and" in English that is
similar to "do or die".
For instance:
"Try it and die!"
-> if (try_it()) { die() }.
"Write code like that, and you won't understand it in six months time".
Where you are right is that these grammatical constructs do not take an
imperative on the right hand side. For that, we need some other conjunction
that is related to and, like "and so".
"Foo is greater than max, {*and | and so | therefore | ... } store max in it."
------------------------------
Date: Mon, 24 Feb 2014 22:09:10 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: obj 8-2
Message-Id: <87vbw45fjt.fsf@sable.mobileactivedefense.com>
Kaz Kylheku <kaz@kylheku.com> writes:
> On 2014-02-22, Peter J. Holzer <hjp-usenet3@hjp.at> wrote:
>> 2) open or die conveys the proper meaning as an English sentence,
>> but $_ > $max and $max = $_ doesn't.
[...]
> There is a logical sequencing use of the conjuction of "and" in English that is
> similar to "do or die".
[...]
> Where you are right is that these grammatical constructs do not take an
> imperative on the right hand side.
There is no such thing as 'an imperative' as grammatical construction in
Perl. Just expressions which do or don't cause side-effects. I'd also
consider
Go find the key and open the door
Shut up and try to behave youself
as having an imperative on both sides ...
------------------------------
Date: Mon, 24 Feb 2014 14:19:07 -0800
From: Keith Thompson <kst-u@mib.org>
Subject: Re: obj 8-2
Message-Id: <lnha7o6tno.fsf@nuthaus.mib.org>
"Dave Saville" <dave@invalid.invalid> writes:
> On Sat, 22 Feb 2014 15:57:57 UTC, Marek Novotny <mach2@hushmail.com>
> wrote:
>>>
>> What is so different about these two lines of code that made that
>> difference?
>>
>> my $answer = 0;
>> my $answer = $array[0];
>
> In the first case you are setting $answer to an arbitrary value which,
> if you are looking for a maximum, needs to be *smaller* than anything
> in the data to be searched. 0 works for all positive numbers as all
> positive numbers are bigger than 0. But for negative numbers 0 is
> *bigger* than all of them - even if there is no 0 in the list as you
> found by removing it.
>
> In the second case you are starting with a value you *know* is
> actually in the list. It matters not what that value is - just that it
> is from the list. You could be really retentive and only search from
> the second [1] element onwards, but its hardly worth the bother to
> save one compare and an index increment.
And that still misses another corner case, namely an empty list.
Which raises the question: what is the maximum value of an empty list?
Logically there is none, but in Perl undef is probably a reasonable
answer -- which means that anything that uses this code will either have
to be prepared to handle a result of undef, or will need to guarantee
that the list is never empty.
You could do it like this:
my $answer = undef;
foreach my $elem (@array) {
if (not defined $answer or $elem > $answer) {
$answer = $elem;
}
}
but that means checking whether $answer is defined on each iteration,
which seems wasteful. This is probably better:
my $answer = undef;
if (scalar @array > 0) {
$answer = $array[0];
foreach my $elem (@array) {
if ($elem > $answer) {
$answer = $elem;
}
}
}
Some might prefer to write "if (scalar @array > 0)" as "if (@array)".
(Warning: I think the above code is correct, but I haven't tested it.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Mon, 24 Feb 2014 22:27:08 +0000
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: obj 8-2
Message-Id: <87r46s5epv.fsf@sable.mobileactivedefense.com>
Keith Thompson <kst-u@mib.org> writes:
> "Dave Saville" <dave@invalid.invalid> writes:
[...]
>> In the second case you are starting with a value you *know* is
>> actually in the list. It matters not what that value is - just that it
>> is from the list. You could be really retentive and only search from
>> the second [1] element onwards, but its hardly worth the bother to
>> save one compare and an index increment.
>
> And that still misses another corner case, namely an empty list.
>
> Which raises the question: what is the maximum value of an empty list?
> Logically there is none, but in Perl undef is probably a reasonable
> answer -- which means that anything that uses this code will either have
> to be prepared to handle a result of undef, or will need to guarantee
> that the list is never empty.
>
> You could do it like this:
>
> my $answer = undef;
> foreach my $elem (@array) {
> if (not defined $answer or $elem > $answer) {
> $answer = $elem;
> }
> }
There's no need to special-case this. Assuming a function defined as
sub max
{
my $m;
$m = shift;
$_ > $m and $m = $_ for @_;
return $m;
}
or
sub max
{
my $m;
$m = $_[0];
$_ > $m and $m = $_ for @_[1 .. $#_];
return $m;
}
empty lists will be handled as desired (Since the list is empty,
attempts to access its first element will return undef and a loop
iterating over all elements or all remaining elements won't do
anything).
------------------------------
Date: Mon, 24 Feb 2014 13:50:03 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: use strict; use warnings;
Message-Id: <NvGdnSvrEcj2P5bOnZ2dnUVZ_rKdnZ2d@supernews.com>
On Mon, 24 Feb 2014 18:12:57 +0000, Ben Morrow wrote:
> Quoth Marek Novotny <mach2@hushmail.com>:
>>
>>
> Since you're learning, I will give you some general advice on good Perl
> practice.
>
>> # declared variables
>>
>> my @startingdeck = 0;
>> my @hearts = 0;
>> my @diamonds = 0;
>> my @spades = 0;
>> my @clubs = 0;
>> my @sorted = 0;
>> my $i = 0;
>> my $ace = 0;
>
> This sort of predeclaration is not good style in Perl, especially with
> those initial values. You should in general not declare a variable until
> you need it; usually this means you declare it at the point where you
> have a sensible value to put in it, though sometimes you need to declare
> a variable a little earlier than that.
>
> 0 is not a good initial value for a Perl variable. This is particularly
> true of arrays (and hashes, when you get to them); a statement like
>
> my @startingdeck = 0;
>
> creates the array @startingdeck containing a single element with the
> value 0, whereas what you want is an empty array. This can be created
> with
>
> my @startingdeck;
>
> though, as I said before, it's better style to put the right value in is
> straight away, like
>
> my @startingdeck = ("A H", "2 H", ...);
>
> A statement like
>
> my $i = 0;
>
> is less obviously wrong, but the number 0 is not quite 'neutral' in
> Perl: if this variable is to be used as a string it now contains a
> non-empty string, which is unhelpful. Perl has a special value called
> 'undef' which numifies as 0 and stringifies as the empty string; a
> statement like
>
> my $i;
>
> leaves $i containing undef, which is a good initial value if you haven't
> got anything better.
>
>> $i = 0;
>> while ($i < 13){
>> $hearts[$i] = shift(@startingdeck);
>> $i++;
>> }
>
> It's already been pointed out that this sort of loop can be better
> written as the C-style for loop
>
> for (my $i = 0; $i < 13; $i++) {
> $hearts[$i] = shift @startingdeck;
> }
>
> This has the advantage that the variable $i doesn't exist outside the
> loop, so there is no chance that you will pick it up by mistake later.
>
> However, explicitly iterating over the indices of an array is not
> usually necessary in Perl. As I said xthread, Perl arrays know how long
> they are, so you can write this loop
>
> my @hearts;
> while (@hearts < 13) {
> push @hearts, shift @startingdeck;
> }
>
> (This is one of the cases where you do need to declare a variable before
> you use it, because declaring it inside the loop would not have the
> desired effect. In fact, the normal Perl way to write this would be to
> use the splice function, like this:
>
> my @hearts = splice @startingdeck, 0, 13;
>
> which grabs all 13 elements in one go without the need for a loop.)
>
>> $i = 0;
>
> This is bad practice. This use of $i is logically distinct from the use
> you made of it in the previous loop, so it should be a different
> variable. With a C-style for loop as above you would get a different
> variable (which happened to also be called $i) every time.
>
> Ben
Thank you, Ben. While I could not use splice for this lesson I will
implement everything else.
--
Marek Novotny
A member of the Linux Foundation
http://www.linuxfoundation.org
------------------------------
Date: Mon, 24 Feb 2014 21:07:10 +0100
From: Janek Schleicher <janek_schleicher@yahoo.de>
Subject: Re: use strict; use warnings;
Message-Id: <bn1n3mFpfd2U1@mid.individual.net>
Am 24.02.2014 18:50, schrieb Marek Novotny:
> That they did cover. For whatever reason I like @array = qw( elem1
> elem2 ...) but I see everyone here has adopted qw/elem1 elem2/. I could
> easily adapt to this instead. For whatever reason the first is easier for
> to remember and think of.
Both are usual but qw/.../ is more common.
Later you'll learn also many operators looking similiar
q/.../ same as '...'
qq/.../ same as "..." (both practical if there a lot of ' or " inside)
/.../ just for a regexp [here you can't use anything else than a /]
qr/.../ also for a regexp that can be reused
s/.../.../ for substion(s)
tr/.../.../ for translaterating [like replace all A's with Z's]
y/.../.../ as synonym for that
qx/.../ for executing the command inside at the shell or system
and probably some I forgot also.
So, most use just /.../ as we Perler are used to it and it tells us on
first glance, inside those /.../ is a kind of string that gets operated on.
When seeing something like (...) or {...}, we expect on first glance
that there is code to execute instead.
Indeed you can use allmost most all characters as seperators for all
those operators,
so using something like
q!...! or q#...# (e.g. if there a lot of '," and / inside the strings)
qw{... ...} (if there are a lot of / and ( ) inside)
is possible also.
There's more than one way to do it, but for the beginning,
just use qw/.../ or qw( ... ) whatever you like more and feel
comfortable with both if you see it in foreign source code.
Greetings,
Janek
PS: BTW, if you see a // that can mean a lot of other stuffs, too, and
without much context you couldn't even be sure :D, but atm that doesn't
matter for you.
PPS: There is a reason why can make most obfuscating Perl program
contests :-))
------------------------------
Date: Mon, 24 Feb 2014 14:12:15 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: use strict; use warnings;
Message-Id: <8761o4i82o.fsf@castleamber.com>
Janek Schleicher <janek_schleicher@yahoo.de> writes:
> Am 24.02.2014 18:50, schrieb Marek Novotny:
>> That they did cover. For whatever reason I like @array = qw( elem1
>> elem2 ...) but I see everyone here has adopted qw/elem1 elem2/. I could
>> easily adapt to this instead. For whatever reason the first is easier for
>> to remember and think of.
>
> Both are usual but qw/.../ is more common.
Not sure about that one, but:
> So, most use just /.../ as we Perler are used to it and it tells us on
> first glance, inside those /.../ is a kind of string that gets
> operated on.
That's a good motivation to pick // over (), thanks.
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
Date: Mon, 24 Feb 2014 16:02:28 -0600
From: Marek Novotny <mach2@hushmail.com>
Subject: Re: use strict; use warnings;
Message-Id: <NvGdnSXrEcjpXJbOnZ2dnUVZ_rKdnZ2d@supernews.com>
On Mon, 24 Feb 2014 14:23:20 +0000, Jens Thoms Toerring wrote:
> This isn't the solution asked for - you're supposed to shuffle the
> array, not sort it. What's the most simple way to shuffle a deck of
> cards? Just remove a random card from the deck and put it on a new heap
> and repeat this until there are no more cards in the deck you started
> with. And you can shuffle your array in exactly the same way when you
> think about it.
Turns out I didn't read the objective correctly. They did indeed what the
deck more shuffled, and they did not want the high value cards, just the
top five elements from the array.
I did this on the fly, it doesn't contain all the suggestions given here
cause I haven't yet really internalized those yet.
#!/usr/bin/perl
# file: obj10-1.pl
# Objective 1:
#
# The pop function "pops" off the last element of an array
# and returns it. It works like this:
#
# $element = pop(@array);
#
# The shift function works the same way, but removes an element
# from the front of an array.
#
# Using pop, shift, push, and the starting code below, write a script
# that sufficiently "shuffles" a simulated deck of cards before printing
# the top five cards. Save this script in your home directory as
# obj10.pl.
#
# The goal of this objective is to familiarize yourself
# with these three functions while using loops and to use them
# together to rearrange the array but not a randomly shuffled array.
use strict;
use warnings;
my @startingdeck = ("A H","2 H","3 H","4 H","5 H","6 H","7 H","8 H",
"9 H","10 H","J H","Q H","K H",
"A D","2 D","3 D","4 D","5 D","6 D","7 D","8 D",
"9 D","10 D","J D","Q D","K D",
"A C","2 C","3 C","4 C","5 C","6 C","7 C","8 C",
"9 C","10 C","J C","Q C","K C",
"A S","2 S","3 S","4 S","5 S","6 S","7 S","8 S",
"9 S","10 S","J S","Q S","K S");
my $i = 0; my @randomdeck;
while ($i < 51){
$randomdeck[$i] = shift(@startingdeck); $i++;
$randomdeck[$i] = pop(@startingdeck); $i++;
}
print "The top 5 cards are: ";
$i = 0;
while ($i < 5){
print "$randomdeck[$i] "; $i++;
}
print "\n\n";
Much easier than I thought. Oh well. Glad I got it so wrong cause it
raised a lot of good points I really need to learn. Thanks everyone. And
I will work to incorporate all your great knowledge into the next lesson.
------------------------------
Date: Mon, 24 Feb 2014 16:10:21 -0600
From: John Bokma <john@castleamber.com>
Subject: Re: use strict; use warnings;
Message-Id: <877g8k2mcy.fsf@castleamber.com>
Marek Novotny <mach2@hushmail.com> writes:
> # Using pop, shift, push, and the starting code below, write a script
> # that sufficiently "shuffles" a simulated deck of cards before printing
> # the top five cards. Save this script in your home directory as
> # obj10.pl.
You might to read:
http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
on how to shuffle.
Or have a look at how List::Util does this. I normally do
use List::Util 'shuffle';
If I need to shuffle something.
--
John Bokma j3b
Blog: http://johnbokma.com/ Perl Consultancy: http://castleamber.com/
Perl for books: http://johnbokma.com/perl/help-in-exchange-for-books.html
------------------------------
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 4150
***************************************