[25174] in Perl-Users-Digest
Perl-Users Digest, Issue: 7423 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 19 00:10:41 2004
Date: Thu, 18 Nov 2004 21:10:11 -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 Thu, 18 Nov 2004 Volume: 10 Number: 7423
Today's topics:
Problem with Hashes <shashank@mia.ece.uic.edu>
Re: Problem with Hashes <shashank@mia.ece.uic.edu>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <postmaster@castleamber.com>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <spamtrap@dot-app.org>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <matthew.garrish@sympatico.ca>
Re: Problem with Hashes <see@sig.invalid>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <spamtrap@dot-app.org>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <spamtrap@dot-app.org>
Re: Problem with Hashes <nospam@nospam.com>
Re: Problem with Hashes <nospam@nospam.com>
regex challange: collapse nested {} blocks <ken@nospam.org>
Re: script hangs waiting for key stroke <see@sig.invalid>
Re: Search for string and return file name <someone@example.com>
Re: Simple (!) HTML/PERL code fpr uploading files throu <vilain@spamcop.net>
Re: Simple (!) HTML/PERL code fpr uploading files throu <postmaster@castleamber.com>
simple folder search <bnbliss@comcast.net>
Re: simple folder search <spamtrap@dot-app.org>
Re: simple folder search <bnbliss@comcast.net>
Re: simple folder search <tassilo.von.parseval@rwth-aachen.de>
Re: the antichomp <ioneabu@yahoo.com>
Re: the antichomp <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 18 Nov 2004 18:00:28 -0600
From: Shashank Khanvilkar <shashank@mia.ece.uic.edu>
Subject: Problem with Hashes
Message-Id: <cnjesc$ftf$1@newsx.cc.uic.edu>
Hi,
I have a program fragment like this...
----------------------------------------
%HoH = ();
sub1(\%HoH); #Passing the Hoh by reference...
print "size of hash: " . keys(%HoH) . ".\n";
.......
sub1{
my $a = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}
----------------------------------------
Since i am passing the Hoh by reference, the print statement should
display 2. However in my case, it gives size of hash as "0"..
what am i doing wrong?
Any help is appreciated.
Regards
Shashank
------------------------------
Date: Thu, 18 Nov 2004 18:19:02 -0600
From: Shashank Khanvilkar <shashank@mia.ece.uic.edu>
Subject: Re: Problem with Hashes
Message-Id: <cnjfv6$fu3$1@newsx.cc.uic.edu>
Shashank Khanvilkar wrote:
> Hi,
> I have a program fragment like this...
>
> ----------------------------------------
> %HoH = ();
> sub1(\%HoH); #Passing the Hoh by reference...
> print "size of hash: " . keys(%HoH) . ".\n";
> .......
>
>
> sub1{
> my $a = @_;
> $a->{A}->{1} = 10;
> $a->{B}->{2} = 20;
> }
>
>
> ----------------------------------------
> Since i am passing the Hoh by reference, the print statement should
> display 2. However in my case, it gives size of hash as "0"..
> what am i doing wrong?
> Any help is appreciated.
> Regards
> Shashank
Sorry.. I got the problem solved..
the onl;y mistake was
sub1{
my ($a) = @_;
$a->{A}->{1} = 10;
$a->{B}->{2} = 20;
}
thasnks
------------------------------
Date: Thu, 18 Nov 2004 19:43:23 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100825111.430224@nntp.acecape.com>
"Shashank Khanvilkar" <shashank@mia.ece.uic.edu> wrote in message
news:cnjfv6$fu3$1@newsx.cc.uic.edu...
> Sorry.. I got the problem solved..
> the onl;y mistake was
>
> sub1{
> my ($a) = @_;
> $a->{A}->{1} = 10;
> $a->{B}->{2} = 20;
> }
hi, am still new to Perl so I mark off threads where i might learn from. so
please forgive my extended this thread.....i am lost as to how changing my
$a to my (a$) resolved your problem.....may i ask "why?"
i was under the impression that you used ( ) to declare mutiple variables
at once, my (a$, b$) ... now can't find what ( ) does here that made the
difference,
thanks ahead,
daniel
------------------------------
Date: 19 Nov 2004 01:04:09 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Problem with Hashes
Message-Id: <Xns95A5C1F9EC5B6castleamber@130.133.1.4>
daniel kaplan wrote:
> "Shashank Khanvilkar" <shashank@mia.ece.uic.edu> wrote in message
> news:cnjfv6$fu3$1@newsx.cc.uic.edu...
>> Sorry.. I got the problem solved..
>> the onl;y mistake was
>>
>> sub1{
>> my ($a) = @_;
>> $a->{A}->{1} = 10;
>> $a->{B}->{2} = 20;
>> }
>
> hi, am still new to Perl so I mark off threads where i might learn
> from. so please forgive my extended this thread.....i am lost as to
> how changing my $a to my (a$) resolved your problem.....may i ask
> "why?"
my $a = scalar context here
my ($a) = list context here
> i was under the impression that you used ( ) to declare mutiple
> variables at once, my (a$, b$) ... now can't find what ( ) does here
> that made the difference,
@_ in scalar context results in the number of items in it, hence my $a =
@_ puts 1 in it if the sub was called with one parameter.
See above. BTW a$ sounds BASIC to me :-D.
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Thu, 18 Nov 2004 20:37:11 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100828339.421106@nntp.acecape.com>
"John Bokma" <postmaster@castleamber.com> wrote in message
news:Xns95A5C1F9EC5B6castleamber@130.133.1.4...
> my $a = scalar context here
> my ($a) = list context here
>
> @_ in scalar context results in the number of items in it, hence my $a =
> @_ puts 1 in it if the sub was called with one parameter.
>
but i thought that he was passing in a "reference" to a hash....not the hash
itself....
------------------------------
Date: Thu, 18 Nov 2004 21:24:29 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Problem with Hashes
Message-Id: <ZcqdnarTSYenxADcRVn-rw@adelphia.com>
daniel kaplan wrote:
> i was under the impression that you used ( ) to declare mutiple variables
> at once, my (a$, b$) ... now can't find what ( ) does here that made the
> difference,
The concept here is that, unlike C expressions, Perl expressions can
return a list of values. Some expressions return either a list or a
scalar value depending on the context in which they're used.
The expression "my $a = @foo" evaluates @foo in scalar context, which
(as you probably know) returns the number of elements in @foo.
The expression my ($a) = @foo" evaluates @foo in list context, which
returns a list of the elements in @foo. You can assign the result to
another array, or to a list of scalars.
Note that this is a *very* common idiom that's used to simulate named
arguments:
sub do_stuff {
my ($foo, $bar, $baz) = @_;
...
}
This results in $foo, $bar, and $baz being assigned the first three
elements in @_.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Thu, 18 Nov 2004 21:51:19 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100832788.850404@nntp.acecape.com>
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:ZcqdnarTSYenxADcRVn-rw@adelphia.com...
> daniel kaplan wrote:
> The concept here is that, unlike C expressions, Perl expressions can
> return a list of values. Some expressions return either a list or a
> scalar value depending on the context in which they're used.
>
> The expression "my $a = @foo" evaluates @foo in scalar context, which
> (as you probably know) returns the number of elements in @foo.
>
> The expression my ($a) = @foo" evaluates @foo in list context, which
> returns a list of the elements in @foo. You can assign the result to
> another array, or to a list of scalars.
>
hi sherm,
my apologies, and this sounds like idiocy not letting go, but am having a
problem grapsing here:
in the OP, there was this:
sub1(\%HoH); #Passing the Hoh by reference...
and receieved here within the subroutine:
my $a = @_;
where i can't grasp what i am missing is this...he passed the REFERENCE to
the hash, not the HASH itself....and put it in a SCALAR...where later he
would use that reference as needed...
i am obviosuly missing sometihing here...
------------------------------
Date: Thu, 18 Nov 2004 21:58:13 -0500
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: Problem with Hashes
Message-Id: <4jdnd.41381$Ho4.1545193@news20.bellglobal.com>
"daniel kaplan" <nospam@nospam.com> wrote in message
news:1100832788.850404@nntp.acecape.com...
> "Sherm Pendley" <spamtrap@dot-app.org> wrote in message
> news:ZcqdnarTSYenxADcRVn-rw@adelphia.com...
>> daniel kaplan wrote:
>
>> The concept here is that, unlike C expressions, Perl expressions can
>> return a list of values. Some expressions return either a list or a
>> scalar value depending on the context in which they're used.
>>
>> The expression "my $a = @foo" evaluates @foo in scalar context, which
>> (as you probably know) returns the number of elements in @foo.
>>
>> The expression my ($a) = @foo" evaluates @foo in list context, which
>> returns a list of the elements in @foo. You can assign the result to
>> another array, or to a list of scalars.
>>
>
> hi sherm,
>
> my apologies, and this sounds like idiocy not letting go, but am having a
> problem grapsing here:
>
> in the OP, there was this:
>
> sub1(\%HoH); #Passing the Hoh by reference...
>
> and receieved here within the subroutine:
>
> my $a = @_;
>
> where i can't grasp what i am missing is this...he passed the REFERENCE to
> the hash, not the HASH itself....and put it in a SCALAR...where later he
> would use that reference as needed...
>
> i am obviosuly missing sometihing here...
>
You're missing the obvious, I'm afraid. $a is a scalar; @_ is an array. It
can be written as:
my ($a) = @_;
to assign the first value to $a, or as
my $a = $_[0];
Make things any clearer?
Matt
------------------------------
Date: Thu, 18 Nov 2004 22:25:25 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: Problem with Hashes
Message-Id: <419d653f$1_1@127.0.0.1>
daniel kaplan wrote:
> "John Bokma" <postmaster@castleamber.com> wrote in message
> news:Xns95A5C1F9EC5B6castleamber@130.133.1.4...
>
>>my $a = scalar context here
>>my ($a) = list context here
>>
>>@_ in scalar context results in the number of items in it, hence my $a =
>>@_ puts 1 in it if the sub was called with one parameter.
>>
>
> but i thought that he was passing in a "reference" to a hash....not the hash
> itself....
>
It doesn't matter *what* is passed, the statement:
my $a=@_;
will put the number of elements in array @_ into $a, and the statement
my($a)=@_;
will put the value of the first element of array @_ into $a.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Thu, 18 Nov 2004 22:34:58 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100835368.809503@nntp.acecape.com>
"Bob Walton" <see@sig.invalid> wrote in message
news:419d653f$1_1@127.0.0.1...
wrote:
It doesn't matter *what* is passed, the statement:
my $a=@_;
will put the number of elements in array @_ into $a, and the statement
i JUST realized it's not about what's coming in, it's how it's assigned!
doh! i am going to have to go over the perl modules i just finished (three
total) and make sure i have it all covered...
thanks so much all for poitning it out...
------------------------------
Date: Thu, 18 Nov 2004 22:38:13 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Problem with Hashes
Message-Id: <7oGdnfG2lYUf9wDcRVn-rw@adelphia.com>
daniel kaplan wrote:
> in the OP, there was this:
>
> sub1(\%HoH); #Passing the Hoh by reference...
>
> and receieved here within the subroutine:
>
> my $a = @_;
Of course you're confused - you're looking at the code that didn't work!
Context is Very Important in Perl. The left hand side of an assignment
determines the context in which the right hand side will be evaluated.
In the expression "$a = @_", the left hand side of the assignment is a
scalar, so the right hand side is evaluated in scalar context. The
result of evaluating an array in scalar context is the number of
elements in the array.
One way to assign the first element of @_ to $a is to force the left
hand side of the expression into list context with parenthesis, like this:
my ($a) = @_;
Now the left hand side of the assignment is a *list*. So @_ is evaluated
in list context, which returns a list of elements in @_.
Now, that's not terribly useful just for one argument - he could just as
well have written it like this:
my $a = $_[0];
Or like this:
my $a = shift;
Where this technique *really* pays off is with subroutines that take
multiple arguments. You can assign them all at once, like this:
sub do_something {
my ($foo, $bar, $baz) = @_;
}
Perl doesn't have named arguments, so you'll see this kind of assignment
used a *lot*.
For another example, let's call split() in list context. Let's say you
want to split a row from a tab delimited file, and assign the value of
each column to a scalar. You can do that easily, with:
my ($foo, $bar, $baz) = split(/\t/, $row);
As an aside, the number of elements in @_ is obviously a plain ol'
integer, not a reference of any kind. If the OP had been using 'strict'
and 'warnings' as he should have been doing, Perl would have complained
when he tried to use $a as a hash reference.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Thu, 18 Nov 2004 22:42:36 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100835826.420849@nntp.acecape.com>
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:7oGdnfG2lYUf9wDcRVn-rw@adelphia.com...
> daniel kaplan wrote:
just posted above sherm that i finally got it, it's about how you assign
variables coming INTO a subroutine...what it was passed in, didn't
matter....
thanks for all your tpying
------------------------------
Date: Thu, 18 Nov 2004 23:04:38 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: Problem with Hashes
Message-Id: <JbOdnU7iWMop7QDcRVn-og@adelphia.com>
daniel kaplan wrote:
> just posted above sherm that i finally got it
Yeah, but I didn't see that until I'd already finished writing my reply.
That's what I get for being a windbag, I guess. ;-)
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Thu, 18 Nov 2004 23:13:02 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100837653.451764@nntp.acecape.com>
"Sherm Pendley" <spamtrap@dot-app.org> wrote in message
news:JbOdnU7iWMop7QDcRVn-og@adelphia.com...
> daniel kaplan wrote:
> Yeah, but I didn't see that until I'd already finished writing my reply.
> That's what I get for being a windbag, I guess. ;-)
nobody is a bigger windbad,.....and i only reposted to you in case you
didn't see my last post, and if you did, didn't want you misunderstanding,
thinking i was just thanking one person.....
but thanks
daniel
------------------------------
Date: Thu, 18 Nov 2004 23:25:53 -0500
From: "daniel kaplan" <nospam@nospam.com>
Subject: Re: Problem with Hashes
Message-Id: <1100838424.952380@nntp.acecape.com>
"daniel kaplan" <nospam@nospam.com> wrote in message
news:1100837653.451764@nntp.acecape.com...
> nobody is a bigger windbad,.....
i meant than me....no one is a bigger windbag than me....yikes, wrong words
to leave out, huh?
------------------------------
Date: Thu, 18 Nov 2004 23:29:30 -0500
From: ken <ken@nospam.org>
Subject: regex challange: collapse nested {} blocks
Message-Id: <419d7697$0$576$b45e6eb0@senator-bedfellow.mit.edu>
Okay, I thought this might be a problem that has been addressed, but
Google didn't lead me to anything. I have source code files with blocks
denoted by {}. For exapmle:
loop a1 {
cmd;
loop a2 {
cmd;
cmd;
}
}
loop b1 {
cmd;
...
}
I want hide to (or collapse) all code blocks at a depth > N.
For example, N=1 would collapse loops a1, a2, b1 and output:
loop a1 {}
loop b1 {}
N=2 would only collapse loop a2 in this example:
loop a1 {
cmd;
loop a2 {}
}
loop b1 {
cmd;
...
}
I'd like to get something where N is just a variable in the Perl script,
but for now I'm already stuck just trying to get the N=1 case.
#!/usr/bin/perl
undef $/;
while(<STDIN>) {
s/{.*}/{}/gms;
print;
}
I see why this does not work, but not sure how to proceed. Any help
would be appreciated. Thanks,
Ken
------------------------------
Date: Thu, 18 Nov 2004 21:57:48 -0500
From: Bob Walton <see@sig.invalid>
Subject: Re: script hangs waiting for key stroke
Message-Id: <419d5ec5$1_1@127.0.0.1>
chris wrote:
> I am running the following script on perl, v5.8.4 built for
> MSWin32-x86-multi-thread (activestate). The script runs but seems to
> hang every now and then and won't continue until I press enter. E.g.:
>
> Processing DE 200407
> Processing DE 200408
>
>>>Hang - Press [enter] <<
>
> Processing DE 200409
>
> Do I need to flush buffers or something?
...
> Chris
>
>
> ----------
>
>
> #!/usr/bin/perl -w
>
> use strict;
> use DBD::ODBC;
>
> use util::YYYYMM;
>
> my $start = 200402;
> my $end = 200410;
> my $range_list = range( $start, $end );
>
> foreach my $opco ( qw[ IE UK DE IT GR ES ] ) {
> foreach my $yyyymm (@$range_list) {
> LOAD: {
> print "Processing $opco $yyyymm\n";
> my $dbh = DBI->connect('dbi:ODBC:XXXXX', 'XXXXX', 'XXXXX')
> or redo LOAD;
You're doing an awful lot of connecting and disconnecting from your
database. You should probably open it once at the beginning and close
it once at the end, for performance if nothing else. But doing this
shouldn't make it hang. However, if it fails to open for some reason,
you're looping back and trying exactly the same thing again. If it
fails to open once, chances are pretty good it will continue to fail to
open, hence an infinite loop. But that wouldn't "continue" on a key press.
> my $sth = $dbh->prepare("exec m_run ( '$opco', $yyyymm );")
> or redo LOAD;
If the prepare fails, it is probably not appropriate to just go and open
the database over again. I'm hard pressed to imagine how a prepare that
failed would subsequently succeed on the same database. It would be
best to invoke RaiseError and let DBI take care of the checks and error
bombouts.
> $sth->execute or redo LOAD;
Ditto for the execute.
> $dbh->disconnect;
> }
> }
> }
> my $yyyymm_plus_one = yyyymm_add( $end );
> my $sth = $dbh->prepare("exec m_run_report ( $yyyymm_plus_one );");
Any path to the statement above will not contain a valid database handle
in $dbh -- the database was either disconnected or never connected. But
that should generate a Perl error, not hang for a keystroke.
> $sth->execute;
So I have no idea why your program is hanging waiting for a keystroke.
But I have no problem seeing why it might hang.
Regarding the question about flushing buffers, the only "buffer" you are
using is for STDOUT, and, I think Perl line-buffers it when it is
connected to a terminal anyway, at least on most OS's. Looks like
you're on Windoze; I don't know if it line buffers STDOUT on a cmd
window or not...yep, it does on XP Home SP2, AS build 810, anyway.
One other possibility, I suppose, is whatever the database procedures
you are invoking (like m_run or m_run_report) might do. Could they ever
wait for a keystroke?
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl
------------------------------
Date: Fri, 19 Nov 2004 00:03:03 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Search for string and return file name
Message-Id: <XKand.257$y72.52@clgrps12>
Örjan Johansson wrote:
>
> I'm trying to figure out how to go through all files with a certain
> extension in a directory, search for a string and return the names of the
> files that contains the string. I have written scripts before that opens a
> single file for reading and writing, but have no clue how to go through all
> files in a folder, like a wildcard *.log kind of thing. Any pointers?
perl -lne'/string/ and print $ARGV and close ARGV' *.log
John
--
use Perl;
program
fulfillment
------------------------------
Date: Thu, 18 Nov 2004 17:00:24 -0800
From: "Michael Vilain <vilain@spamcop.net>"
Subject: Re: Simple (!) HTML/PERL code fpr uploading files through web pages entyr field ??
Message-Id: <vilain-22A88F.17002418112004@news.giganews.com>
In article <cnj38a$h93$00$1@news.t-online.com>,
gpohl@email.com (Gerd Pohlmann) wrote:
> I want to offer a possibility to upload a file on a certain (password
> protected) web page.
>
> Ok, I have seen a lot of hugo perl libraries which offer such a
> functionality.
> But I am not the administrator of the server. I cannot install a perl
> library.
> So I am searching for a simpler way to achieve this.
> I can install simple perl scripts and HTML pages - that's all.
>
> Does someone know such a simple perl script ?
>
> Gerd
Actually, someone pointed out to me that you _can_ install a CPAN module
in the home directory of your account and add it's path to your script
using the
use lib LIST;
line. Read the perldoc page on "lib" for more info.
--
DeeDee, don't press that button! DeeDee! NO! Dee...
------------------------------
Date: 19 Nov 2004 01:06:16 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: Simple (!) HTML/PERL code fpr uploading files through web pages entyr field ??
Message-Id: <Xns95A5C2554C61Acastleamber@130.133.1.4>
wrote:
> Actually, someone pointed out to me that you _can_ install a CPAN module
> in the home directory of your account
Yes, done that several times even :-D.
If it's a pure Perl module you can even install the module at home, and
just copy the .pm file(s) (in a similar (sub)dir hierarchy)
--
John Small Perl scripts: http://johnbokma.com/perl/
Perl programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
------------------------------
Date: Thu, 18 Nov 2004 19:28:43 -0800
From: Ken Saunders <bnbliss@comcast.net>
Subject: simple folder search
Message-Id: <02qqp017kkj0db48696elmkt9hav9ta40c@4ax.com>
Below is the script I've been working on. I know theres something
wrong with it but can't find it for the life of me. I've made some
changes to tighen up the logic. Can someone help me get this running.
Its supposed to search my all of my files and return a page with the
matchs. I had this working as an html file and a cgi but when I tried
to merge the two it go broken. Thanks everyone
#!/usr/bin/perl
# Ken Saunders
#11/16/2004
#itc 216
#assignment 3
#this perl script displays a search form, and searchs for matchs on
html files
use strict;
use File::Find;
use CGI;
#variable - query
my $query = param("query");
# There's nothing in the query string
if ($query eq '')
{
print header();
print start_html();
# Displays the search form
print q(<form action="./assign_3.cgi" method="post">);
print q(<input type="text" name="query" size="50" />);
print q(<input type="submit" />);
print q(</form>);
print end_html();
# Bail out early if there are no results to print
exit;
#$filePath = $File::Find::name;
#$filePath =~
s/(home\/classes\/ksaund01\/public_html)/~ksaund01/ig
}
#prints the header wiht the query name in an ordered list
print header();
print start_html();
print "\n<p>For the query $query, these results were
found:</p>\n<ol>\n";
undef $/;
#search
find( sub
{
return if($_ =~ /^\./);
return unless($_ =~ /\.html/i);
stat $File::Find::name;
return if -d;
return unless -r;
open(FILE, "< $File::Find::name") or return;
my $string = <FILE>;
close (FILE);
return unless ($string =~ /\Q$query\E/i);
my $page_title = $_;
if ($string =~ /<title>(.*?)<\/title>/is)
{
$page_title = $1;
}
print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
},
'/~ksaund01/page1.html');
print "</ol>\n";
print end_html();
End
------------------------------
Date: Thu, 18 Nov 2004 22:43:05 -0500
From: Sherm Pendley <spamtrap@dot-app.org>
Subject: Re: simple folder search
Message-Id: <7oGdnfC2lYU49gDcRVn-rw@adelphia.com>
Ken Saunders wrote:
> #itc 216
> #assignment 3
Do your own homework.
sherm--
--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
------------------------------
Date: Thu, 18 Nov 2004 20:17:30 -0800
From: Ken Saunders <bnbliss@comcast.net>
Subject: Re: simple folder search
Message-Id: <1usqp0h1v9tj1osaqof58fajal6a78sdhh@4ax.com>
On Thu, 18 Nov 2004 22:43:05 -0500, Sherm Pendley
<spamtrap@dot-app.org> wrote:
>Ken Saunders wrote:
>
>> #itc 216
>> #assignment 3
>
>Do your own homework.
>
>sherm--
I am doing my own homework. If you look carefully you'll see the
code. Its called asking for help when you get stuck, besides reading,
doing and asking questions are how you learn. I happen to be stuck.
------------------------------
Date: Fri, 19 Nov 2004 05:31:10 +0100
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: simple folder search
Message-Id: <slrncpqtoe.qg.tassilo.von.parseval@localhost.localdomain>
Also sprach Ken Saunders:
> Below is the script I've been working on. I know theres something
> wrong with it but can't find it for the life of me. I've made some
> changes to tighen up the logic. Can someone help me get this running.
> Its supposed to search my all of my files and return a page with the
> matchs. I had this working as an html file and a cgi but when I tried
> to merge the two it go broken. Thanks everyone
It would help if you said in which way it went broken.
> #!/usr/bin/perl
> # Ken Saunders
> #11/16/2004
> #itc 216
> #assignment 3
> #this perl script displays a search form, and searchs for matchs on
> #html files
>
> use strict;
> use File::Find;
> use CGI;
>
> #variable - query
> my $query = param("query");
>
> # There's nothing in the query string
> if ($query eq '')
> {
> print header();
> print start_html();
>
> # Displays the search form
> print q(<form action="./assign_3.cgi" method="post">);
> print q(<input type="text" name="query" size="50" />);
> print q(<input type="submit" />);
> print q(</form>);
>
> print end_html();
>
> # Bail out early if there are no results to print
> exit;
>
> #$filePath = $File::Find::name;
> #$filePath =~
> s/(home\/classes\/ksaund01\/public_html)/~ksaund01/ig
> }
>
> #prints the header wiht the query name in an ordered list
> print header();
> print start_html();
> print "\n<p>For the query $query, these results were
> found:</p>\n<ol>\n";
> undef $/;
>
> #search
> find( sub
> {
> return if($_ =~ /^\./);
> return unless($_ =~ /\.html/i);
> stat $File::Find::name;
> return if -d;
> return unless -r;
This is wrong, although by accident it works. A file test operator works
on $_ when you ommit its parameter. In the File::Find callback, $_ is
just the filename, $File::Find::name is the absolute path.
The above 'stat' will set the current stat buffer to the special token
'_' (same is true for any file test operator). File test operators can
work on those, but yours wont. You have three separate stat() calls: One
on $File::Find::name, and two on $_. File::Find::find() changes into
each directory by default, so those two are equivalent in this context.
So you may just write:
return if -d or ! -r _;
Also note that you never need stat() when all you want is executing some
file test operators.
> open(FILE, "< $File::Find::name") or return;
> my $string = <FILE>;
> close (FILE);
>
> return unless ($string =~ /\Q$query\E/i);
> my $page_title = $_;
> if ($string =~ /<title>(.*?)<\/title>/is)
> {
> $page_title = $1;
> }
> print "<li><a href=\"$File::Find::name\">$page_title</a></li>\n";
> },
> '/~ksaund01/page1.html');
Here's the problem: find() expects a directory (or a list thereof) as
second argument. You're passing it a file (unless of course you have a
directory named 'page1.html'). Pass it a proper directory and I would
expect your script to produce the desired results.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Thu, 18 Nov 2004 22:49:29 -0500
From: wana <ioneabu@yahoo.com>
Subject: Re: the antichomp
Message-Id: <10pqnvrjfn84bca@news.supernews.com>
Uri Guttman wrote:
[snip]
>
> where did you get the impression that it was hard to use? how would
> making a hard to use module be useful? one thing i strive for in all my
> work is making stuff easy to use without sacrificing speed and
> flexibility and file::slurp does all that.
>
> and if you have your lines in an array then this is how you would do it:
>
> use File::Slurp ;
>
> write_file( 'my_file_with_newlines', map "$_\n", @array );
>
> really hard to use!! :)
>
> uri
>
I would not thought of it myself (the use of map), but now I will.
Thanks :-)
My previous language experience is with (in order) Java, Borland C++,
Microsoft C++, and, briefly, PHP. Compared to Perl, they might as well all
be the same. I have always been used to carrying over programming
practices from one language/platform to the next with little change. I am
not used to so much power and expression being built into the language
itself.
Very cool. Thanks for making File::Slurp available. It's my kind of
module. You might want to add to the documentation when you update it that
you can just copy the File directory from the lib directory and use it as
is without the install process for non-root users.
wana
------------------------------
Date: Fri, 19 Nov 2004 03:36:09 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: the antichomp
Message-Id: <x7d5yaa4t6.fsf@mail.sysarch.com>
>>>>> "w" == wana <ioneabu@yahoo.com> writes:
w> I would not thought of it myself (the use of map), but now I will.
w> Thanks :-)
map is easy but for some reason freaks out many perl newbies.
w> Very cool. Thanks for making File::Slurp available. It's my kind
w> of module. You might want to add to the documentation when you
w> update it that you can just copy the File directory from the lib
w> directory and use it as is without the install process for non-root
w> users.
i didn't create the module but i rewrote it and took it over. there is
no need for that doc note as it is true for any pure perl module. and
the FAQ tells you how to install modules into your own space without
needing root.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
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 V10 Issue 7423
***************************************