Subroutines

O

Ondra

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
 
B

Brad Baxter

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
 
T

Tad McClellan

Ondra said:
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.
 
A

Anno Siegel

Tad McClellan said:
Ondra said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top