Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Subroutines

Reply
Thread Tools

Subroutines

 
 
Ondra
Guest
Posts: n/a
 
      02-13-2004
I would like to know what is do the code below:

sub safe {

my $url = shift;

return 1 if ($url =~ /^(javascript|mailto):/is);

for (@safe) { #*******I cann't understand this
return 1 if ($url =~ /\w+:\/\/((\w|-)+\.)*$_/is);
}
return 0;

}

Thanks a lot

Ondrej
 
Reply With Quote
 
 
 
 
Brad Baxter
Guest
Posts: n/a
 
      02-13-2004
On Fri, 13 Feb 2004, Ondra wrote:

> I would like to know what is do the code below:
>
> sub safe {
>
> my $url = shift;
>
> return 1 if ($url =~ /^(javascript|mailto):/is);
>
> for (@safe) { #*******I cann't understand this
> return 1 if ($url =~ /\w+:\/\/((\w|-)+\.)*$_/is);
> }
> return 0;
>
> }


It looks to me like the _intention_ is to verify whether a url matches a
set of 'safe' urls (presumably stored in @safe). However, I'm not
confident the code above does that very well.

Regards,

Brad
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      02-13-2004
Ondra <(E-Mail Removed)> wrote:

> I would like to know what is do the code below:



I will assume your interest is as a learning exercise, because
it is amateurish and crufty code, I wouldn't trust it.


> sub safe {
>
> my $url = shift;
>
> return 1 if ($url =~ /^(javascript|mailto):/is);



I lose confidence in a programmer when they throw do-nothing
options on willy-nilly. The //s does nothing when used with
that pattern, so it should not be there.

Whoever wrote this didn't really understand what they were doing.


> for (@safe) { #*******I cann't understand this



It does the same as these:

for $_ (@safe)
foreach (@safe)
foreach $_ (@safe)


See the "Foreach Loops" section in perlsyn.pod.


> return 1 if ($url =~ /\w+:\/\/((\w|-)+\.)*$_/is);



//s does nothing yet again.

//i doesn't do anything either!


> }
> return 0;
>
> }



It appears that @safe holds "approved" TLDs or something. The code
attempts to return true for those domains.

It does it badly. If @safe contains "com", it will match (return true)
when $url = "http://www.company" ...

Send that code to the bit bucket, where it belongs.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-13-2004
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Ondra <(E-Mail Removed)> wrote:
>
> > I would like to know what is do the code below:

>
>
> I will assume your interest is as a learning exercise, because
> it is amateurish and crufty code, I wouldn't trust it.


[...]

> > return 1 if ($url =~ /\w+:\/\/((\w|-)+\.)*$_/is);

>
>
> //s does nothing yet again.
>
> //i doesn't do anything either!


I completely agree with your overall assessment, but /i may do something
here, depending on $_.

Anno
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
defining subroutines when using interpreter interactively? MackS Perl 0 03-11-2005 01:26 AM
Global subroutines fd123456 ASP .Net 6 02-04-2005 11:12 PM
Global subroutines fd123456 ASP .Net 1 01-28-2005 11:12 PM
Global subroutines tshad ASP .Net 7 01-26-2005 06:42 PM
References and subroutines ReaprZero Perl 1 12-04-2003 02:53 PM



Advertisments