checking existance of an item in an array
Greetings This is my first stab at Perl. Please be gentle :-) I have the following snippet which seems to do what I am trying to do, namely, check the url field value in a POST request, do a server redirect (as opposed to browser redirect) if the value is in safe_destinations array, redirect to start.htm if not. #!/usr/bin/perl -wT # use strict; use CGI qw(:standard); my @safe_destinations = ('a.htm', 'b.htm', 'c.htm'); my $destination_url = '/start.htm'; if(param()) { foreach (@safe_destinations) { if (param('url') eq $_) { $destination_url = '/' . param('url'); last; } } } print redirect($destination_url); I have two questions Is there a function to check if an item is in an array, instead of looping through the array? Can you see a problem with the snippet? Many thanks for your time. K.
Re: checking existance of an item in an array
--- Konstantine [EMAIL PROTECTED] wrote: Greetings This is my first stab at Perl. Please be gentle :-) I have the following snippet which seems to do what I am trying to do, namely, check the url field value in a POST request, do a server redirect (as opposed to browser redirect) if the value is in safe_destinations array, redirect to start.htm if not. #!/usr/bin/perl -wT # use strict; use CGI qw(:standard); my @safe_destinations = ('a.htm', 'b.htm', 'c.htm'); my $destination_url = '/start.htm'; if(param()) { foreach (@safe_destinations) { if (param('url') eq $_) { $destination_url = '/' . param('url'); last; } } } print redirect($destination_url); I have two questions Is there a function to check if an item is in an array, instead of looping through the array? Can you see a problem with the snippet? Many thanks for your time. Typically this is done by putting the array elements in a hash. Here's how one might write this: my %safe_destination = map { $_ = 1 } ('a.htm', 'b.htm', 'c.htm'); my $destination_url = '/start.htm'; if ( my $url = param('url') ) { if ( exists $safe_destination{$url} ) { $destination_url = /$url; } } Since you're new to Perl, the map statement will look weird. You might find it clearer to do this (and this will create the same data structure as the map statement): my %safe_destination = ( 'a.htm' = 1, 'b.htm' = 1, 'c.htm' = 1, ); In this case, the values are irrelevant. We're just using a hash as a lookup table. Cheers, Ovid -- If this message is a response to a question on a mailing list, please send follow up questions to the list. Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Preventing unauthorized use of a CGI script
Greetings, Just wanted hear opinions on how effective this is, as way of preventing email relaying stoping unauthorized use my script. This from a script that connects a form page to sendmail #!/usr/bin/perl use CGI qw/:standard/; #..snip.. my $referer = referer; # what page called the script, check the domain exit if $referer = ($referer !~ /www\.mydomain\.com/i); #..more code below etc... If somebody from a foreign domain trys to invoke my script it should exit with out a trace. Yes? Thanks, Dave Gilden Visit my schedule page for up to the minute performance info: http://www.coraconnection.com/cgi-bin/schedule.pl Endorsing Artist for the Moog Music: http://www.moogmusic.com/artists.html?cat_id=25 == Cora Connection: Your West African Music Source Resources, Recordings, Instruments More! http://www.coraconnection.com/ == -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Win32::OLE for IIS
Hello all, I have one require to modify the properties of IIS runing on remote machine. i tried it with Win32::OLE module but unsuccessful my code :- use strict; use Win32::OLE; use Win32::OLE::Enum; Win32::OLE-Option(Warn = 3); my $hostname = 'myremotehost'; my $rootkey ; my $Collection = Win32::OLE-GetObject(IIS://.$hostname./w3svc); my $virts = Win32::OLE::Enum-new($Collection); my @vhosts = $virts-All; foreach (@vhosts){ print $_\n; } ---errror Win32::OLE(0.1703) error 0x800401e4: Invalid syntax after character 0 in IIS: at testiis.pl line 23 eval {...} called at testiis.pl line 23 Can't call method in on an undefined value at testiis.pl line 24. Any body help me in resolving how to modify the properties of default website of IIS running on remote machine Thanks.
Re: On Focus
On 2/5/06, Ron Smith wrote: Hi all, I've been looking for this all over, but can't seem to find a link. Is there a Perl or PerlScript equivalent to the JavaScript or vbscript on-focus function? If there is, could someone please, point me in the right direction. Omega -1911 [EMAIL PROTECTED] wrote: Can you tell us what exactly it is that you are trying to do? Ron Smith am Montag, 6. Februar 2006 01.35: Yes, I'm trying to have the blinking cursor start in a specific text field, on a web page, when the page loads in the browser. Hi Ron There is no such thing as a perl equivalent for the javascript onfocus attribute. Javascript handlers are interpreted by the javascript engine of a browser, based on the definition of this attribute and its associated javascript handler code, both defined in the (x)html code. Perl on the other side is a script language that can be interpreted by the server (via cgi, modperl etc.) to produce html page source code - but can not be given to the browser to be interpreted by it. What you want probably is something that produces the onfocus attribute on the server side, along with producing the html code. There are lots of modules on search.cpan.org that help to produce html source and even javascript code. Check out the CGI module, http://search.cpan.org/~lds/CGI.pm-3.15/CGI.pm, and search for the word javascript. hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Duplicates elements in an array
anand kumar am Montag, 6. Februar 2006 08.34: Hi all I have an array with hundreads of elements in it. Can anyone suggest the easiest way to keep only the duplicate elements in the array. Hi Anand Depends a bit if you want to keep all duplicates of a duplicate element or just one of it. Your question is not clear in this respect. An easy way would be to consult the perlfaq: perldoc -q 'duplicate' hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Gtk2::GladeXML on Windows: permission denied
Hi all. I originally posted this to the Gtk2-Perl mailing list, but got a total of 0 answers ... I'm trying to do an install of a Gtk2-Perl setup on Windows 2000, but not getting anywhere :( I've installed Gtk2 binaries and Perl and all modules ( for Gtk2, using the latest Win32 binaries from the gtk2-perl website ) as a user ( Windows 2000 Power User on a Windows NT domain ). Gtk2 bindings for Perl ... by themself ... work perfectly. However, I'm having *great* difficulty with the Gtk2::GladeXML module. No matter what I do do file / folder permissions ( eg remove all permissions, and add 'Full Control' to 'Everyone', and make these permissions propogate to children, I keep getting: Can't load 'C:/Perl/site/lib/auto/Gtk2/GladeXML/GladeXML.dll' for module Gtk2::GladeXML: load_file:Access is denied at C:/Perl/lib/DynaLoader.pm line 230 I've also tried pretty much everything with respect to the permission of this particular file ( GladeXML.dll ). Trust me when I say that I have full permission to this file :) I've had this problem before, but frigging around with file / folder permissions got me working then ... it doesn't seem to do it for me this time though. If I run as administrator, or modify the profile of the user and make them an administrator, then the app works fine. Otherwise as a user, I simply can't load the Gtk2::GladeXML module. Any ideas? Dan -- BEGIN-ANTISPAM-VOTING-LINKS --If you are not the CanIt administrator and you think this message is spam, please give the id 38154 and magic value 68128e9d82ae to [EMAIL PROTECTED] to be marked as spam. Teach CanIt if this mail (ID 38154) is spam: Spam:http://entropy.homelinux.org/canit/b.php?c=si=38154m=68128e9d82ae Not spam:http://entropy.homelinux.org/canit/b.php?c=ni=38154m=68128e9d82ae Forget vote: http://entropy.homelinux.org/canit/b.php?c=fi=38154m=68128e9d82ae -- END-ANTISPAM-VOTING-LINKS -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
dbm again
Hi! Can anyone tell me why this script doesn't work? use strict; use warnings; my %hash = (); my $key1 = 'nul'; my $en = 'en'; my $to = 'to'; my $tre = 'tre'; $hash{$key1} = [$en, $to, $tre]; dbmopen(my %dbm_result_hash, 'hash_database', 0666) or die cannot save database_name to dbm\n; %dbm_result_hash = %hash; dbmclose(%dbm_result_hash); dbmopen(my %dbm_hash, 'hash_database', 0666); my ($key) = keys %dbm_hash; print $dbm_hash{$key}[2],\n; dbmclose(%dbm_hash); exit; Regards Anders.
Re: dbm again
Anders Stegmann am Montag, 6. Februar 2006 12.30: Hi! Hi Anders Can anyone tell me why this script doesn't work? use strict; use warnings; my %hash = (); my $key1 = 'nul'; my $en = 'en'; my $to = 'to'; my $tre = 'tre'; $hash{$key1} = [$en, $to, $tre]; dbmopen(my %dbm_result_hash, 'hash_database', 0666) or die cannot save database_name to dbm\n; No quotes around the string after die. This is a syntax error. %dbm_result_hash = %hash; dbmclose(%dbm_result_hash); dbmopen(my %dbm_hash, 'hash_database', 0666); my ($key) = keys %dbm_hash; Not shure if this is intended. $key contains the first key of the list of keys from %dbm_hash, which itself has not a specific order. print $dbm_hash{$key}[2],\n; dbmclose(%dbm_hash); exit; No need for an exit at the end of a script. btw, from perldoc -f dbmopen: dbmopen HASH,DBNAME,MASK [This function has been largely superseded by the tie function.] hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Fwd: Re: dbm again
Anders Stegmann 02/06/06 1:02 pm Thaks for replying! The hash has only one key, so it sould be okay. I get the output: Can't use string (ARRAY(0x648290)) as an ARRAY ref while strict refs in use at testhash6.pl line 8. When I run the script. It seems like I am somehow dealing with a reference to an array!? Or what? Anders. John Doe [EMAIL PROTECTED] 02/06/06 12:55 pm Anders Stegmann am Montag, 6. Februar 2006 12.30: Hi! Hi Anders Can anyone tell me why this script doesn't work? use strict; use warnings; my %hash = (); my $key1 = 'nul'; my $en = 'en'; my $to = 'to'; my $tre = 'tre'; $hash{$key1} = [$en, $to, $tre]; dbmopen(my %dbm_result_hash, 'hash_database', 0666) or die cannot save database_name to dbm\n; No quotes around the string after die. This is a syntax error. %dbm_result_hash = %hash; dbmclose(%dbm_result_hash); dbmopen(my %dbm_hash, 'hash_database', 0666); my ($key) = keys %dbm_hash; Not shure if this is intended. $key contains the first key of the list of keys from %dbm_hash, which itself has not a specific order. print $dbm_hash{$key}[2],\n; dbmclose(%dbm_hash); exit; No need for an exit at the end of a script. btw, from perldoc -f dbmopen: dbmopen HASH,DBNAME,MASK [This function has been largely superseded by the tie function.] hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
regarding fd script
Hello All Can any one please tell what are fd script and how to run those script with example. when do we get this error : Wrong syntax (suid) fd script name Failure is the step for success. If you know you are failed it will be easy to climb step. - Jiyo cricket on Yahoo! India cricket
Re: Duplicates elements in an array
Do you want to keep the duplicate elements simply and don't mind the order of them? If I have understood for you correctly,I should write the code as below: use strict; use warnings; my @array = qw/aaa bbb ccc aaa ddd ddd aaa ccc bbb bbb/; my %hash; $hash{$_}++ for @array; my @duplicate = grep { $hash{$_} == 2 } keys %hash; print join ' ',@duplicate; -Original Message- From: anand kumar [EMAIL PROTECTED] Sent: Feb 6, 2006 2:34 AM To: beginners@perl.org Subject: Duplicates elements in an array Hi all I have an array with hundreads of elements in it. Can anyone suggest the easiest way to keep only the duplicate elements in the array. Thanks in Advance for the help Regards Anand - Jiyo cricket on Yahoo! India cricket -- Jeff Pang NetEase AntiSpam Team http://corp.netease.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Gtk2::GladeXML on Windows: permission denied
On 2/6/06, Daniel Kasak [EMAIL PROTECTED] wrote: snip If I run as administrator, or modify the profile of the user and make them an administrator, then the app works fine. Otherwise as a user, I simply can't load the Gtk2::GladeXML module. Any ideas? snip I think the fact above is why you are getting no answers. This fundamentally an OS problem not a Perl problem. That said, have you checked the permissions of the entire directory chain? The other possibility is that the Gtk2::GladeXML XS code is trying to open a file that the user has no permissions to. I don't remember anything like that, but it has been years since I looked at the code. What version are you using? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Gtk2::GladeXML on Windows: permission denied
On 2/6/06, Chas Owens [EMAIL PROTECTED] wrote: On 2/6/06, Daniel Kasak [EMAIL PROTECTED] wrote: snip If I run as administrator, or modify the profile of the user and make them an administrator, then the app works fine. Otherwise as a user, I simply can't load the Gtk2::GladeXML module. Any ideas? snip I think the fact above is why you are getting no answers. This fundamentally an OS problem not a Perl problem. That said, have you checked the permissions of the entire directory chain? The other possibility is that the Gtk2::GladeXML XS code is trying to open a file that the user has no permissions to. I don't remember anything like that, but it has been years since I looked at the code. What version are you using? I just took a look at version 1.005 and I see no code that runs at load time past the standard sub import { my $class = shift; $class-VERSION (@_); }. Which doesn't touch any files. I would say that this is an OS problem. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Fwd: Re: dbm again
Anders Stegmann 02/06/06 1:02 pm Thaks for replying! The hash has only one key, so it sould be okay. I get the output: Can't use string (ARRAY(0x648290)) as an ARRAY ref while strict refs in use at testhash6.pl line 8. When I run the script. It seems like I am somehow dealing with a reference to an array!? Or what? Hi Anders It's not easy to find a line 8 with a try to derefence an array in your script :-) Yes, the error tells you that something that has been an arrayref once is used as a string and tried to be derefenced. You use an arrayref (on the right side) in the line $hash{$key1} = [$en, $to, $tre]; You can have a look at this data structure (and any other you like) with Data::Dumper, by putting code like below into your script: use Data::Dumper; warn Dumper \%hash; The error occurs because of your line print $dbm_hash{$key}[2]; It seems that nested structures are not supported in the *dbm implementation, since at the beginning of your code it's possible to print $hash{$key1}[2]; resulting in 'nul'. I did not go further into the docs, and won't, and have never used dbmopen. It's deprecated. I think as a beginner you should not use deprecated things. Please read perldoc -f dbmopen and follow the advice there :-) hth, joe [top post history:] John Doe [EMAIL PROTECTED] 02/06/06 12:55 pm Anders Stegmann am Montag, 6. Februar 2006 12.30: Hi! Hi Anders Can anyone tell me why this script doesn't work? use strict; use warnings; my %hash = (); my $key1 = 'nul'; my $en = 'en'; my $to = 'to'; my $tre = 'tre'; $hash{$key1} = [$en, $to, $tre]; dbmopen(my %dbm_result_hash, 'hash_database', 0666) or die cannot save database_name to dbm\n; No quotes around the string after die. This is a syntax error. %dbm_result_hash = %hash; dbmclose(%dbm_result_hash); dbmopen(my %dbm_hash, 'hash_database', 0666); my ($key) = keys %dbm_hash; Not shure if this is intended. $key contains the first key of the list of keys from %dbm_hash, which itself has not a specific order. print $dbm_hash{$key}[2],\n; dbmclose(%dbm_hash); exit; No need for an exit at the end of a script. btw, from perldoc -f dbmopen: dbmopen HASH,DBNAME,MASK [This function has been largely superseded by the tie function.] hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Error in the postgres update
When using the DBI it is best to either let the database handle quote the strings or use placeholders: database handle quoting: my $update = UPDATE tab SET foo = . $dbh-quote($foo_val) . WHERE bar = . $dbh-quote($bar_val); $dbh-do($update); place holders: my $sth = $dbh-prepare(UPDATE tab SET foo = ? WHERE bar = ?); $sth-execute($foo_val, $bar_val); My preference is place holders because it alows you to create more generic solutions. On 2/4/06, Anish Kumar K. [EMAIL PROTECTED] wrote: Hi I am getting a strange error and I have no clue as how to fix this.. I am getting values from select multiple tag from the FORM and trying to update the value in the table. Say the select tag is like this select name=course multiple option value=cc/option option value=javajava/option option value=perlperl/option /select And I selected java , perl. then the select course will have the value as javaperl. when I did the update query I am getting the error as DBI returned - ERROR: Unterminated quoted string Then I used the hardcoding in the query then it was working fine.. So i feel there is some hiddencharacter which is causing the issue. when I printed the length it was giving 1 more the actual length. I tried all possible ways like $course=~ s/\r//g; splitting up ...substring... I hardcorde the select value in $course and then tried then it worked fine.. I am using postgres databse Query was $course I get from the select value update course_info set course='$course' where name='anish'; Thanks Anish -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: On Focus
John, Thanks for the explanation. It's clarified things for me. I'll be searching CPAN for a solution, as you suggested. Thanks, again Ron John Doe [EMAIL PROTECTED] wrote: On 2/5/06, Ron Smith wrote: Hi all, I've been looking for this all over, but can't seem to find a link. Is there a Perl or PerlScript equivalent to the JavaScript or vbscript on-focus function? If there is, could someone please, point me in the right direction. Omega -1911 [EMAIL PROTECTED] wrote: Can you tell us what exactly it is that you are trying to do? Ron Smith am Montag, 6. Februar 2006 01.35: Yes, I'm trying to have the blinking cursor start in a specific text field, on a web page, when the page loads in the browser. Hi Ron There is no such thing as a perl equivalent for the javascript onfocus attribute. Javascript handlers are interpreted by the javascript engine of a browser, based on the definition of this attribute and its associated javascript handler code, both defined in the (x)html code. Perl on the other side is a script language that can be interpreted by the server (via cgi, modperl etc.) to produce html page source code - but can not be given to the browser to be interpreted by it. What you want probably is something that produces the onfocus attribute on the server side, along with producing the html code. There are lots of modules on search.cpan.org that help to produce html source and even javascript code. Check out the CGI module, http://search.cpan.org/~lds/CGI.pm-3.15/CGI.pm, and search for the word javascript. hth, joe -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Ron Smith [EMAIL PROTECTED]
regular expressions over multiple lines
Hi, I've never used perl before, but was told that it's pretty powerful for text processing. Here's a problem I'm trying to solve: I have an expression, exp1 that I need to grep for recursively over a directory structure. If I find a match, I need to look at the matching file and find another expression, exp2. Both exp1 and exp2 belong to the same pattern that always looks like this { exp2 some text some lines of text some lines of tex . exp1 more lines of text more lines of text } The goal of this exercise is to see what some text says in the files that contain exp1. I can always read all of the files recursively line by line, save each line into an array until I find exp1 and then go backwards through the array until I find exp2 and see what some text on that line is, but I was wondering if I can somehow use regular expressions over multiple lines to do this. I hope this is a clear explanation of my problem. Any insights or pointers to resourcesare greatly appreciated! Thanks! - Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
Re: how to run perl file from a html page
Hy all, I think i got the solution i am using perlscript @ client side in that webpage and i am able to got the params from user and able to execute some functions (with out any web server) Thanks for your help. a b On 2/3/06, JupiterHost.Net [EMAIL PROTECTED] wrote: a b wrote: Hy , Hello, my scenario is have a html page in your c:\statichtmlpage.html in the section of form i've written like form method=post action=perfile.pl and a perl file that can retrieve params from statichtmlpage.html file and execute some thing at local machine i don't want to install any web server :-) So you want a plain text file to do an HTTP session with a local script? *something* has to send the form's params to the script, i not a web server then I suppose you could write javascript that will get executed when they click submit that will take the form's values and run a system commend to your script with those params, but its going to be very difficult to server a dynamic web page with a web server to do it :) -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Win32::OLE for IIS
Hello all, I have one require to modify the properties of IIS runing on remote machine. i tried it with Win32::OLE module but unsuccessful my code :- use strict; use Win32::OLE; use Win32::OLE::Enum; Win32::OLE-Option(Warn = 3); my $hostname = 'myremotehost'; my $rootkey ; my $Collection = Win32::OLE-GetObject(IIS://.$hostname./w3svc); my $virts = Win32::OLE::Enum-new($Collection); my @vhosts = $virts-All; foreach (@vhosts){ print $_\n; } ---errror Win32::OLE(0.1703) error 0x800401e4: Invalid syntax after character 0 in IIS: at testiis.pl line 23 eval {...} called at testiis.pl line 23 Can't call method in on an undefined value at testiis.pl line 24. Any body help me in resolving how to modify the properties of default website of IIS running on remote machine Thanks.