[32076] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3340 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Mar 31 16:09:27 2011

Date: Thu, 31 Mar 2011 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, 31 Mar 2011     Volume: 11 Number: 3340

Today's topics:
        how about for(<>){...} instead of while(<>){...} <jidanni@jidanni.org>
    Re: how about for(<>){...} instead of while(<>){...} (Randal L. Schwartz)
    Re: how about for(<>){...} instead of while(<>){...} <tadmc@seesig.invalid>
    Re: how about for(<>){...} instead of while(<>){...} <jl_post@hotmail.com>
    Re: pattern matching and abstract functions <cartercc@gmail.com>
    Re: pattern matching and abstract functions <uri@StemSystems.com>
    Re: pattern matching and abstract functions <cwilbur@chromatico.net>
    Re: pattern matching and abstract functions <cartercc@gmail.com>
    Re: Search script to index dynamic pages <rdw204@gmail.com>
    Re: Search script to index dynamic pages <willem@toad.stack.nl>
    Re: Search script to index dynamic pages <rdw204@gmail.com>
    Re: Search script to index dynamic pages <willem@toad.stack.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 30 Mar 2011 18:11:05 -0700 (PDT)
From: jidanni <jidanni@jidanni.org>
Subject: how about for(<>){...} instead of while(<>){...}
Message-Id: <460fe5b0-eeef-4c40-b611-07929b3caf68@glegroupsg2000goo.googlegroups.com>

Gentlemen, what peril awaits me if I use
for  (<>){...} loops instead of
while(<>){...} loops?

I got the idea from perlsyn:
 'Instead of using "given()", you can use a "foreach()" loop.'
And indeed I can now use when() without an enclosing given().


------------------------------

Date: Wed, 30 Mar 2011 19:45:09 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: how about for(<>){...} instead of while(<>){...}
Message-Id: <867hbg57pm.fsf@red.stonehenge.com>

>>>>> "jidanni" == jidanni  <jidanni@jidanni.org> writes:

jidanni> Gentlemen, what peril awaits me if I use
jidanni> for  (<>){...} loops instead of
jidanni> while(<>){...} loops?

Try this:

    @ARGV = qw(verylargefile);
    while (<>) { ... }

vs

    @ARGV = qw(verylargefile);
    foreach (<>) { ... }

The first one has a line-at-a-time in memory, regardless of the size of
the file.  The second one *must* read the *entire file* into memory
before starting the foreach loop.

Oops.

Yes, this is in Learning Perl.  I recommend you read Learning Perl if
you have questions at this level.

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

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


------------------------------

Date: Wed, 30 Mar 2011 22:51:13 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: how about for(<>){...} instead of while(<>){...}
Message-Id: <slrnip7u8b.tgf.tadmc@tadbox.sbcglobal.net>

jidanni <jidanni@jidanni.org> wrote:
> Gentlemen, what peril awaits me if I use
> for  (<>){...} loops instead of
> while(<>){...} loops?


Using great gobs of RAM unnecessarily.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


------------------------------

Date: Thu, 31 Mar 2011 10:21:05 -0700 (PDT)
From: "jl_post@hotmail.com" <jl_post@hotmail.com>
Subject: Re: how about for(<>){...} instead of while(<>){...}
Message-Id: <14d326b0-7867-4550-b351-b4e0fb32b2fa@e9g2000vbk.googlegroups.com>

On Mar 30, 7:11=A0pm, jidanni <jida...@jidanni.org> wrote:
> Gentlemen, what peril awaits me if I use
> for =A0(<>){...} loops instead of
> while(<>){...} loops?
>
> I got the idea from perlsyn:
> =A0'Instead of using "given()", you can use a "foreach()" loop.'
> And indeed I can now use when() without an enclosing given().


   They are very similar, but as others pointed out, for(<>) reads in
the entire input before processing the elements, whereas while(<>)
reads and processed the input line-by-line.  That's because, according
to "perldoc perltrap", "while (<FH>)" is equivalent to:

      while (defined($_ =3D <FH>))

So using for(<>) should work with small input, but be careful of large
inputs, or the entire file/input will be read in at once.

   There is also another difference that is rather subtle, so it often
goes unnoticed:  Because while(<>) implicitly assigns to the $_
variable, that variable WILL be different after the while-loop than
before the loop.

   In contrast to the for-loop, for(<>) sets $_ to each entry of the
list that <> expands to, but the difference being that $_ will be
restored to whatever it was right before the for-loop was
encountered.  (So for example, if $_ was "hello" before the for-loop,
it will be restored to "hello" when the for-loop is done.)

   This is not so for while-loops.  If $_ was "hello" right before the
while-loop, after the while-loop it will be set to the last line
returned from <> (accounting to any changes you make to $_ inside the
loop, of course).

   The fact that for-loops restore $_ when they're finished looping
make it (usually) safe to nest them inside each each other, as even if
they all operate on $_, they'll restore the outer $_ when they're
done.  I found out the hard way that while-loops don't do this, as
inner while-loops will readily "hijack" $_ without restoring them to
the value set by the outer while-loops.

   So while(<>) generally uses less memory and is more efficient than
