Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create custom link tag using Perl CGI

    In a nutshell, I want know if there is a way to do this using the CGI module or submodules:

    Given a href script path (relative to server) and a CGI object with parameters (a, b, c) output a proper tag, such as:

    <a href="some_cgi_script.pl?a=value1&b=value2&c=value3">some text</a>

    Could I use the link method ( CGI->a() ) for this?

    I would imagine there is some way to create the tag with a query string attached...

    Now I KNOW I could just get the query string string and append:

    CGI->a( $url . '?' . $query_string, 'some_text' );

    ...for example, but I am looking for a more elegant solution...


    Thanks,
    -Nathan

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by djperrier View Post
    In a nutshell, I want know if there is a way to do this using the CGI module or submodules:

    Given a href script path (relative to server) and a CGI object with parameters (a, b, c) output a proper tag, such as:

    <a href="some_cgi_script.pl?a=value1&b=value2&c=value3">some text</a>

    Could I use the link method ( CGI->a() ) for this?

    I would imagine there is some way to create the tag with a query string attached...

    Now I KNOW I could just get the query string string and append:

    CGI->a( $url . '?' . $query_string, 'some_text' );

    ...for example, but I am looking for a more elegant solution...


    Thanks,
    -Nathan
    define elegant, This is a matter of imagination, in my opinion, you can design your code as you like.

    I'm curios if this is elegant?:
    Code:
    my %link = ('a' => 'a_value',
                      'b' => 'b_value',
                      'c' => 'c_value');
    my $base = 'some_cgi_script.pl';
    
    my $url = $base . '?' . join '&' map { $_."=".$link{$_}; } keys %links;
    I didn't test it, I guess it work, but I'm not so sure that is a elegant solution, maybe a complex one. I would avoid a complex solution to something if I have one simple.

    best regards

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    define elegant, This is a matter of imagination, in my opinion, you can design your code as you like.

    I'm curios if this is elegant?:
    Code:
    my %link = ('a' => 'a_value',
                      'b' => 'b_value',
                      'c' => 'c_value');
    my $base = 'some_cgi_script.pl';
    
    my $url = $base . '?' . join '&' map { $_."=".$link{$_}; } keys %links;
    I didn't test it, I guess it work, but I'm not so sure that is a elegant solution, maybe a complex one. I would avoid a complex solution to something if I have one simple.

    best regards
    That's my point. I would imagine that this is a problem run into often enough that would warrent a built-in CGI function rather than simply appending a '?' between a url and a query string!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by djperrier View Post
    That's my point. I would imagine that this is a problem run into often enough that would warrent a built-in CGI function rather than simply appending a '?' between a url and a query string!
    that's the key question, If this is the case I would write a sub, probably I would detect some pattern and write the code in a way that make my life easy,
    For exmaple you could do some checking, undef or missing arguments, use a callback for validation, and so on,
    As I said is a mater of imagination,

    best regards

  • #5
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I know. I'm asking if there is something in a standard module (such as CGI.pm, for instance) that already does this.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by djperrier View Post
    I know. I'm asking if there is something in a standard module (such as CGI.pm, for instance) that already does this.
    I don't know to be and I don't know if there is a module to do that. One place where you could search for ideas is inside some frameworks like Catalyst or others.
    Maybe FishMonger or others on this forum can help.

    best regards

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,872
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Your question is a little vague.

    Where is the data coming from for the query string?

    What is so inelegant with the $cgi->() method?

    Exactly what do you want the, yet unknown elegant, method to do that the $cgi->() method doesn't do?

    Can you provide some pseudo code of the process that you want to accomplish?


    Code:
    map { $_."=".$link{$_}; }
    is better written as:
    Code:
    map { "$_=$link{$_}" }

  • Users who have thanked FishMonger for this post:

    oesxyl (10-29-2009)

  • #8
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    define elegant, This is a matter of imagination, in my opinion, you can design your code as you like.

    I'm curios if this is elegant?:
    Code:
    my %link = ('a' => 'a_value',
                      'b' => 'b_value',
                      'c' => 'c_value');
    my $base = 'some_cgi_script.pl';
    
    my $url = $base . '?' . join '&' map { $_."=".$link{$_}; } keys %links;
    Thanks for sharing!
    Last edited by FishMonger; 11-22-2009 at 01:20 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •