Arrays
Hi Folks. I have a select statement that pulls out names and email addresses from a database . These values then get represented in a multiple select box with the name as the visual value and the email as the posted value. What I want to do is collect all the email values into a array and then post that array off the a foreach loop where I can send a mail to all the addresses in the email array. Sounds simple enough but I just cant get it to work and I am beginning to feel stoopid. This is basically what my code looks like. if ($var == 1) { $SQL=select statement; execute etc etc select name =\email\ size=\5\ multiple while ( ($email, $name)=$sth -fetchrow()) { print option value=\$email\$name/option; } /select } then the form in $var == 1 sends it to $var == 2 if ($var ==2) { foreach $email (@in) { Then here is where I will send the mail. } } Why is this not working? Thanks in advance. Justin
Re: Beginner question
At 11:09 21.05.2001 -0700, you wrote: if ($oldLot[1] == 0) { $arpCount = $arp{$lot}; == Part 1 } else { $arpCount = -; == Part 2 } printf %3s %3d , $arpCount, $count; My problem occurs in the printf at the %3s. Here is the situation: When $oldLot[1] == 0, $arpCount is equal to an integer, but when $oldLot[1] != 0 $arpCount is equal to a character. the printf foramtting will only allow me to print 'either' a character or an integer. how can I do both or how can I change the integer into a character variable so that it will work under the %s? I know this is really un-Perl, but I always try to keep my number variables separate from my string variables. It keeps things clean, and debugging is a lot easier, because you don't have to worry about whether or not Perl is turning a number into a letter for you, or vice-versa. Call it my C++ influence, but I find mixing numbers and strings in the same variable a rather risky business. Aaron Craig Programming iSoftitler.com
Re: use lib directive
I have the same problem with a client of mine -- unfortunately, unless you have your own server, your stuck with their setup. I've gotten around the problem using the I switch in the shebang line: #!/usr/bin/perl -I./lib after than, I can include any module living in my lib directory the same way I include those in the regular lib directory: use strict; use X; use Web::X; # which is in a directory called Web in my lib directory This has worked for me both with UNIX type servers, as well as with Windows (ugh!) servers. At 14:23 21.05.2001 -0400, you wrote: Hello, I am attempting to develop my first module. Because of permissions/security, etc.. I cannot store the module in the standard perl lib directories. So it is currently living in a subdir called lib in my home directory. I use a use lib directive to add this directory to @INC. this works , but when I try then to use another module that does reside in the standard lib directories, I get a Can't locate object method new in module X at path-specified-in -use-lib-directive. When I print the elements of @INC all of the standard directories are printed as is the new one I've specified. I've also tried putting the directory into @INC using push, but I get the same error message. I am using Perl 5.00501 on SunOS 5.6. Thanks! Peter Cline Inet Developer New York Times Digital Aaron Craig Programming iSoftitler.com
Re: useful scripts
When I first started, I found these two sites helpful... http://www.cgi-resources.com/ http://www.hotscripts.com/ Andy On Tue, 22 May 2001, Richard KHOO Guan Chen wrote: Sorry if this is a stupid question Just wondering if there is a site which have useful simple perl scripts for totally clueless people like me to look at? I am actually interested in trimming mail headers (save subject, from etc) for storage Thanks -- Ah Khoo says life will never give you grapes and lavenders everytime occasionally, she will throw you a durian...
Re: Help - Rookie question on Arrays
Hello everyone, Your assumption was right - I am a raw newbie and did not post the follow up questions... but they did lead me to some fairly interesting discoveries on Perl! I really did appreciate your help in solving my problem though. Thanks for your support amit From: Paul [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED], [EMAIL PROTECTED] CC: Aaron Craig [EMAIL PROTECTED], [EMAIL PROTECTED] Subject: Re: Help - Rookie question on Arrays Date: Fri, 18 May 2001 09:07:51 -0700 (PDT) --- Jeff Pinyan [EMAIL PROTECTED] wrote: On May 18, Paul said: @hits = grep /^$TEST1$/, @HOLDER; Do not use a regular expression to check for equality. It breaks far too often. @equal = grep $_ eq $wanted, @original; agreed. Thanks again, Jeff. In trying to keep it simple, I fell asleep. =o) That's two now -- you'll make a programmer out of me yet! lol! For everyone else -- I was thinking in too specific a context. Assuming you know the data is usually a bad idea unless you *really* know your data. Also, I AssUMed the author of the original question knew less than he apparently does, and so knowingly glossed a few things I *did* think of, trying not to babble (I've been told I confuse people...~sigh~) __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/ _ Get your FREE download of MSN Explorer at http://explorer.msn.com
Dynamic regular expressions
Hi people, I need to match string against regular expressions that are only known at run-time. I'm having problems doing it so I made a small test script like the following : #!/net/tcmvega35/data1/automation/perl/bin/perl -w use strict; die test.pl [string] [regex] unless $#ARGV == 1; if ($ARGV[0] =~ $ARGV[1]) { print $ARGV[0] matches $ARGV[1]\n; } else { print $ARGV[0] does not match $ARGV[1]\n; } On the command lines, the following happend allo al = match allo /al/ = no match (should match, no ?) allo lo$= match allo ^al= match allo ^ao$ = no match (should match, no ?) Questions : - Why don't I need the regular expression delimiters (/), is it implicit when using variables ? - If I don't add them (/), will it still work for all cases ? - Why doesn't the last case work ? Thanks ! -Robin
RE: Dynamic regular expressions
Just wanted to point out that the pair: allo ^ao$ doesn't work, because this regex matches ao on a line by itself. you would have to perform two matches, one for ^a and another for o$ in order to match te string allo this way. As for why the '/' doesn't cooperate, it has to do with the fact that when you parse it as part of a variable, the '/' becomes part of the string, rather than a trigger to the regex evaluator that this is a regular expression. I noticed the same thing in AWK, if I put the /al/ inside quotes, thus, /al/ it didn't match, but without the quotes, it worked fine. -Original Message- From: Robin Lavallee (LMC) [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 22, 2001 8:23 AM To: [EMAIL PROTECTED] Subject: Dynamic regular expressions Hi people, I need to match string against regular expressions that are only known at run-time. I'm having problems doing it so I made a small test script like the following : #!/net/tcmvega35/data1/automation/perl/bin/perl -w use strict; die test.pl [string] [regex] unless $#ARGV == 1; if ($ARGV[0] =~ $ARGV[1]) { print $ARGV[0] matches $ARGV[1]\n; } else { print $ARGV[0] does not match $ARGV[1]\n; } On the command lines, the following happend allo al = match allo /al/ = no match (should match, no ?) allo lo$ = match allo ^al = match allo ^ao$ = no match (should match, no ?) Questions : - Why don't I need the regular expression delimiters (/), is it implicit when using variables ? - If I don't add them (/), will it still work for all cases ? - Why doesn't the last case work ? Thanks ! -Robin
Re: Dynamic regular expressions
Robin Lavallee wrote: : On the command lines, the following happend : : allo al = match : allo /al/ = no match (should match, no ?) No, because allo doesn't contain any slashes. : allo lo$ = match : allo ^al = match : allo ^ao$ = no match (should match, no ?) No. This expression would only match the string ao. I think what you mean is ^a|o$. : : Questions : : : - Why don't I need the regular expression delimiters (/), is it : implicit when using : variables ? Apparently so, though I've never used it that way. : - If I don't add them (/), will it still work for all cases ? Again, apparently so, though I've never used them that way. One advantage to using slashes (or any kind of delimiter with the m operator; e.g., m{}, m::, etc) is that you can add options like o, which means compile the regex just once (m/$ARGV[0]/o). This can speed things up if you're using the same regex to do a lot of matching. (o has no effect if the regex is a constant expression.) : - Why doesn't the last case work ? See above. -- tdk
Re: Dynamic regular expressions
Good day; See below for what I think may be the issues...Haven't tried it out before, but hope this helps. At 09:23 AM 5/22/2001 -0400, Robin Lavallee (LMC) wrote: Hi people, I need to match string against regular expressions that are only known at run-time. I'm having problems doing it so I made a small test script like the following : #!/net/tcmvega35/data1/automation/perl/bin/perl -w use strict; die test.pl [string] [regex] unless $#ARGV == 1; if ($ARGV[0] =~ $ARGV[1]) { print $ARGV[0] matches $ARGV[1]\n; } else { print $ARGV[0] does not match $ARGV[1]\n; } On the command lines, the following happend allo al = match allo /al/ = no match (should match, no ?) No, because it's looking to match the literal (/). If the left hand value had (/), then it would match. allo lo$= match allo ^al= match allo ^ao$ = no match (should match, no ?) No, see below. Questions : - Why don't I need the regular expression delimiters (/), is it implicit when using variables ? Apparently so. I've never used =~ with two arguments before. (There are some REGEXperts out there who may be able to help you more) - If I don't add them (/), will it still work for all cases ? I'm not sure, but it seems by your test cases that if you don't use (/) it should work for all cases. - Why doesn't the last case work ? It's looking for a string that begins with ao (^ao) and ends with ao (ao$). If your argument was ao, it should match (and I think aoanything else hereao would match as well). Thanks ! -Robin
Re: Arrays
if ($var ==2) { foreach $email (@in) { Then here is where I will send the mail. } } Why is this not working? Can you tell us a bit more about the problem. Are there any error-messages, etc? A bit more code would be helpfull. Especially the part where @in is filled. Because if @in is empty you won't send any mails. cr
Re: Dynamic regular expressions
Try this: my $stringToMatch = hello; my $runtimeRegExp = he; print $stringToMatch. world if($stringToMatch =~ /$runtimeRegExp/gi); At 09:23 22.05.2001 -0400, you wrote: Hi people, I need to match string against regular expressions that are only known at run-time. I'm having problems doing it so I made a small test script like the following : #!/net/tcmvega35/data1/automation/perl/bin/perl -w use strict; die test.pl [string] [regex] unless $#ARGV == 1; if ($ARGV[0] =~ $ARGV[1]) { print $ARGV[0] matches $ARGV[1]\n; } else { print $ARGV[0] does not match $ARGV[1]\n; } Aaron Craig Programming iSoftitler.com
RE: error using sendmail
Thanks for the response Peter! This is being performed through a browser as part of an admin tool and when I added the below code as suggested the entire file is sent but all the MIME info is displayed on the screen. How can I eliminate this, as it would just confuse the user. This is what I added after the shebang line: BEGIN { $| = 1; print Content-type: text/html\n\n } I put it at the beginning of the subroutine that sends out the email but zero are sent out: foreach my $email_address (@email) { $| = 1; print Content-type: text/html\n\n open (MAIL, | /var/tmp/sendmail -oi -t -X /var/tmp/sendmail_email.log) || display_error(Mail error, Can't open sendmail $!); print MAIL To: $email_address\n; print MAIL From: $from\n; print MAIL Cc: \n; print MAIL Subject: $subject_message\n\n; print MAIL $email_text\n; close (MAIL) || display_error(Mail error, Can't close sendmail $!); } Any advice would be appreciated, Regards, Mike Stussie -Original Message- From: Peter Scott [SMTP:[EMAIL PROTECTED]] Sent: Monday, May 21, 2001 2:34 PM To: Stussie, Mike; [EMAIL PROTECTED] Subject: Re: error using sendmail At 02:17 PM 5/21/01 -0500, Stussie, Mike wrote: I am trying to send an email to users based on a list of emails that have been read into an array. The array contains about 600 records but it abends at around 200. Woo, I'll answer someone who knows what ABEND means :-) Your script is producing output that violates the HTTP protocol. Rather than a whole song and dance about this (which takes about a chapter - I know, there's a chapter on it in my book :-) just put this into your script after the #! line: BEGIN { $| = 1; print Content-type: text/plain\n\n } and the next time you run it you should see the reason. -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com *** WARNING: All e-mail sent to and from this address will be received or otherwise recorded by the A.G. Edwards corporate e-mail system and is subject to archival, monitoring or review by, and/or disclosure to, someone other than the recipient. ***
Beginner question
Hello all, I'm trying to populate an array from a file, I think I've got the array populated, however, I'm not sure. I then want to compare an input against the array to determine if its there, then look at the second component of each record. Any assistance would be greatly appreciated. Thanks, Mark
Re: Beginner question
On May 22, Mark on GCI Server said: I'm trying to populate an array from a file, I think I've got the array populated, however, I'm not sure. I then want to compare an input against the array to determine if its there, then look at the second component of each record. Any assistance would be greatly appreciated. Do you really need to store the entire file in memory? And from the sound of it, you'd probably want to use a hash, instead. open PASSWORDS, passwd or die can't read passwd: $!; while (PASSWORDS) { chomp; my ($username, $password) = split /=/; $pwd{$username} = $password; } close PASSWORDS; if (exists $pwd{$who}) { # $who was in the file } You might not even need to store it in a hash: open PASSWORDS, passwd or die can't read passwd: $!; while (PASSWORDS) { chomp; my ($username, $password) = split /=/; if ($who eq $username) { # ... } } close PASSWORDS; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** I need a publisher for my book Learning Perl's Regular Expressions **
RE: Beginner question
The better point is to provide the list with a snippet of the code and the individuals are then more than willing to assist. Otherwise necessary information may be omitted and you go down the wrong path. Wags ;) -Original Message- From: Mark on GCI Server [mailto:[EMAIL PROTECTED]] Sent: Tuesday, May 22, 2001 09:03 To: [EMAIL PROTECTED] Subject: Beginner question Hello all, I'm trying to populate an array from a file, I think I've got the array populated, however, I'm not sure. I then want to compare an input against the array to determine if its there, then look at the second component of each record. Any assistance would be greatly appreciated. Thanks, Mark
Re: Beginner question
On May 22, Mark on GCI Server said: open(TESTER, password.dat) or die File cannot be opened.\n; print Enter a username: ; $input = STDIN; You need to chomp $input, since it has a newline at the end. $x = 0; $y = 0; $w = 0; $z = 1; if (TESTER ne ) { That reads a line (and it is lost forever). while($line = TESTER) { chomp($line); ($stuname, $stupasswd) = split(/,/,$line); @students[$x,$y] = ($stuname); @students[$w,$z] = ($stupasswd); 2-d arrays do not work this way in Perl. You need to say: $students[$x][$y] = $stuname; $students[$x][$z] = $stupasswd; The $w variable here is useless. Doing @array[$x,$y] is the same as ($array[$x], $array[$y]) -- that is, you get two elements from the array. $x = $x + 1; $w = $w + 1; print @students[$x,$y]; }; if ($stuname = $input) { print Username is $stuname and their student number is $stupasswd\n; } else { print Unauthorized user $input, please check your spelling.\n; } close(TESTER); } You don't need an array here at all, and you probably don't need a hash. print Enter a username: ; chomp(my $input = STDIN); open TESTER, password.dat or die can't read password.dat: $!; while (TESTER) { chomp; my ($u, $p) = split /,/; if ($u eq $input) { print Username: $u\n; print Password: $p\n; last; # this stops reading from the file } } close TESTER; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** I need a publisher for my book Learning Perl's Regular Expressions **
ternary operator stability(e.g., in case statements)
Hi all. I know that in many C compilers, the a ? b : c construct with the ternary ?: operator si not stable after the second or third nesting, but I've never seen that sort of problem in tests I've run in Perl. Anybody know if there would likely be any problem with building a case statement like the folowing (without installing Switch.pm)? sub rate ($) { $_[0] eq 'A' ? .03 : $_[0] eq 'B' ? .05 : $_[0] eq 'C' ? .06 : .08; # the default } Does anyone know of any arbitrary limit on this sort of structure? __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: ternary operator stability(e.g., in case statements)
On Tue, May 22, 2001 at 09:37:13AM -0700, Paul wrote: Anybody know if there would likely be any problem with building a case statement like the folowing (without installing Switch.pm)? sub rate ($) { $_[0] eq 'A' ? .03 : $_[0] eq 'B' ? .05 : $_[0] eq 'C' ? .06 : .08; # the default } Does anyone know of any arbitrary limit on this sort of structure? Apparently not: print join(\n, rate('A'), rate('B'), rate('C'), rate('D'), undef); produces: 0.03 0.05 0.06 0.08 If you were to add more rates, it'll get unmaintainable and confusing pretty quickly. You're doing simple string comparisons. Try using a hash instead: my %rate = ( A = .03, B = .05, C = .06, default = .08, ); my $default_rate = .08; sub rate ($) { return $rate{$_[0]} || $rate{default}; } You could also elimiate the function by examining the hash directly in your code, depending on how you use it. Z.
RE: error using sendmail
At 10:30 AM 5/22/01 -0500, Stussie, Mike wrote: Thanks for the response Peter! This is being performed through a browser as part of an admin tool and when I added the below code as suggested the entire file is sent but all the MIME info is displayed on the screen. How can I eliminate this, as it would just confuse the user. You misunderstand - that wasn't a fix for the problem, that was so we could diagnose what the problem was. This is what I added after the shebang line: BEGIN { $| = 1; print Content-type: text/html\n\n } Nope, that should be text/plain. It'll be easier to read. I put it at the beginning of the subroutine that sends out the email but zero are sent out: foreach my $email_address (@email) { $| = 1; print Content-type: text/html\n\n Get rid of that line. Run the CGI from a browser through the web server. See what output is produced. Show us the first 10 lines, and we'll explain the problem. Something is happening in your email subroutine that is causing output to go to the web server, and we need to see what it is. open (MAIL, | /var/tmp/sendmail -oi -t -X /var/tmp/sendmail_email.log) || display_error(Mail error, Can't open sendmail $!); print MAIL To: $email_address\n; print MAIL From: $from\n; print MAIL Cc: \n; print MAIL Subject: $subject_message\n\n; print MAIL $email_text\n; close (MAIL) || display_error(Mail error, Can't close sendmail $!); Also pass $? to display_error. } Any advice would be appreciated, Regards, Mike Stussie -Original Message- From: Peter Scott [SMTP:[EMAIL PROTECTED]] Your script is producing output that violates the HTTP protocol. Rather than a whole song and dance about this (which takes about a chapter - I know, there's a chapter on it in my book :-) just put this into your script after the #! line: BEGIN { $| = 1; print Content-type: text/plain\n\n } and the next time you run it you should see the reason. -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com
Re: Multiple submit buttons CGI question
Dave wrote: : Can any one tell where I went wrong here? : (I remember reading that a html form can have : multiple submit forms as long as you parse them : via their value) : ... : In the html: : input type=submit value=addinput type=submit value=sub These tags need a name attribute. Without it, the values will not be bound to a CGI parameter name. e.g., input name=submit_action value=addinput name=submit_action value=sub Then in the script: if ( $FORM_DATA{submit_action} eq 'add' ) { ... } -- tdk
RE: Multiple submit buttons CGI question
I believe multiple submit buttons only send off the information for the form they are a part of. If you have multiple forms, clicking one submit button will only get you that form sent off. At 10:18 22.05.2001 -0700, you wrote: In the html: input type=submit value=addinput type=submit value=sub the 'value' attribute just set's up what the button text is, not a unique identifier for the button, so like tdk wrote, you need a name attribute to get the behavior you want. Aaron Craig Programming iSoftitler.com
RE: Multiple submit buttons CGI question
I believe multiple submit buttons only send off the information for the form they are a part of. If you have multiple forms, clicking one submit button will only get you that form sent off. But you can have more than one submit button per form. This is useful when you want something like, one button to add a record and one button remove a record but both functions operate on the same data. I think it's this kind of functionality that David is after, and to do it he can just name the buttons 'Add' and 'Sub' and then check to see which one got pressed.
RE: Multiple submit buttons CGI question
Aaron Craig wrote: : I believe multiple submit buttons only send off the information for the : form they are a part of. If you have multiple forms, clicking one submit : button will only get you that form sent off. This is true (unless you're into Javascript). However, a single form can have multiple submit buttons, each of which can have different values (the value of a submit button is also its label). But this is getting off-topic. -- tdk
Re: Beginner Question
Try this. my $text = Browser/Version Platform; my ($keep,$discard) = split / /, $text; print $keep\n; This splits on space and saves the part you want to the variable $keep and the rest to $discard. Also you could use a regular expression like such: my $text = Browser/Version Platform; $text =~ /(.+)\s.+/; print $1\n; This matches any character (one or more, indicated by +) before a space (\s) and saves the characters before the space into a global variable $1 - called a backreference which can than be referred to later on. Good luck! At 02:14 PM 5/22/01 -0400, you wrote: I need a way to return the start of a line up to the first whitespace. eg Browser/Version Platform I want just the Browser/Version information returned. It should be simple, but for some reason the solution eludes me. Peter Cline Inet Developer New York Times Digital
Re: ternary operator stability(e.g., in case statements)
--- Jeff Pinyan [EMAIL PROTECTED] wrote: On May 22, Paul said: I know that in many C compilers, the a ? b : c construct with the ternary ?: operator si not stable after the second or third nesting, but I've never seen that sort of problem in tests I've run in Perl. The only to watch out for is precendence: $a ? $b = $c : $b = $d; is like ($a ? $b = $c : $b) = $d; which always sets $b to $d. Yup, tho in that case I'd usually say $b = $a ? $c : $d; sub rate ($) { $_[0] eq 'A' ? .03 : $_[0] eq 'B' ? .05 : $_[0] eq 'C' ? .06 : .08; # the default } That's fine, but I prefer to use hashes for that sort of thing. { my %rates = qw( A .03 B .05 C .06 ); sub rate { exists $rates{$_[0]} ? $rates{$_[0]} : .08 } } Yep! But the actual function has a little more processing: sub charge ($) { # receives a boolean; rerate or not my $band = @_ and $routes{$mf03-route} ? 0 : $mf03-band; my $mou = $mf03-bmou*($peak{$mf03-tod}||.50) #50% off-peak discount if $band; # wasted effort on 'X', but that's rare return int(100 * sprintf %.2f, $band eq '0' ? .12 : $band eq 'X' ? .12 : $band eq 'A' ? .04 + $mou * .03 : # setup is .04 per call $band eq 'B' ? .04 + $mou * .05 : $band eq 'C' ? .04 + $mou * .06 : .04 + $mou * .08); } Still tossing this salad, but I think I've got all my veggies in it now. =o) __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: ternary operator stability(e.g., in case statements)
--- Adam Turoff [EMAIL PROTECTED] wrote: On Tue, May 22, 2001 at 09:37:13AM -0700, Paul wrote: Anybody know if there would likely be any problem with building a case statement like the folowing (without installing Switch.pm)? sub rate ($) { $_[0] eq 'A' ? .03 : $_[0] eq 'B' ? .05 : $_[0] eq 'C' ? .06 : .08; # the default } Does anyone know of any arbitrary limit on this sort of structure? Apparently not: print join(\n, rate('A'), rate('B'), rate('C'), rate('D'), undef); produces: 0.03 0.05 0.06 0.08 Oh, I knew it'd work this deep. I was just thinking ahead for bigger tables, but this program won't need them. Sorry if I misled you. If you were to add more rates, it'll get unmaintainable and confusing pretty quickly. Absolutely, but this is a known table that can't be changed without federal agreement, and any change would more likely be simplification than expansion. You're doing simple string comparisons. Try using a hash instead: my %rate = ( A = .03, B = .05, C = .06, default = .08, ); my $default_rate = .08; sub rate ($) { return $rate{$_[0]} || $rate{default}; } You could also elimiate the function by examining the hash directly in your code, depending on how you use it. Japhy said the same thing. =o) However, there's a little more code actually in the function, including other calculated values used in a formula in the assignment, and retrieval of properties off a deep-bound static object that gets initialized elsewhere in the program. (Still in beta) See my previous response in this thread for a more complete liting of the code in it's current form. __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Passing command line arguements
We have moved a website off a server outside our offices to one inside. everything went fine but one of our cgi is not working so using some info shared in this forum we tried to run from the command line. now here is the problem we cannot get it to pass the arg. I have tried: Perl cnty-nos.cgi 89 1 -w Perl cnty-nos.cgi 89, 1 am I needing the -w before the arguments and are commas used in the line Richard Thompson Application Programmer/Analyst Kansas Dept. of Transportation Bureau of Computer Services Internet/Intranet Support Phone: (785)-296-8647 FAX: 785-296-6222 Email: [EMAIL PROTECTED]
Re: Passing command line arguements
On May 22, Richard Thompson said: We have moved a website off a server outside our offices to one inside. everything went fine but one of our cgi is not working so using some info shared in this forum we tried to run from the command line. now here is the problem we cannot get it to pass the arg. Perl cnty-nos.cgi 89 1 -w Perl cnty-nos.cgi 89, 1 am I needing the -w before the arguments and are commas used in the line -w is an option to perl, not to your program perl -w your_program arg1 arg2 arg 3 here Commas aren't used. -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** I need a publisher for my book Learning Perl's Regular Expressions **
Re: Passing command line arguements
Richard Thompson wrote: We have moved a website off a server outside our offices to one inside. everything went fine but one of our cgi is not working so using some info shared in this forum we tried to run from the command line. now here is the problem we cannot get it to pass the arg. If it really is a script written for cgi, sending arguments via command line won't work. When a cgi script runs information is available to that script because the web server provides an environment from which the script can access the information. The reason you might want to run a cgi script via command line is to check that it compiles and runs without errors. After that it's a matter of debugging the script in the web environment. Perl scripts run from a command line have access to command line values via the @ARGV array. Unless this script does something with @ARGV, the command line params will not be available to it. Dan
Re: Passing command line arguements
Dan Brown wrote: : If it really is a script written for cgi, sending arguments via command : line won't work. When a cgi script runs information is available to : that script because the web server provides an environment from which : the script can access the information. If the script is written using CGI.pm, then it will be able to pick up input from the command line: % my_cgi_script foo=bar etc. In fact, if you run a CGI.pm script from the command line with no arguments, it will say: (offline mode: enter name=value pairs on standard input) so you can enter them via stdin. If it doesn't use CGI.pm, it should. ;) -- tdk
autonumbered data fields in ODBC database.
I'm writing a form that inserts into a database table (a header record) with an autonumbered field (system assigned). Ordinarily, the value of the serial field assignment in an SQL database is stored in sqlca.sqlerrd[2], but I can't find any way to reference this in the documentation I have for Win32::ODBC. Does anyone have any ideas how I can get at this value? I need it as part of the foreign key relationship for detail records. ___ Kristopher Cook (mailto:\\[EMAIL PROTECTED]) e-Commerce Director Galyan's Trading Company (317) 532-0200 x239 (317) 532-0258 (fax)
Require
Hello All, I am new to Perl and putting together a script that needs about 20 reasonably complex subroutines. To keep the code as modular and managable as possible I would prefer not to define all the routines in the main script. It seems like using 'require' is the way to go (since I don't have the time or experience to do modules at the moment). I can get the required scripts and subroutines to call OK when there is only one sub per required file and I don't have to pass arguments. When I try to pass args they don't seem to get picked up by the called script. Is this possible? Can someone give me some guidance or point me in the direction of the appropriate doco? I have looked at some books etc but can't seem to find the info that I need. Thanks in advance. john
Re: Require
john wrote: : It seems like using 'require' is the way to go (since I don't have the time or : experience to do modules at the moment). : : I can get the required scripts and subroutines to call OK when there is only one : sub per required file and I don't have to pass arguments. : : When I try to pass args they don't seem to get picked up by the called script. : Is this possible? How are you writing the require files? It sounds like you're trying to run the subroutine code by requiring the files. The way to write them is to put the subroutines definitions into the file, require the file *just once*, and call the subroutines by name. Something like this: === mySubs.pl === sub f2c { return ($_[0] - 32) * 5 / 9; } sub c2f { return $_[0] * 9 / 5 + 32; } === myScript === require 'mySubs.pl'; my $celsius = f2c(73); my $fahrenheit = c2f(30); # etc... My apologies if I'm misunderstanding you. -- tdk
Re: Require
--- [EMAIL PROTECTED] wrote: To keep the code as modular and managable as possible I would prefer not to define all the routines in the main script. It seems like using 'require' is the way to go (since I don't have the time or experience to do modules at the moment). Modules are actually pretty easy. Try it: make a file in the same directory as a test script. Call it abc.pm, and paste in this: package abc; # this declares the namespace for the module sub argtest { print recieved: , join(';', @_),\n; } 1; # always add this at the end of a module now make test script tst.pl: use abc; abc::argtest(1..3); then run it with perl tst.pl It should print: recieved: 1;2;3 That's a horrifically simplistic example, but it's a good quick-start. =o) Then read perldoc perlmod perlmodlib Worlds more to see in the OO examples, but that's for later. ;o] __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re:[OT]Require
--- Paul [EMAIL PROTECTED] wrote: sub argtest { print recieved: , join(';', @_),\n; } sorry -- recEIved :o __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: Require
At 01:51 PM 5/22/01 -0700, Paul wrote: --- [EMAIL PROTECTED] wrote: To keep the code as modular and managable as possible I would prefer not to define all the routines in the main script. It seems like using 'require' is the way to go (since I don't have the time or experience to do modules at the moment). Modules are actually pretty easy. Try it: make a file in the same directory as a test script. Call it abc.pm, and paste in this: package abc; # this declares the namespace for the module sub argtest { print recieved: , join(';', @_),\n; } 1; # always add this at the end of a module now make test script tst.pl: use abc; abc::argtest(1..3); It's always seemed to me that the 'approved' way of creating modules to import routines into another script was a bit wordy. In a nutshell, you have to do something like package abc; use Exporter; @ISA = qw(Exporter); @EXPORT = qw(foo bar baz); @EXPORT_OK = qw(blech flurble); sub foo { ... } And that's not even with proper strictness enabled. Now there are lots of advantages to this approach - ability to use private package variables, hide subroutines from the caller, allow them to selectively import symbols, etc. But there's a lot to learn in there for someone who just wants to abstract some common routines into one place, which someone who'd only been learning Perl for a week might want to do. So I'd suggest that someone wanting to do that, if they don't yet know how to use the Exporter and other terminology above, just stick the routines into a file and require them, subject to the following caveats: Don't declare any package variables in the file unless they're constants you want visible to the main script. Declare lexical variables all you want, they'll remain private. You won't have any 'private' subroutines in that file. That'll have to be okay. Just bear in mind that there's a whole new world of wonderful stuff behind door #2 when you're ready for it. -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com
Re: copying an array
- Original Message - From: Jeff Pinyan [EMAIL PROTECTED] To: Aaron Craig [EMAIL PROTECTED] OTOT (off the original topic) Before I answer your question, I have to ask you to not use subroutine prototypes. 9 out of 10 Perl programmers use them incorrectly or don't know what they do. The problem is: PROTOTYPES MUST BE SEEN BEFORE THE FUNCTION IS CALLED. So few people realize that (for one reason or another[1]). I've seen this statement before and do not really understand it, having come from a 'true-compiler' background. It is the 'seen' that puzzles me. If I have a 'require' then I believe the prototype is inspected. If the subroutine occurs at the front of the 'main' code (ie before being used) then I believe the prototype is inspected However if I have code that goes: dummysub( ) realsub('paulus') . . sub realsub($) {} sub dummysub( ) {...} Has the realsub prototype been analyzed? I guess what I am asking is what constitutes seeing? Please correct glaring mistakes, misapprehensions, omissions and apocryphal statements. Regards - Paul Cotter
Mailer
I want to add a Mailer to my site, where can I find a simple perl script to handle this? Chris Tunnell
Sendmail
I use a program called activeperl to run perl scripts on my W2k machine. Does anyone know where the sendmail is located Chris Tunnell
Re: Beginner Question
On May 22, Peter Cline said: Try this. my $text = Browser/Version Platform; my ($keep,$discard) = split / /, $text; print $keep\n; This splits on space and saves the part you want to the variable $keep and the rest to $discard. Actually, it splits on EXACTLY one space. That can cause problems: @parts = split / /, this and that; { local $ = ) (; print (@parts); } # (this) () (and) (that) You might want to use the special split ' ' syntax: @parts = split ' ', this and that; { local $ = ) (; print (@parts); } # (this) (and) (that) my $text = Browser/Version Platform; $text =~ /(.+)\s.+/; print $1\n; That reads too much into $1. The OP asked for the FIRST whitespace, and you've given him the LAST whitespace. The second .+ is also not needed, and can break certain cases: foobar =~ /(.+)\s.+/; # can't match! foo bar blat =~ /(.+)\s.+/; # matches foo bar Here's the output from 'explain': NODE EXPLANATION -- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): -- (group and capture to \1: -- .+ any character except \n (1 or more times (matching the most amount possible)) -- )end of \1 -- \s whitespace (\n, \r, \t, \f, and ) -- .+ any character except \n (1 or more times (matching the most amount possible)) -- )end of grouping -- So you see that the .+ matches AS MUCH AS IT CAN. How about: ($wanted) = $string =~ /(\S*)/; or ($wanted) = split ' ', $string; -- Jeff japhy Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/ Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/ Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/ Acacia Fraternity, Rensselaer Chapter. Brother #734 ** I need a publisher for my book Learning Perl's Regular Expressions **
Re: Sendmail
- Original Message - From: Chris Tunnell [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, May 22, 2001 11:23 PM Subject: Sendmail I use a program called activeperl to run perl scripts on my W2k machine. Does anyone know where the sendmail is located Yes, on the nearest unix machine. Try the MIME::Lite module
Re: Beginner Question
Thanks for catching my errors. I was thinking too narrowly, using just the one template provided. This can of course be dangerous, for things change. At 05:30 PM 5/22/01 -0400, Jeff Pinyan wrote: You might want to use the special split ' ' syntax: @parts = split ' ', this and that; { local $ = ) (; print (@parts); } # (this) (and) (that) Will the ) ( assign any amount of space to the list separator? This is interesting. I haven't encountered this syntax before. my $text = Browser/Version Platform; $text =~ /(.+)\s.+/; print $1\n; That reads too much into $1. I was not taking into account the greedy nature of the Perl regular expression engine. I was assuming that the data would always be in the format specified in the template. The OP asked for the FIRST whitespace, and you've given him the LAST whitespace. This raises the issue of process and having good specs to work from and following them etc. I did not follow the spec provided :-( Peter Cline Inet Developer New York Times Digital
Re:[OT]function prototyping (was: copying an array)
- Original Message - From: Jeff Pinyan [EMAIL PROTECTED] To: Aaron Craig [EMAIL PROTECTED] Before I answer your question, I have to ask you to not use subroutine prototypes. 9 out of 10 Perl programmers use them incorrectly or don't know what they do. The problem is: PROTOTYPES MUST BE SEEN BEFORE THE FUNCTION IS CALLED. So few people realize that (for one reason or another[1]). For that reason, I usually do my function definitions at the top of my programs. That way they've already been thoroughly parsed before ever being called. I used to do that in my C code, too. One problem there, though, is that you have to look for where the actual program begins. Good documentation helps, but that sort of structure can hinder human-readability. = print Just another Perl Hacker\n; # edited for readability =o) = Real friends are those whom, when you inconvenience them, are bothered less by it than you are. -- me. =o) = There are trivial truths and there are great Truths. The opposite of a trival truth is obviously false. The opposite of a great Truth is also true. -- Neils Bohr __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: copying an array
On Tue, May 22, 2001 at 05:26:19PM -0400, Jeff Pinyan wrote: On May 21, Paul Cotter said: dummysub( ) realsub('paulus') . . sub realsub($) {} sub dummysub( ) {...} It's probably also worth mentioning that calling subroutines in this manner, ie with the leading , bypasses the prototype checking anyway. -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net
Re: Modules (was: Require)
--- Peter Scott [EMAIL PROTECTED] wrote: It's always seemed to me that the 'approved' way of creating modules to import routines into another script was a bit wordy. In a nutshell, you have to do something like package abc; use Exporter; @ISA = qw(Exporter); @EXPORT = qw(foo bar baz); @EXPORT_OK = qw(blech flurble); sub foo { ... } And that's not even with proper strictness enabled. But you don't *have* to do any of that. Exporter is often only use to pollute the main package namespace. It's an awesome module, but almost never something you *have* to use. You can always just specify the namespace where the functions reside. That said, I might agree that require is a better mechanism in many cases, but when I first started looking for ways to modularize my code, I looked at require in perlfunc and saw: For a yet-more-powerful import facility, see use and perlmod. Being too much the gearhead, I went straight there and have virtually never used a require statement in a script. __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
Re: Sendmail
--- M.W. Koskamp [EMAIL PROTECTED] wrote: - Original Message - From: Chris Tunnell [EMAIL PROTECTED] To: [EMAIL PROTECTED] Sent: Tuesday, May 22, 2001 11:23 PM Subject: Sendmail I use a program called activeperl to run perl scripts on my W2k machine. Does anyone know where the sendmail is located Yes, on the nearest unix machine. Try the MIME::Lite module Or Mail::Sendmail, also on CPAN. __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
foreach confusion
As an exercise here I want to play with this for a few minutes, and have a few basic questions #!/usr/bin/perl # First question do I use # ( ) OR { ... } @lines = qq{dave john mike drew}; # First error, should be qw( ... ) # qq is garbage, right? # is the ',' correct in my($a,$b) can you use a space my($a $b @c) my (%sortKeys,$i); foreach ( @lines ) { print$_$i\n; my $sortKey = $i++; # do something to create the sort key, # using %idToName to map the ID to the name $sortKeys{$_} = $sortKey; } # Then create a sub to pass to sort: print @lines $i\n; # Could you elaborate on this # $a $b are not passed any values -- don't understand sub bySortKey { $sortKey{$a} cmp $sortKey{$b} } print bySortKey; exit(0); # If script executes successfully do you exit(1) OR exit(0); Thanks Dave G.
Re: foreach confusion
At 07:47 PM 5/22/01 -0400, David Gilden wrote: As an exercise here I want to play with this for a few minutes, and have a few basic questions #!/usr/bin/perl # First question do I use # ( ) OR { ... } In the below, either, they are just delimiters. However, given what I am intuiting your level of expertise to be, I recommend you stick with () for qw everywhere for now. And also that you make sure you know that @lines = (dave, john, mike, drew); does the same thing and that the thing on the right is a list. @lines = qq{dave john mike drew}; # First error, should be qw( ... ) Correct. # qq is garbage, right? No, it's like saying @lines = dave john mike drew; which puts the one element in @lines. # is the ',' correct in my($a,$b) can you use a space my($a $b @c) No. my (%sortKeys,$i); foreach ( @lines ) { print$_$i\n; my $sortKey = $i++; # do something to create the sort key, # using %idToName to map the ID to the name $sortKeys{$_} = $sortKey; You have no variable $sortKey. } # Then create a sub to pass to sort: print @lines $i\n; # Could you elaborate on this # $a $b are not passed any values -- don't understand sub bySortKey { $sortKey{$a} cmp $sortKey{$b} } print bySortKey; Nope, bySortKey is a comparison subroutine designed to be used for a sort operation, but there isn't one here. exit(0); # If script executes successfully do you exit(1) OR exit(0); exit(0) for success, but it's not necessary; I only call exit if I want to supply a non-zero value. You need to start with some more basic tutorials. I recommend you get Learning Perl, Second Edition, by Schwartz et al, from O'Reilly Associates. -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com
Re: foreach confusion
--- David Gilden [EMAIL PROTECTED] wrote: As an exercise here I want to play with this for a few minutes, and have a few basic questions #!/usr/bin/perl # First question do I use # ( ) OR { ... } @lines = qq{dave john mike drew}; It's not that simple, and it's simpler. First, qq{foo} is exatly the same as qq(foo), and even qq/foo/. The trick is that if you use bracketing characters like () {} [] then one opens, the other closes. If you use non-bracketing characters like / | ! ' (or whatever) then the first opens, the next closes. See perdoc perlop (I think). Next, qq{foo} is the same as foo. qq is the interpolating operator, intended to let you embed various types of quotes, among other things, like this: print qq{ I said She said 'Hi' to me to George.}; qq{foo} is foo, just as q{foo} is 'foo'. Double-q, double-quote, single-q, single-quote. so what you've done with @lines = qq{dave john mike drew}; is exactly the same as @lines = dave john mike drew; In the other hand, qw{} is often pronounced Quote-Words, and it returns a list of the whitespace-delimited strings it encloses. Thus, qw / a b c / is equivelent to ('a', 'b', 'c') (I don't have Perl installed here at home yet, so somebody please correct any of this that I'm fouling up off the top of my head! lol!) # First error, should be qw( ... ) # qq is garbage, right? Well, depends on what you want, lol But in this use, probably. # is the ',' correct in my($a,$b) can you use a space my($a $b @c) The comma is correct, an no, I don't think a space works in my(). my (%sortKeys,$i); That declares %sortKeys and $i. foreach ( @lines ) { That will iterate over each element of @lines, aliasing each to $_ That's fine, but a more explicit syntax would be to declare a loop-local variable to use as the alias. foreach my $line (@lines) { # assines each elem of @lines to $line print$_$i\n; Assuming you didn't print the code that put a value in $i, ok. If this is all the code you're using, then $i hasn't been loaded, but you *did* make $_ an alias to each element of @lines, so it should print whatever's in @lines. my $sortKey = $i++; # do something to create the sort key, # using %idToName to map the ID to the name Just remember that this assigns the value of $i *before* incrementing $i. That may be exactly what you wanted -- just being explicit. =o) $sortKeys{$_} = $sortKey; Which assigns the value of $sortKey to the element of %sortKeys at the location indexed by $_, which has whichever element of @lines it got this iteration. } # Then create a sub to pass to sort: print @lines $i\n; # Could you elaborate on this # $a $b are not passed any values -- don't understand sub bySortKey { $sortKey{$a} cmp $sortKey{$b} } print bySortKey; Ok -- let's look at some code. my %h = (1..100); # $h{1}==2, $h{3}==4, etc A quick shuffle my @ary = keys %h; @ary now has a hash-order list of the odd numbers less than 100. All that's just to have something to sort. I could say my @srt = sort @ary; and sort would walk through @ary, using the default comparison, putting them in order. (Question: isn't the default a string compare? That would but 10-19 before 2, and 20-29 before 3.) So, maybe, since I seem a little unsure, I want to be certain to put them in numeric sort order. One way to do that is to build a comparicon function right inside the sort statement, like this: my @srt = sort { $a = $b } @ary; In a case like this, the elements being compared are given to the routine I built in the middle of the sort as $a and $b. That's just a standard Perl-ism. Sorts provide $a and $b as the default variables. Had I wanted them backwards, I could have said: my @srt = sort { $b = $a } @ary; And it would reverse-sort. This is the way I usually do this sort of thing, but you can also write a named subroutine to handle it, in which case the syntax would be my @srt = sort funcName @ary; sort would then call your subroutine named funcName to evaluate the comparison of elements. It would still be able to use $a and $b, which are package globals. So funcName (which might actually be bySortKey) might look like you wrote it above: sub bySortKey { $sortKey{$a} cmp $sortKey{$b} } but to print the sorted values, rather than print bySortKey; you might say print sort bySortKey @ary; Of course, you'd need to populate %sortKey first, which I didn't. But if I wanted to use %h, I could: sub funcName { $h{$a} cmp $h{$b} } For my example values that would be silly, but that happens a lot in examples, I'm told. lol exit(0); # If script executes successfully do you exit(1) OR exit(0); usually, if everything it fine, you don't need either. just say: exit; Passing an argument explicitly says to use that for your return code to the operating system. exit; # same as exit(0) exit($someCode); # returns
Re: foreach confusion
At 07:47 PM 5/22/01 -0400, David Gilden wrote: # is the ',' correct in my($a,$b) can you use a space my($a $b @c) I missed this the first time. NEVER my() $a and $b. If you're using them as variables in your code, make better names. If you're using them as elements in a sort routine, then you DEFINITELY don't want to my() them! $a and $b are package globals, and a little magical, sort of like $_, so just don't go there! __ Do You Yahoo!? Yahoo! Auctions - buy the things you want at great prices http://auctions.yahoo.com/
RE: autonumbered data fields in ODBC database.
Kris Cook wrote .. I'm writing a form that inserts into a database table (a header record) with an autonumbered field (system assigned). Ordinarily, the value of the serial field assignment in an SQL database is stored in sqlca.sqlerrd[2], but I can't find any way to reference this in the documentation I have for Win32::ODBC. Does anyone have any ideas how I can get at this value? I need it as part of the foreign key relationship for detail records. different databases have different ways of getting the last inserted autonumber .. if you're using MSSQL it should be stored in the built-in variable @@IDENTITY (that's an SQL variable - not a Perl array) .. so once you've done your insert you should be able to immediately do a SELECT @@IDENTITY AS 'Id' and then grab it from the rowset of that command -- jason king By South Carolina state law, if a man promises to marry an unmarried woman, the marriage must take place. - http://dumblaws.com/
Similar mail lists for C and C++?
Folks, Can anyone suggest an address of similar mail list for C|C++? Eugene