Professional Web Applications Themes

Printing GET parameters - PERL Beginners

Hi! I thought the following script: #!/usr/bin/perl -T use strict; use warnings; use CGI; use CGI::Carp qw(fatalsToBrowser); my $cgi = new CGI; my $parameters = $cgi->Vars(); get_params($parameters); sub get_params() { my %parameters = %{ $_ }; print $cgi->header(); print $cgi->start_html; my $p; foreach $p (sort keys(%parameters)) { print "key: $p value: $parameters{$p}\n"; } print $cgi->end_html; } exit(0); when accessed like this: http://example.com/cgi-bin/viewparams.pl?param1=7&param2=11 should print key: param1 value: 7 key: param2 value: 11 but instead it says: "Software error: Can't use an undefined value as a HASH reference at viewparams.pl line 26." Why? Thanks, Kiarra...

  1. #1

    Default Printing GET parameters

    Hi! I thought the following script:

    #!/usr/bin/perl -T
    use strict; use warnings;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser);

    my $cgi = new CGI;
    my $parameters = $cgi->Vars();
    get_params($parameters);

    sub get_params() {
    my %parameters = %{ $_ };

    print $cgi->header();
    print $cgi->start_html;
    my $p;
    foreach $p (sort keys(%parameters)) {
    print "key: $p value: $parameters{$p}\n";
    }
    print $cgi->end_html;
    }

    exit(0);


    when accessed like this:
    http://example.com/cgi-bin/viewparams.pl?param1=7&param2=11
    should print

    key: param1 value: 7
    key: param2 value: 11

    but instead it says: "Software error: Can't use an undefined value as a
    HASH reference at viewparams.pl line 26."

    Why?

    Thanks,
    Kiarra






    Kiarra Guest

  2. #2

    Default RE: Printing GET parameters

    From: Kiarra Parker <mailto:com> wrote:

    : Hi! I thought the following script:
    :
    : #!/usr/bin/perl -T
    : use strict; use warnings;
    : use CGI;
    : use CGI::Carp qw(fatalsToBrowser);
    :
    : my $cgi = new CGI;
    : my $parameters = $cgi->Vars();
    : get_params($parameters);
    :
    : sub get_params() {
    : my %parameters = %{ $_ };

    my %parameters = %{ $_[0] };

    HTH,

    Charles K. Clarkson
    --
    Mobile Homes Specialist
    254 968-8328

    Charles Guest

  3. #3

    Default Re: Printing GET parameters

    On Sep 4, Kiarra Parker said:
     

    That's all excellent to see!
     

    Two issues. Your big problem is that you're using $_ instead of $_[0].
    The arguments to a function are stored in _, the first element of which
    is $_[0]. The *other* issue is you've defined your function get_params
    with a prototype. Get rid of those parentheses in the function
    definition:

    sub get_params {
    ...
    }

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

  4. #4

    Default Re: Printing GET parameters

    Kiarra Parker wrote: 

    Besides what Charles and Jeff pointed out, the get_params() function
    seems to be redundant.

    If you want to have the parameters in a named hash, you can do:

    my %param = $cgi->Vars;

    for my $p (sort keys %param) {
    print "key: $p value: $param{$p}\n";
    }

    Optionally you can stick with a reference, and access the parameters
    directly from it:

    my $param = $cgi->Vars;

    for my $p (sort keys %$param) {
    print "key: $p value: $param->{$p}\n";
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  5. #5

    Default Re: Printing GET parameters

    Gunnar Hjalmarsson wrote: 

    To express myself more clearly: Populating a named hash is unnecessary
    when you have a reference, since you easily can access respective
    parameter via the reference.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  6. #6

    Default Re: Printing GET parameters

    Gunnar Hjalmarsson wrote:
     
    >
    >
    > Besides what Charles and Jeff pointed out, the get_params() function
    > seems to be redundant.
    >
    > If you want to have the parameters in a named hash, you can do:
    >
    > my %param = $cgi->Vars;
    >
    > for my $p (sort keys %param) {
    > print "key: $p value: $param{$p}\n";
    > }
    >
    > Optionally you can stick with a reference, and access the parameters
    > directly from it:
    >
    > my $param = $cgi->Vars;
    >
    > for my $p (sort keys %$param) {
    > print "key: $p value: $param->{$p}\n";
    > }
    >[/ref]
    Thanks all. Actually 'get_params' is just a stub I renamed to make the
    question clearer, so I'll keep the function there. However I guess I
    will keep the reference as is.
    Regards,
    Kiarra

    Kiarra Guest

Similar Threads

  1. Printing problems - not printing the bullets I have in the doent
    By Jennifer_Butler@adobeforums.com in forum Adobe Indesign Windows
    Replies: 3
    Last Post: August 31st, 07:40 PM
  2. Replies: 0
    Last Post: July 6th, 02:50 PM
  3. Printing Dynamically Insantiated MovieClips (multi-page printing)
    By :: e1 :: webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 1
    Last Post: February 9th, 08:09 AM
  4. Printing without showing the printing dialog window
    By jkoxvold webforumsuser@macromedia.com in forum Macromedia Flash Actionscript
    Replies: 3
    Last Post: February 5th, 09:38 PM
  5. create parameters without creating parameters
    By dan marks in forum ASP Components
    Replies: 0
    Last Post: October 30th, 01:51 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139