Re: recommend oo tut
> "Simon" == Simon K Chan <[EMAIL PROTECTED]> writes: Simon> I'm not new to perl, however I would like to pick up some Simon> object-oriented perl. Can anyone out there recommend a good Simon> BEGINNER'S tutorial out there? I've spent some time searching Simon> on Google, couldn't find much for a oo newbie. Right now, I Simon> reading Randal's oo tutorial on CPAN. Well, I'm always interested in feedback about "perlboot". Let me know what's missing for *you*, and what your background is, so that I can better figure out how to connect the concepts I'm trying to teach to your particular background. -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training! -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
need help to output to screen and file simultaneously
This little script is one of my first attempts with perl, it works ok but I would also like to capture the output to file. Help appreciated. Would also appreciate constructive criticism of the script itself.(oh,oh!) The script basically takes a named file, and searches the file for a specified string. ### #!/usr/bin/perl -w print "File to search ?\n"; $logfile = ; chomp $logfile; print "Search for what ?\n"; $string = ; chomp $string; open(LOG,"$logfile") or die "Unable to open $logfile:$!\n"; while(){ print if /\b$string\b/i; } close(LOG); cheers jimf -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: need help to output to screen and file simultaneously
On Feb 17, jimf said: >This little script is one of my first attempts with perl, it works ok >but I would also like to capture the output to file. Help appreciated. >Would also appreciate constructive criticism of the script >itself.(oh,oh!) > >The script basically takes a named file, and searches the file for a >specified string. One thing you might want to add is \Q...\E around the $string in the regex, so that if I enter "(foo)", the regex tries to match a literal '(' instead of thinking I want to capture text. > print "File to search ?\n"; > $logfile = ; > chomp $logfile; > > print "Search for what ?\n"; > $string = ; > chomp $string; > > open(LOG,"$logfile") or die "Unable to open $logfile:$!\n"; Open another file for output: open RESULTS, "> $somefile" or die "Can't write to $somefile: $!\n"; > while(){ > print if /\b$string\b/i; print RESULTS if /\b\Q$string\E\b/i; > } > close(LOG); -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** what does y/// stand for? why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Regular Expressions - matching the first time
On Feb 15, Russ Foster said: >> As of Perl 5.6.2 (not released yet), /^(.*?):/ and >> /^([^:]*):/ will have the same efficiency (read: speed). If >> you're curious, currently /^.*?:/ and /^[^:]*:/ have the same >> speed -- it's the capturing that killed .*?, but I have fixed that. > >Just to be clear, it's the capturing (.*?) of the string that suffers the >most (performance-wise), correct? What do you mean you have it fixed--that >future versions of Perl don't suffer a hit? It's not so much the ACTUAL capturing. Capturing /(.*?):/ and capturing /([^:]*):/ requires the same effort. What killed /(.*?):/ is that normally, Perl's regex engine would optimize /.*?:/ to make .*? jump from one colon to the next in the string. However, because of the internal representation of /(.*?):/, it couldn't do that optimization, and ..*? would move one character at a time, instead of the optimized jump. I made it look a little hard for that optimization. >> And personally, I'd use /([^:]*)/ instead of /^([^:]*):/, >> since they match the same thing (assuming there IS a colon in >> the string). Or I'd use /(.*?):/ instead of /^(.*?):/ but whatever. > >What's the reason for preferring not to use the '^' ? Is it speed? Or just a >desire to make a regex a simple as possible? Personal style. Regexes are already crufty enough -- if I can reduce the amount of content in the regex itself, that's good for me. -- Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** what does y/// stand for? why, yansliterate of course. [ I'm looking for programming work. If you like my work, let me know. ] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to copy and keep file permissions
On Sat, 16 Feb 2002 20:23:02 +, [EMAIL PROTECTED] (Craig Eberly) wrote: >Hey, any help would be appreciated. >I just wanted to make a simple backup script to backup any file i >specified quickly.. and eventually let it accept options and whatnot for >directories, but right now, I'm just trying to get it to keep the file >permissions of the original file. I'm using File::Copy for the >copying, and I thought Digest::MD5 would work for the permissions >problem. > >The backup itself works like this, but the # lines are what i was >trying to do to keep the file permissions. This not only doesnt work, >but it causes the backup process to break as well. Anyone have any >ideas? Here, this shows you the basics of retaining file permissions. #!/usr/bin/perl use warnings; use File::Copy; use strict; my $mode = (stat $ARGV[0])[2]; copy( "$ARGV[0]", "$ARGV[0]\.bak" ); chmod ($mode, "$ARGV[0]\.bak"); print "Backup completed.\n"; exit 0; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: recommend oo tut
On Sat, 16 Feb 2002 20:31:29 -0800 (PST), [EMAIL PROTECTED] (Simon K. Chan) wrote: >Hi All, > >I'm not new to perl, however I would like to pick up some object-oriented perl. >Can anyone out there recommend a good BEGINNER'S tutorial out there? I've spent >some time searching on Google, couldn't find much for a oo newbie. Right now, >I reading Randal's oo tutorial on CPAN. > http://www.perl.com/cs/user/query/q/6?id_topic=19 and don't miss Damian Conways Free Chapters at: http://www.manning.com/getpage.html?project=conway&filename=Chapters.html -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Perspective on $foo [slightly OT]
Hi all, In the recent past, I've noticed that several participants of this list have asked about the meaning / background / derivation of the commonly used variable: $foo. Now, those making this inquiry have had a more than reasonable basis for their confusion. Other than being just a variable, '$foo' does have a subtle, underlying meaning which usually requires a background in American culture to fully appreciate. These questioners are of a group which usually share two attributes: 1) they're new to Perl -- or even new to programming in general -- and, 2) are often non-native English speakers. Their interest in Perl is why we're all here, of course, but their ability to both interact *and* make themselves generally understood by us native English speakers is the truly astounding (and humbling) part. With that in mind, I'd like to offer some perspective, or insight, in the way of a true story, to those of us who are native English speakers and who do have a programming background, but who may be puzzled by this seemingly simplistic question. Way back in the olden days, I took my first, college-level, computer programming course, Introduction to FORTRAN. To understand just how 'way back' it was, I have to confess that our first class quiz was on the Hollorith code. Now, for the younger folks reading this, I should explain that the Hollorith code is what was used to encode the holes found in those infamous punch cards you've all seen in museums. My sense of perspective on the use of '$foo' and its being a sometimes difficult cultural concept comes from what happened to me right after I completed and handed in that first quiz paper. After the class, I walked out with a classmate who had a very worried look on his face. My classmate was from Columbia and, while his second language was an excellent spoken English, his native language was Spanish and his cultural background was, of course, Latin American. We were comparing answers we'd given on the quiz when he asked me about the very last question. It had to do with decoding a simple string of Hollorith code. We compared each of the six characters we'd decoded and decided that our answers were correct as we'd both come up with the same string. I was happy. We'd gotten the 'right' answer and we'd both 'aced' the quiz. But, while I was happy, he still looked worried. "But, friend John," he asked, "I know how the Hollorith code works, but what is a 'PHOOEY'?" It turns out that while he was a great student, he still had much to appreciate about the humor of some American college professors. I learned then, and am reminded by $foo now, that cultural context can mean a lot in learning a new computer language. John-- [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Crop string to x characters
How do I truncate a string to a particular number of characters? This is expensive: $shortdescription =~ s/( ).*/$1/; Gary -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Crop string to x characters
On Sun, 17 Feb 2002 at 22:34 GMT, Gary Hawkins wrote: > --=_NextPart_000_02E4_01C1B7C0.2E6F3B50 > Content-Type: text/plain; > charset="US-ASCII" > Content-Transfer-Encoding: 7bit > > How do I truncate a string to a particular number of characters? > > This is expensive: > > $shortdescription =~ > s/( > ... ).*/$1/; You can use the substr() function, or optimize your regex: #!/usr/bin/perl -w use strict; my $string; # with substr $string = "hello word"; $string = substr($string,0,4); print "substr: $string\n"; # with a regex $string = "hello word"; $string =~ s/^(.{4}).*/$1/; print "regex: $string\n"; __END__ -- briac << dynamic .sig on strike, we apologize for the inconvenience >> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: need help to output to screen and file simultaneously
Jeff 'Japhy' Pinyan wrote: > > On Feb 17, jimf said: > > >This little script is one of my first attempts with perl, it works ok > >but I would also like to capture the output to file. Help appreciated. > >Would also appreciate constructive criticism of the script > >itself.(oh,oh!) > > > >The script basically takes a named file, and searches the file for a > >specified string. > > One thing you might want to add is \Q...\E around the $string in the > regex, so that if I enter "(foo)", the regex tries to match a literal '(' > instead of thinking I want to capture text. > > >print "File to search ?\n"; > >$logfile = ; > >chomp $logfile; > > > >print "Search for what ?\n"; > >$string = ; > >chomp $string; > > > >open(LOG,"$logfile") or die "Unable to open $logfile:$!\n"; > > Open another file for output: > > open RESULTS, "> $somefile" or die "Can't write to $somefile: $!\n"; > > >while(){ > >print if /\b$string\b/i; > > print RESULTS if /\b\Q$string\E\b/i; > > >} > >close(LOG); > > -- > Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ > RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/ > ** Look for "Regular Expressions in Perl" published by Manning, in 2002 ** > what does y/// stand for? why, yansliterate of course. > [ I'm looking for programming work. If you like my work, let me know. ] Thanks "japhy" worked a treat. jimf -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Crop string to x characters
> > How do I truncate a string to a particular number of characters? > > > You can use the substr() function, or optimize your regex: > > #!/usr/bin/perl -w > use strict; > > my $string; > > # with substr > $string = "hello word"; > $string = substr($string,0,4); > print "substr: $string\n"; > > # with a regex > $string = "hello word"; > $string =~ s/^(.{4}).*/$1/; > print "regex: $string\n"; > __END__ > > > -- > briac > << dynamic .sig on strike, we apologize for the inconvenience >> substr 0 wallclock secs ( 0.31 usr + 0.00 sys = 0.31 CPU) regex 2 wallclock secs ( 1.99 usr + 0.00 sys = 1.99 CPU) Thanks, substr over 6 times faster, looks like I get to use it for the first time. Gary #!/usr/bin/perl -w use Benchmark; $t0 = new Benchmark; my $string; # with substr for (1 .. 10) { $string = "hello word"; $string = substr($string,0,4); } $t1 = new Benchmark; $td = timediff($t1, $t0); print timestr($td) . "\n"; $t0 = new Benchmark; # with a regex for (1 .. 10) { $string = "hello word"; $string =~ s/^(.{4}).*/$1/; } $t1 = new Benchmark; $td = timediff($t1, $t0); print timestr($td) . "\n"; __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: text file database question
How about Berkely DB? "Hughes, Andrew" wrote: > I have been given the task to create a contest for which appox. 90,000 > people might be signing up(collected info: name, company, email, phone, > address1, address2). Due to various reasons, I am not able to use a true > database like mySQL to store the information. At this point I am going to > have to use a text file, which I have used in the past, but not at this > level of possible entries (we are really going to be promoting this heavily > on and offline). Please let me know if you think this is a possible > dangerous level of entries for a text file. > > Also, in my experience with text files, I have not been able to stop people > from hitting submit a few times before the form submits (i.e. I have > multiple lines of the same entry). How would I stop this -- still talking > about text files? > > Thanks, > Andrew > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: References/unitialized variable error message
In article <[EMAIL PROTECTED]> wrote "Geoffrey F. Green" <[EMAIL PROTECTED]>: > #!/usr/bin/perl -w > > my %table = ( > "key1" => > {search=> "alpha", > shortname=> "beta", > }, > "key2" => > {search => "gamma", > shortname=> "delta", > }, > "key3" => > {search=> "epsilon", > shortname=> "whatever", > }, >); I'd like to give another hint: I find it dangerous, to repeat in every line search and shortname. The chance of mistyping is really big. Even if you use Copy+Paste (what's never a good solution) you get the trouble when changing later. Again, I would be too lazy - even for writing and for reading :-) Here's one possibility: my %table = map { my ($key, $search, $short) = split / /, $_; ($key => { search=> $search, shortname => $shortname } ) } split /\n/, <<'TABLE'; key1 alpha beta key2 gamma delta key3 epsilon whatever TABLE That of course only goes strait when the keys don't have white spaces. And again, it only makes sense when you will use more than 3 keys a day. Greetings, Andrea -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]