[31318] in Perl-Users-Digest
Perl-Users Digest, Issue: 2563 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Aug 20 16:09:45 2009
Date: Thu, 20 Aug 2009 13:09:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 20 Aug 2009 Volume: 11 Number: 2563
Today's topics:
Error uninitialized value <w.nijs@alf4all.demon.nl>
Re: Error uninitialized value (Jens Thoms Toerring)
Re: Error uninitialized value <w.nijs@alf4all.demon.nl>
Re: Error uninitialized value (Jens Thoms Toerring)
Re: Error uninitialized value <nat.k@gm.ml>
Re: Error uninitialized value <w.nijs@alf4all.demon.nl>
Re: Error uninitialized value <jimsgibson@gmail.com>
Re: Error uninitialized value <nat.k@gm.ml>
Re: Error uninitialized value <kst-u@mib.org>
Re: Error uninitialized value <tadmc@seesig.invalid>
Re: trapping errors using $! <derykus@gmail.com>
Why doesn't this work? HerbF@earthlink.net
Re: Why doesn't this work? <glennj@ncf.ca>
Re: Why doesn't this work? HerbF@earthlink.net
Re: Why doesn't this work? <jurgenex@hotmail.com>
Re: Why doesn't this work? <nat.k@gm.ml>
Re: Why doesn't this work? <nat.k@gm.ml>
Re: Why doesn't this work? <kst-u@mib.org>
Re: Why doesn't this work? <tadmc@seesig.invalid>
Re: windows one liner to output unix line feed <source@netcom.com>
Re: windows one liner to output unix line feed <glennj@ncf.ca>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 20 Aug 2009 16:48:16 +0200
From: Wijnand Nijs <w.nijs@alf4all.demon.nl>
Subject: Error uninitialized value
Message-Id: <4a8d623b$0$188$e4fe514c@news.xs4all.nl>
Hello,
I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?
Thanks and regards...
Wijnand
Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.
14 # get form params
15 #-----------------
16
17 $Year = param("Year");
18 $Month = param("Month");
19
20
21 # convert localtime
22 #-------------------
23
24 $HourOffset = 0;
25
26 $time = time;
27 ($local_monthday,$local_month,$local_year) = (localtime($time+($HourOffset*3600)))[3,4,5];
28 $local_month = $local_month + 1;
29 $local_year = $local_year + 1900;
30
31
32 # check form params
33 #-------------------
34
35 if ($Year eq "") {
36 $Year = $local_year;
37 };
------------------------------
Date: 20 Aug 2009 15:02:57 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Error uninitialized value
Message-Id: <7f56t1F2j9uftU1@mid.uni-berlin.de>
Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
> I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?
> Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.
> 14 # get form params
> 15 #-----------------
> 16
> 17 $Year = param("Year");
> 35 if ($Year eq "") {
> 36 $Year = $local_year;
> 37 };
IIRC if "Year" hasn't been filled in in the form you seem to be
getting this then param("Year') is an undefined value, not an
empty string. So all you have to do is to apply the 'defined'
operator instead of comparing to "". Or write it all in a single
line:
$Year = param("Year") || $local_year;
This deals with the case that param("Year") is not defined as well
as that it's an empty string.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 20 Aug 2009 17:33:30 +0200
From: Wijnand Nijs <w.nijs@alf4all.demon.nl>
Subject: Re: Error uninitialized value
Message-Id: <4a8d6cd5$0$184$e4fe514c@news.xs4all.nl>
Jens Thoms Toerring schreef:
> Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
>> I get a lot of "uninitialized value" errors in my logfile. The script is working fine but I don't like errors I don't understand. Ok, if there are no form params, I understand that $Year en $Month are empty. Is there a nice way to give $Year and $Month a value if there are no form params without an error message in the log fiale?
>
>> Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.
>
>> 14 # get form params
>> 15 #-----------------
>> 16
>> 17 $Year = param("Year");
>
>> 35 if ($Year eq "") {
>> 36 $Year = $local_year;
>> 37 };
>
> IIRC if "Year" hasn't been filled in in the form you seem to be
> getting this then param("Year') is an undefined value, not an
> empty string. So all you have to do is to apply the 'defined'
> operator instead of comparing to "". Or write it all in a single
> line:
>
> $Year = param("Year") || $local_year;
>
> This deals with the case that param("Year") is not defined as well
> as that it's an empty string.
> Regards, Jens
Thanks!!!
But what to do with:
Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.
in:
109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
???
Wijnand
------------------------------
Date: 20 Aug 2009 16:21:29 GMT
From: jt@toerring.de (Jens Thoms Toerring)
Subject: Re: Error uninitialized value
Message-Id: <7f5bg9F2id15uU1@mid.uni-berlin.de>
Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
> But what to do with:
> Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.
> in:
> 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
Don't know if Andrew came up with something useful (my newsreader
doesn't display HTML messages). But what are '$dateday' and
'%SmallTable'? Why do you need the int() on '$dateday'? Since
you don't show any code I can't say more than Perl already did,
i.e. that either '$dateday' or '$SmallTable{int($dateday)}' is
not defined (those are the only variables on that line). Why this
is the case can only be explained when you show how you set up
'%SmallTable' and '$dateday'.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
------------------------------
Date: Thu, 20 Aug 2009 10:32:03 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: Error uninitialized value
Message-Id: <uMfjm.270660$Ta5.210986@newsfe15.iad>
Wijnand Nijs wrote:
> Jens Thoms Toerring schreef:
>> Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
>>> I get a lot of "uninitialized value" errors in my logfile. The
>>> script is working fine but I don't like errors I don't understand.
>>> Ok, if there are no form params, I understand that $Year en $Month
>>> are empty. Is there a nice way to give $Year and $Month a value if
>>> there are no form params without an error message in the log fiale?
>>
>>> Use of uninitialized value in string eq at
>>> G:/cgi-bin/calendar/calendar.pl line 35.
>>
>>> 14 # get form params
>>> 15 #-----------------
>>> 16
>>> 17 $Year = param("Year");
>>
>>> 35 if ($Year eq "") {
>>> 36 $Year = $local_year;
>>> 37 };
>>
>> IIRC if "Year" hasn't been filled in in the form you seem to be
>> getting this then param("Year') is an undefined value, not an
>> empty string. So all you have to do is to apply the 'defined'
>> operator instead of comparing to "". Or write it all in a single
>> line:
>>
>> $Year = param("Year") || $local_year;
>>
>> This deals with the case that param("Year") is not defined as well
>> as that it's an empty string.
>> Regards, Jens
>
> Thanks!!!
>
> But what to do with:
>
> Use of uninitialized value in substitution (s///) at
> G:/cgi-bin/calendar/calendar.pl line 109.
>
> in:
>
> 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
>
> ???
>
> Wijnand
Looks like you're trying to make a script someone else wrote work, or
you'd know what and why the above is set to do whatever you think it's
supposed to do. Going by the code you've posted, I'd recommend
dropping the idea of using this calendar script and to use a better
one.
------------------------------
Date: Thu, 20 Aug 2009 20:26:17 +0200
From: Wijnand Nijs <w.nijs@alf4all.demon.nl>
Subject: Re: Error uninitialized value
Message-Id: <4a8d9554$0$187$e4fe514c@news.xs4all.nl>
Nathan Keel schreef:
> Wijnand Nijs wrote:
>
>> Jens Thoms Toerring schreef:
>>> Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
>>>> I get a lot of "uninitialized value" errors in my logfile. The
>>>> script is working fine but I don't like errors I don't understand.
>>>> Ok, if there are no form params, I understand that $Year en $Month
>>>> are empty. Is there a nice way to give $Year and $Month a value if
>>>> there are no form params without an error message in the log fiale?
>>>> Use of uninitialized value in string eq at
>>>> G:/cgi-bin/calendar/calendar.pl line 35.
>>>> 14 # get form params
>>>> 15 #-----------------
>>>> 16
>>>> 17 $Year = param("Year");
>>>> 35 if ($Year eq "") {
>>>> 36 $Year = $local_year;
>>>> 37 };
>>> IIRC if "Year" hasn't been filled in in the form you seem to be
>>> getting this then param("Year') is an undefined value, not an
>>> empty string. So all you have to do is to apply the 'defined'
>>> operator instead of comparing to "". Or write it all in a single
>>> line:
>>>
>>> $Year = param("Year") || $local_year;
>>>
>>> This deals with the case that param("Year") is not defined as well
>>> as that it's an empty string.
>>> Regards, Jens
>> Thanks!!!
>>
>> But what to do with:
>>
>> Use of uninitialized value in substitution (s///) at
>> G:/cgi-bin/calendar/calendar.pl line 109.
>>
>> in:
>>
>> 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
>>
>> ???
>>
>> Wijnand
>
> Looks like you're trying to make a script someone else wrote work, or
> you'd know what and why the above is set to do whatever you think it's
> supposed to do. Going by the code you've posted, I'd recommend
> dropping the idea of using this calendar script and to use a better
> one.
Hello Andrew, Jens and Nathan
Yes it is a script of someone else. I can drop this script and look for another, but I am also trying to learn something (writing Perl scripts).
I have a few files with (birthday- and other)events with a line format:
00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm
after collecting and sorting the data the script filters de events for that month to a hash:
foreach $line (@sortedevents) {
($date,$event,$URL) = split (/\|/, $line);
($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;
# collect the events for $dateday
if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) && (int($datemonth) == int($Month))) {
if ($URL) {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A HREF=\"$URL\">$event</A>";
} else {
$SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
}
}
# drop the first HR
$SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
}
with this hash the calendar for that month is built.
Thanks for your help, regards...
Wijnand
------------------------------
Date: Thu, 20 Aug 2009 12:07:50 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: Error uninitialized value
Message-Id: <200820091207500786%jimsgibson@gmail.com>
In article <4a8d9554$0$187$e4fe514c@news.xs4all.nl>, Wijnand Nijs
<w.nijs@alf4all.demon.nl> wrote:
> I have a few files with (birthday- and other)events with a line format:
>
> 00000209|Anita Bruls-Jokhorst|/roots/lines/jokhorst/jok-aaa/jok-jeu/anita.htm
>
> after collecting and sorting the data the script filters de events for that
> month to a hash:
>
> foreach $line (@sortedevents) {
>
> ($date,$event,$URL) = split (/\|/, $line);
> ($dateyear,$datemonth,$dateday) = $date =~ m#(\d\d\d\d)(\d\d)(\d\d)#o;
You may have a line that does not match the above reqular expression,
resulting in invalid values for $dateyear, etc. You should always check
whether the RE has been successfully matched before using the values
from the match.
>
> # collect the events for $dateday
> if (((int($dateyear) == int($Year)) || (int($dateyear) < 1)) &&
> (int($datemonth) == int($Month))) {
This if statement expression may evaluate to false, ...
> if ($URL) {
> $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%><A
> HREF=\"$URL\">$event</A>";
> } else {
> $SmallTable{int($dateday)} = "<BR><HR NOSHADE WIDTH=25%>$event";
> }
> }
>
> # drop the first HR
> $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
... but here you assume that it succeeded and put a value in
$SmallTable. You should put this line somewhere else, where you are
assured of having a value in $SmallTable{int($dateday)}, or at least
test for a value before trying to modify it:
if( $SmallTable{int($dateday)} ) {
...
}
> }
>
> with this hash the calendar for that month is built.
A possibly better approach would be to push each event into an array,
then use join to concatenate the events when you have finished parsing
the file.
--
Jim Gibson
------------------------------
Date: Thu, 20 Aug 2009 12:11:39 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: Error uninitialized value
Message-Id: <Mdhjm.175259$3m2.112652@newsfe06.iad>
Wijnand Nijs wrote:
>>>
>>> But what to do with:
>>>
>>> Use of uninitialized value in substitution (s///) at
>>> G:/cgi-bin/calendar/calendar.pl line 109.
That says it all.
> # drop the first HR
> $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
That is trying to blindly substitute.
I would not recommend learning from a bad script, unless you just want
to learn what to avoid. Trying to fix a bad script is possibly
helpful, but more likely will confuse you.
------------------------------
Date: Thu, 20 Aug 2009 12:37:13 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Error uninitialized value
Message-Id: <lnbpmatgye.fsf@nuthaus.mib.org>
jt@toerring.de (Jens Thoms Toerring) writes:
> Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
>> I get a lot of "uninitialized value" errors in my logfile. The
>> script is working fine but I don't like errors I don't
>> understand. Ok, if there are no form params, I understand that
>> $Year en $Month are empty. Is there a nice way to give $Year and
>> $Month a value if there are no form params without an error message
>> in the log fiale?
>
>> Use of uninitialized value in string eq at G:/cgi-bin/calendar/calendar.pl line 35.
>
>> 14 # get form params
>> 15 #-----------------
>> 16
>> 17 $Year = param("Year");
>
>> 35 if ($Year eq "") {
>> 36 $Year = $local_year;
>> 37 };
>
> IIRC if "Year" hasn't been filled in in the form you seem to be
> getting this then param("Year') is an undefined value, not an
> empty string. So all you have to do is to apply the 'defined'
> operator instead of comparing to "". Or write it all in a single
> line:
>
> $Year = param("Year") || $local_year;
>
> This deals with the case that param("Year") is not defined as well
> as that it's an empty string.
But be careful; this will set $Year to $local_year if param("Year")
is either undef, the empty string, or 0. That shouldn't cause any
visible problems for a year number (unless you're dealing with very
old events or using 2-digit years), but it could bite you for values
that could legitimately be 0.
Perl 6 has a "defined-or" operator, spelled "//", that acts like
"||" except that it tests its first operand for definedness rather
than truth. So in Perl6 you could write:
$Year = param("Year") // $local_year;
Perl 5.10 has this as well, but I think it was introduced relatively
recently (5.8.8 doesn't have it), so there are still plenty of Perl
installations out there that don't support it.
If you can't assume that the // operator will be available, you
can do something like this:
$Year = param("Year");
$Year = $local_year if not defined $Year;
Or like this:
$Year = defined param("Year") ? param("Year") : $local_year;
But note that the latter calls param("Year") twice.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Thu, 20 Aug 2009 14:57:55 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Error uninitialized value
Message-Id: <slrnh8ra4p.t9p.tadmc@tadmc30.sbcglobal.net>
Jens Thoms Toerring <jt@toerring.de> wrote:
> Wijnand Nijs <w.nijs@alf4all.demon.nl> wrote:
>> But what to do with:
>
>> Use of uninitialized value in substitution (s///) at G:/cgi-bin/calendar/calendar.pl line 109.
>
>> in:
>
>> 109 $SmallTable{int($dateday)} =~ s/^<BR><HR NOSHADE WIDTH=25%>//;
>
> Don't know if Andrew came up with something useful
Trolls never say anything useful.
> (my newsreader
> doesn't display HTML messages).
You have been auto-spared!
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 20 Aug 2009 11:02:57 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: trapping errors using $!
Message-Id: <7d11a8a6-df14-45cd-8816-df951270840a@13g2000prl.googlegroups.com>
On Aug 20, 4:54=A0am, "John" <john1...@yahoo.com> wrote:
> Hi
>
> This part of =A0code is in a web service so there is no output to screen.
> I've turned off printing to standard error.
>
> open STDERR,'>/dev/null';
>
> ....
> read (STDIN,$request,$length);
> if ($1 > 0) {$response=3D$error1};
>
> Would the above condition trap any error?
>
Hm, I'm not sure what "any error" means in this
context. (also the use of $1 isn't clear to me)
You could check with something such as:
my $nread =3D read( STDIN, $request, $length );
unless (defined $nread) {
$response =3D "read error: $!";
...
}
This'd pick up the read error only though.
perldoc -f read
--
Charles DeRykus
------------------------------
Date: Thu, 20 Aug 2009 11:08:00 -0700
From: HerbF@earthlink.net
Subject: Why doesn't this work?
Message-Id: <k24r85pqa151ahc9jo131u9e70kealf3pu@4ax.com>
Noob here.
I'm trying to write a simple routine to print a character every second. I
have:
foreach (1..5) {
print "*";
sleep 1;
}
When I run it, it waits for the full 5 seconds to tick by, and then prints
the string '*****'. Why doesn't it print an asterisk every second until
finished?
Herb
------------------------------
Date: 20 Aug 2009 18:16:06 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: Why doesn't this work?
Message-Id: <slrnh8r4n7.rcl.glennj@smeagol.ncf.ca>
At 2009-08-20 02:08PM, "HerbF@earthlink.net" wrote:
> Noob here.
>
> I'm trying to write a simple routine to print a character every second. I
> have:
>
> foreach (1..5) {
> print "*";
> sleep 1;
> }
>
> When I run it, it waits for the full 5 seconds to tick by, and then prints
> the string '*****'. Why doesn't it print an asterisk every second until
> finished?
Your output is buffered. Set $|=1 first.
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
------------------------------
Date: Thu, 20 Aug 2009 11:55:51 -0700
From: HerbF@earthlink.net
Subject: Re: Why doesn't this work?
Message-Id: <8v6r85hof3gasdsporgn1uuucqp1etpg2i@4ax.com>
Glenn Jackman wrote:
>At 2009-08-20 02:08PM, "HerbF@earthlink.net" wrote:
>> Noob here.
>>
>> I'm trying to write a simple routine to print a character every second. I
>> have:
>>
>> foreach (1..5) {
>> print "*";
>> sleep 1;
>> }
>>
>> When I run it, it waits for the full 5 seconds to tick by, and then prints
>> the string '*****'. Why doesn't it print an asterisk every second until
>> finished?
>
>Your output is buffered. Set $|=1 first.
Tried that, but it made no difference.
Herb
------------------------------
Date: Thu, 20 Aug 2009 12:14:43 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: Why doesn't this work?
Message-Id: <8s7r85td6s3eljd799j3l4u00dtj91oirv@4ax.com>
HerbF@earthlink.net wrote:
>Glenn Jackman wrote:
>>At 2009-08-20 02:08PM, "HerbF@earthlink.net" wrote:
>>> I'm trying to write a simple routine to print a character every second. I
>>> have:
>>>
>>> foreach (1..5) {
>>> print "*";
>>> sleep 1;
>>> }
>>>
>>> When I run it, it waits for the full 5 seconds to tick by, and then prints
>>> the string '*****'. Why doesn't it print an asterisk every second until
>>> finished?
>>
>>Your output is buffered. Set $|=1 first.
>
>Tried that, but it made no difference.
Then something else is buffering on top of Perl's buffering. See
'perldoc -q buffer' for some in-depth discussion and for other ways to
work around buffering.
jue
------------------------------
Date: Thu, 20 Aug 2009 12:20:58 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: Why doesn't this work?
Message-Id: <Bmhjm.175263$3m2.67135@newsfe06.iad>
HerbF@earthlink.net wrote:
> Noob here.
>
> I'm trying to write a simple routine to print a character every
> second. I have:
>
> foreach (1..5) {
> print "*";
> sleep 1;
> }
>
> When I run it, it waits for the full 5 seconds to tick by, and then
> prints the string '*****'. Why doesn't it print an asterisk every
> second until finished?
>
> Herb
Is this on the command line or via a web browser?
------------------------------
Date: Thu, 20 Aug 2009 12:21:16 -0700
From: Nathan Keel <nat.k@gm.ml>
Subject: Re: Why doesn't this work?
Message-Id: <Mmhjm.175264$3m2.171469@newsfe06.iad>
HerbF@earthlink.net wrote:
> Glenn Jackman wrote:
>
>>At 2009-08-20 02:08PM, "HerbF@earthlink.net" wrote:
>>> Noob here.
>>>
>>> I'm trying to write a simple routine to print a character every
>>> second. I have:
>>>
>>> foreach (1..5) {
>>> print "*";
>>> sleep 1;
>>> }
>>>
>>> When I run it, it waits for the full 5 seconds to tick by, and then
>>> prints the string '*****'. Why doesn't it print an asterisk every
>>> second until finished?
>>
>>Your output is buffered. Set $|=1 first.
>
> Tried that, but it made no difference.
>
> Herb
Did you put it above the for loop?
------------------------------
Date: Thu, 20 Aug 2009 12:21:37 -0700
From: Keith Thompson <kst-u@mib.org>
Subject: Re: Why doesn't this work?
Message-Id: <lnfxbmthoe.fsf@nuthaus.mib.org>
HerbF@earthlink.net writes:
> Glenn Jackman wrote:
>>At 2009-08-20 02:08PM, "HerbF@earthlink.net" wrote:
>>> Noob here.
>>>
>>> I'm trying to write a simple routine to print a character every second. I
>>> have:
>>>
>>> foreach (1..5) {
>>> print "*";
>>> sleep 1;
>>> }
>>>
>>> When I run it, it waits for the full 5 seconds to tick by, and then prints
>>> the string '*****'. Why doesn't it print an asterisk every second until
>>> finished?
>>
>>Your output is buffered. Set $|=1 first.
>
> Tried that, but it made no difference.
It worked for me.
Here's my program:
====================
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
foreach (1..5) {
print "*";
sleep 1;
}
print "\n";
====================
With the "$| = 1" commented out, it waits 5 seconds and then prints
"*****". With the line in place, it prints a '*' character every
second.
Maybe it's related to the environment in which you're running the
script, but it works for me under Ubuntu, Cygwin, and Windows
(ActiveState Perl).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
------------------------------
Date: Thu, 20 Aug 2009 14:56:08 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Why doesn't this work?
Message-Id: <slrnh8ra1e.t9p.tadmc@tadmc30.sbcglobal.net>
Nathan Keel <nat.k@gm.ml> wrote:
> HerbF@earthlink.net wrote:
>
>> Noob here.
>>
>> I'm trying to write a simple routine to print a character every
>> second. I have:
>>
>> foreach (1..5) {
>> print "*";
>> sleep 1;
>> }
>>
>> When I run it, it waits for the full 5 seconds to tick by, and then
>> prints the string '*****'. Why doesn't it print an asterisk every
>> second until finished?
>>
>> Herb
>
> Is this on the command line or via a web browser?
Perl does not run in a web browser.
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
------------------------------
Date: Thu, 20 Aug 2009 10:22:03 -0700
From: David Harmon <source@netcom.com>
Subject: Re: windows one liner to output unix line feed
Message-Id: <ZqydnRehtL72GxDXnZ2dnUVZ_hqdnZ2d@earthlink.com>
On 19 Aug 2009 20:25:50 GMT in comp.lang.perl.misc, Glenn Jackman
<glennj@ncf.ca> wrote,
>At 2009-08-19 01:28PM, "boman" wrote:
>> I have a simple one liner running on Windows that does a substitution.
>> However, with the -p option, the line endings are coming out \r\n, and
>> I need them to be just \n.
>>
>> perl -pi.bak -e "s|foo|bar|g" myfile.txt
>
>perhaps you need to chomp the windows line ending, and add the unix line
>ending manually:
>
> perl -pi.bak -e "s|foo|bar|g; chomp; print qq{$_\n}" myfile.txt
How could that work when the output stream converts \n to the
system-defined line ending?
------------------------------
Date: 20 Aug 2009 18:15:16 GMT
From: Glenn Jackman <glennj@ncf.ca>
Subject: Re: windows one liner to output unix line feed
Message-Id: <slrnh8r4ll.rcl.glennj@smeagol.ncf.ca>
At 2009-08-20 01:22PM, "David Harmon" wrote:
> On 19 Aug 2009 20:25:50 GMT in comp.lang.perl.misc, Glenn Jackman
> <glennj@ncf.ca> wrote,
> >At 2009-08-19 01:28PM, "boman" wrote:
> >> I have a simple one liner running on Windows that does a substitution.
> >> However, with the -p option, the line endings are coming out \r\n, and
> >> I need them to be just \n.
> >>
> >> perl -pi.bak -e "s|foo|bar|g" myfile.txt
> >
> >perhaps you need to chomp the windows line ending, and add the unix line
> >ending manually:
> >
> > perl -pi.bak -e "s|foo|bar|g; chomp; print qq{$_\n}" myfile.txt
>
> How could that work when the output stream converts \n to the
> system-defined line ending?
Hmm, guess you'd have to open a temp file in binmode, write to it, then
overwrite the input file with the temp file. Not really one-liner
territory.
--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 2563
***************************************