Subroutines

Discussion in 'Perl Misc' started by Ondra, Feb 13, 2004.

  1. Ondra

    Ondra Guest

    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
     
    Ondra, Feb 13, 2004
    #1
    1. Advertising

  2. Ondra

    Brad Baxter Guest

    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
     
    Brad Baxter, Feb 13, 2004
    #2
    1. Advertising

  3. Ondra <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Feb 13, 2004
    #3
  4. Ondra

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > Ondra <> 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
     
    Anno Siegel, Feb 13, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ReaprZero

    References and subroutines

    ReaprZero, Dec 4, 2003, in forum: Perl
    Replies:
    1
    Views:
    455
    Gunnar Hjalmarsson
    Dec 4, 2003
  2. MackS
    Replies:
    0
    Views:
    474
    MackS
    Mar 11, 2005
  3. tshad

    Global subroutines

    tshad, Jan 24, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    379
    IPGrunt
    Jan 26, 2005
  4. fd123456

    Global subroutines

    fd123456, Jan 25, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    406
    tshad
    Feb 4, 2005
  5. fd123456

    Global subroutines

    fd123456, Jan 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    401
    tshad
    Jan 28, 2005
Loading...

Share This Page