strict ref problem

Discussion in 'Perl Misc' started by Dave, Oct 11, 2005.

  1. Dave

    Dave Guest

    Hi,

    When I run the code below, I get the following error message "Can't use
    string ("postf1") as a SCALAR ref while "strict refs" in use at
    test.cgi line 20"

    I searched google, this group's archives, and perldoc. I found a couple
    of articles in this group's archives that I think could apply, but I
    did not entirely understand them.

    Could someone let me know what I need to do in order to dynamically
    load variable names and then reference them? In the example below @rows
    actually represents a call to a database field which could have an
    infinate number of lines, separated by \n. The field is split using \n
    and then variables assigned to each part of each row. Is there a
    different way to do that?

    Thanks,
    Dave

    -------------------

    #!/usr/bin/perl
    use strict;
    use warnings;

    my @rows=('f1|v1','f2|v2');
    my ($postf1,$postv1,$postf2,$postv2);

    my $rowcount=@rows;

    if ($rowcount>0)
    {
    my $x=0;
    foreach my $row(@rows)
    {
    $x++;
    my $field="postf$x";
    my $value="postv$x";
    my ($name,$content)=split(/\|/,$row);
    ${$field}=$name;
    ${$value}=$content;
    }
    }

    print "f1: " . $postf1 . " v1: " . $postv1 . "\n";
    print "f2: " . $postf2 . " v2: " . $postv2 . "\n";
    Dave, Oct 11, 2005
    #1
    1. Advertising

  2. Dave

    Dave Weaver Guest

    On 10 Oct 2005 23:29:17 -0700, Dave <> wrote:
    > Hi,
    >
    > When I run the code below, I get the following error message "Can't use
    > string ("postf1") as a SCALAR ref while "strict refs" in use at
    > test.cgi line 20"


    That is because you are trying to use "symbolic references" (i.e. use
    the value of a variable as another variable's name) - this is a Bad
    Thing, for many reasons.

    See `perldoc -q "variable name"` for a discussion of the issue.


    > Could someone let me know what I need to do in order to dynamically
    > load variable names and then reference them?


    Use that most handy of Perl constructs, a hash; you don't need to
    create a variable called 'postf1', just use a hash entry that has the
    key 'postf1';

    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > my @rows=('f1|v1','f2|v2');
    > my ($postf1,$postv1,$postf2,$postv2);
    >
    > my $rowcount=@rows;
    >
    > if ($rowcount>0)


    You could have replaced those 2 lines with:
    if ( @rows )

    but the check is redundant anyway, since the foreach loop below won't
    be iterated anyway if @rows is emtpy

    > {
    > my $x=0;
    > foreach my $row(@rows)
    > {



    #!/usr/bin/perl
    use strict;
    use warnings;

    my @rows = qw( f1|v1 f2|v2 );
    my %var;

    for my $x ( 1 .. @rows ) {
    ($var{"postf$x"}, $var{"postv$x"}) = split /\|/, $rows[ $x - 1 ];
    }

    use Data::Dumper;
    print Dumper \%var;
    Dave Weaver, Oct 11, 2005
    #2
    1. Advertising

  3. Dave

    Anno Siegel Guest

    Dave Weaver <> wrote in comp.lang.perl.misc:
    > On 10 Oct 2005 23:29:17 -0700, Dave <> wrote:


    [...]

    > > Could someone let me know what I need to do in order to dynamically
    > > load variable names and then reference them?

    >
    > Use that most handy of Perl constructs, a hash; you don't need to
    > create a variable called 'postf1', just use a hash entry that has the
    > key 'postf1';


    [...]

    > my @rows = qw( f1|v1 f2|v2 );
    > my %var;
    >
    > for my $x ( 1 .. @rows ) {
    > ($var{"postf$x"}, $var{"postv$x"}) = split /\|/, $rows[ $x - 1 ];
    > }
    >
    > use Data::Dumper;
    > print Dumper \%var;


    Still simpler:

    my %var = map "post$_", map split( /\|/), @rows;

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Oct 11, 2005
    #3
    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. Replies:
    0
    Views:
    337
  2. Replies:
    22
    Views:
    757
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    347
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    143
    Navindra Umanee
    Feb 12, 2005
  5. Juha Nieminen
    Replies:
    13
    Views:
    613
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page