for(<>), but keep in mind that while-loops won't restore the original
value of $_ when they're done looping.

   Cheers,

   -- Jean-Luc


------------------------------

Date: Wed, 30 Mar 2011 11:13:37 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: pattern matching and abstract functions
Message-Id: <3c0d2962-1d86-43f2-8f3e-813914028ebe@t19g2000prd.googlegroups.com>

On Mar 30, 1:00=A0pm, "Uri Guttman" <u...@StemSystems.com> wrote:
> =A0 c> cost(orange) -> 4;
> =A0 c> cost(apple) -> 5;
> =A0 c> cost(banana) -> 6;
> =A0 c> cost(apple) -> 7;
> =A0 c> cost(_) -> unknown.
>
> and that is a dispatch table with a different syntax. also it seems your
> code example is really a hash as the values are just constants.

Uri, the integers above (4, 5, 6, an 7) are just the return values
from the function. Erlang is a functional language, and it doesn't
matter if the return values are symbols, primitives, constants,
variable values, return values from named functions, or return values
from lambda functions.

I'm not complaining, or touting one language as opposed to another,
just pointing out differences. There's a reason I use Perl for 95% of
my programming tasks, and it's because it's suited to the tasks I need
to do.

Yes, I like Erlang style multi functions, but Erlang doesn't have a
string data type, and it's really hard to do Perl style string
manipulation if you don't have a string data type. It's not a crime to
like a nifty feature of some language and wish your own workhorse
language had that feature.

Besides, the Perl style dispatch table will do what I want, so take my
word for it that I'm not complaining or dissing Perl in any way.

CC.


------------------------------

Date: Wed, 30 Mar 2011 14:34:49 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: pattern matching and abstract functions
Message-Id: <87mxkc8njq.fsf@quad.sysarch.com>

>>>>> "c" == ccc31807  <cartercc@gmail.com> writes:

  c> On Mar 30, 1:00 pm, "Uri Guttman" <u...@StemSystems.com> wrote:
  >>   c> cost(orange) -> 4;
  >>   c> cost(apple) -> 5;
  >>   c> cost(banana) -> 6;
  >>   c> cost(apple) -> 7;
  >>   c> cost(_) -> unknown.
  >> 
  >> and that is a dispatch table with a different syntax. also it seems your
  >> code example is really a hash as the values are just constants.

  c> Uri, the integers above (4, 5, 6, an 7) are just the return values
  c> from the function. Erlang is a functional language, and it doesn't
  c> matter if the return values are symbols, primitives, constants,
  c> variable values, return values from named functions, or return values
  c> from lambda functions.

and your example reduces to a hash. that is all that matters here. a
hash that has code references is the same as erlang returning named or
lambda functions. it is the SAME CONCEPT in about the same amount of
code. no lang has a magic way to do dispatch tables as you always need a
map of keys to subs. the syntax varies but the required data is the
same.


  c> Besides, the Perl style dispatch table will do what I want, so take my
  c> word for it that I'm not complaining or dissing Perl in any way.

then just learn perl dispatch tables already and stop this thread.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


------------------------------

Date: Wed, 30 Mar 2011 15:54:15 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: pattern matching and abstract functions
Message-Id: <86bp0sxu3c.fsf@mithril.chromatico.net>

>>>>> "cc" == ccc31807  <cartercc@gmail.com> writes:

    cc> If you call cost(X), you get the cost of X -- simple, straight
    cc> forward pattern matching, and it doesn't matter whether X is an
    cc> apple, orange, or anything else.

Congratulations, you've just reinvented parametric polymorphism.

The syntactic sugar for that in Perl is $x->cost;

Its implementation is left as an exercise for the reader. 

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


------------------------------

Date: Thu, 31 Mar 2011 04:51:53 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: pattern matching and abstract functions
Message-Id: <e61ccf0b-eb65-4366-be85-cd8f7daf484d@j13g2000yqj.googlegroups.com>

On Mar 30, 2:54=A0pm, Charlton Wilbur <cwil...@chromatico.net> wrote:
> Congratulations, you've just reinvented parametric polymorphism.

I may be dead wrong, but I've always understood polymorphism to depend
on the TYPE of the parameter, rather than the value of the parameter.

In Prolog, the function parameters are pattern matched as to the
value, yet I've never heard anyone say that Prolog functions are
polymorphic. Same is true in Erlang, which is a direct descendant of
Prolog.

Lisp has generic functions which are specialized on the parameter
TYPE, rather than value, but in my case, all the parameters are of the
same TYPE -- it's the value that differs and it's the value that I
want to key on.

Dispatch tables work fine. Looking back in HOP, MJD discusses them
early on, but I had never used them and had forgotten about them.

CC.


------------------------------

Date: Thu, 31 Mar 2011 12:11:23 -0700 (PDT)
From: Rob <rdw204@gmail.com>
Subject: Re: Search script to index dynamic pages
Message-Id: <afec895d-9954-47a7-bd9e-4826a13a2ed6@1g2000yqq.googlegroups.com>

On Mar 30, 4:53=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
> Did it occur to you that the text of the error messages
> might be helpful in debugging the problem?
>

