Soft refs problem with cgi - soft ref appears empty :-(

Discussion in 'Perl Misc' started by ade0, Apr 6, 2005.

  1. ade0

    ade0 Guest

    Hi Group,

    I'm a relative cgi newbie. In the script below, I am trying to cycle
    through 3 variables using a loop to create a soft reference for each,
    and print the contents of each reference.

    When I print the dereferenced value, it is always empty - but when I
    print the variables themselves ($p0_mount etc), they contain the
    correct values.

    Can anybody see what I'm doing wrong.

    Thanks
    Adrian


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

    my ($ref,$p0_mount,$p1_mount,$p2_mount,$q,$val);

    $ENV{'PATH'}="/bin:/usr/bin";

    print header, start_html("Test Soft Refs"), h1("Test Soft Refs");
    $q=new CGI;
    if ($q->param()) { # the form has already been filled out
    # Read in parameters
    $p0_mount=$q->param("p0_mount");
    $p1_mount=$q->param("p1_mount");
    $p2_mount=$q->param("p2_mount");
    {
    no strict 'refs';
    for $val (0,1,2) {
    # Create p0_mount, p1_mount, p2_mount etc
    $ref="p${val}_mount";
    # Ref in the following line always empty
    print "Ref for value $val=$$ref",p;
    }
    print hr;
    }
    # The following provides correct values
    print "Ref for value 0=$p0_mount",p,
    "Ref for value 1=$p1_mount",p,
    "Ref for value 2=$p2_mount",p,
    hr;
    } else {
    print start_form;
    print h2("Test soft refs");
    print $q->textfield(-name=>'p0_mount',-value=>'p0');
    print $q->textfield(-name=>'p1_mount',-value=>'p1');
    print $q->textfield(-name=>'p2_mount',-value=>'p2');

    print p(submit("Submit"),reset("clear"));
    print end_form(), hr();
    }
    print end_html;


    Script provides output like this:
    Ref for value 0=
    Ref for value 1=
    Ref for value 2=
    -------------------------------------------------------------------Ref
    for value 0=p0
    Ref for value 1=p1
    Ref for value 2=p2
     
    ade0, Apr 6, 2005
    #1
    1. Advertising

  2. ade0 wrote:
    > In the script below, I am trying to cycle
    > through 3 variables using a loop to create a soft reference for each,
    > and print the contents of each reference.
    >
    > When I print the dereferenced value, it is always empty - but when I
    > print the variables themselves ($p0_mount etc), they contain the
    > correct values.
    >
    > Can anybody see what I'm doing wrong.


    Answer 1:
    You are using soft references when you easily could replace them by
    using e.g. a hash. For instance:

    my %par = map { $_, $q->param("p${_}_mount") } 0,1,2;
    for my $key (0,1,2) {
    print "Value $key=$par{$key}",p;
    }

    Answer 2:
    The $p0_mount etc. variables are my() declared, while they'd need to be
    package globals.

    Answer 3:
    You didn't check the FAQ before posting here. The FAQ entry

    perldoc -q "variable name"

    provides both answer 1 and 2 above.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 6, 2005
    #2
    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. John Nagle
    Replies:
    3
    Views:
    312
    John Nagle
    Feb 25, 2007
  2. Replies:
    0
    Views:
    366
  3. Replies:
    22
    Views:
    797
    peter koch
    Apr 30, 2008
  4. Replies:
    6
    Views:
    379
    James Kanze
    Apr 29, 2008
  5. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    160
    Navindra Umanee
    Feb 12, 2005
Loading...

Share This Page