Yes, this did occur to me- however my hosting provider does not give
error logs. The code works when run on my own machine, but does not
work correctly when run on the server. If I pass the information from
a 'get' command to a variable it is simply left blank.


> After it is working from the command line, *then* run it under
> a web server.

I am now able to 'get' a page when I run the script from my own
computer - it successfully downloads the page and I can do what I like
with the data. However when I run this script online it does not work
at all. I have tried other techniques, such as WWW::Mechanize and
LWP::UserAgent, neither of which produce better results.

One thought was that there may have been firewall/bot protection on
the server, but as the script works from my own computer then it
should work from the server also?

Many thanks for your help so far,

Rob


------------------------------

Date: Thu, 31 Mar 2011 19:22:26 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Search script to index dynamic pages
Message-Id: <slrnip9l3i.313u.willem@toad.stack.nl>

Rob wrote:
) On Mar 30, 4:53?pm, Tad McClellan <ta...@seesig.invalid> wrote:
)> Did it occur to you that the text of the error messages
)> might be helpful in debugging the problem?
)>
)
) Yes, this did occur to me- however my hosting provider does not give
) error logs.

Off the top of my head:

BEGIN {
	print "Content-type: text/plain\n\n";
	$SIG{__WARN__} = sub { print @_ };
	$SIG(__DIE__} = sub { print @_ unless $^S };
}

Should make the error message go to the browser,
and that should even work for compile-time errors.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Thu, 31 Mar 2011 12:38:03 -0700 (PDT)
From: Rob <rdw204@gmail.com>
Subject: Re: Search script to index dynamic pages
Message-Id: <0804f5f6-4561-451c-9d67-fc1b24334be7@n10g2000yqf.googlegroups.com>

On Mar 31, 8:22=A0pm, Willem <wil...@toad.stack.nl> wrote:

> Off the top of my head:
>
> BEGIN {
> =A0 =A0 =A0 =A0 print "Content-type: text/plain\n\n";
> =A0 =A0 =A0 =A0 $SIG{__WARN__} =3D sub { print @_ };
> =A0 =A0 =A0 =A0 $SIG(__DIE__} =3D sub { print @_ unless $^S };
>
> }
>
> Should make the error message go to the browser,
> and that should even work for compile-time errors.

Thank you for this. When I run with this code the only error I get is
that the variable '$data' is uninitialized (presumably because the
'get' function has not succeeded in passing any data to it).

The code now stands as follows (for the test file I have made):


###########################################################################=
#####
#!/usr/bin/perl -w
use CGI qw(:all);
use LWP::Simple;

BEGIN {
        print "Content-type: text/plain\n\n";
        $SIG{__WARN__} =3D sub { print @_ };
        $SIG{__DIE__} =3D sub { print @_ unless $^S };
}
  my $data =3D get("http://www.samplesite.org");

  open (CF2, "testtext.txt");
  print CF2 "$data";
  close(CF2);
###########################################################################=
#####

Like I said, it works on my computer but not on the server.

Thanks,

Rob


------------------------------

Date: Thu, 31 Mar 2011 19:50:43 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Search script to index dynamic pages
Message-Id: <slrnip9moj.7hf.willem@toad.stack.nl>

Rob wrote:
) On Mar 31, 8:22?pm, Willem <wil...@toad.stack.nl> wrote:
)
)> Off the top of my head:
)>
)> BEGIN {
)> ? ? ? ? print "Content-type: text/plain\n\n";
)> ? ? ? ? $SIG{__WARN__} = sub { print @_ };
)> ? ? ? ? $SIG(__DIE__} = sub { print @_ unless $^S };
)>
)> }
)>
)> Should make the error message go to the browser,
)> and that should even work for compile-time errors.
)
) Thank you for this. When I run with this code the only error I get is
) that the variable '$data' is uninitialized (presumably because the
) 'get' function has not succeeded in passing any data to it).

Yes.  LWP::Simple doesn't do error reporting.  At all.
You should use LWP::UserAgent if you want to know more than 'it failed'.

) The code now stands as follows (for the test file I have made):
)
)
) ################################################################################
) #!/usr/bin/perl -w
) use CGI qw(:all);
) use LWP::Simple;

use strict;
use warnings;

)
) BEGIN {
)         print "Content-type: text/plain\n\n";
)         $SIG{__WARN__} = sub { print @_ };
)         $SIG{__DIE__} = sub { print @_ unless $^S };
) }
)   my $data = get("http://www.samplesite.org");
)
)   open (CF2, "testtext.txt");
)   print CF2 "$data";
)   close(CF2);

# Use lexical filehandles.

) ################################################################################
)
) Like I said, it works on my computer but not on the server.

Dump LWP::Simple, and code it using LWP::UserAgent

use LWP::UserAgent;
my $response = LWP::UserAgent->new->get("http://www.samplesite.org");
if ($response->is_success) {
  open (my $cf, '>', 'testtext.txt') or die "Failed to write: $!";
  print $cf $response->decoded_content;
  close $cf;
} else {
  die $response->status_line;
}


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: 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 3340
***************************************


home help back first fref pref prev next nref lref last post