Find the answer to your Linux question:
Results 1 to 7 of 7
Suggest me any online link where I can see samples of references and get better idea. And please help me in understand following 1) My $fb_r1 open (FB,"/tmp/test.txt") or die ...
  1. #1
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114

    Need to understand all type references in perl?

    Suggest me any online link where I can see samples of references and get better idea. And please help me in understand following

    1)

    My $fb_r1

    open (FB,"/tmp/test.txt") or die ("unable to open FB file: $!\n");

    while (<FB>) {
    my $line=$_;
    my ($m1,$r1,$s1) = split(":",$line);
    $fb_r1->{"$m1"}->{"$m1"}="";
    $fb_r1->{"$m1"}->{"$r1"}="";
    $r_list->{"$r1"}=" ";
    }
    close (FB);


    2)

    my $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh-errstr();
    $sth->execute() or die "Cannot execute: " . $sth->errstr();

    foreach $source (@source_date){
    my $sql = "select test, percent from testing_stat";

    while (my $row = $sth->fetchrow_arrayref) {
    $list->{@$row[0]}->{$source}=@$row[1];
    }
    }


    Meaning of above given???
    I am completely confused in references, please help me in understand and suggest me some online link......
    Switched to Scripting

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    I can't speak of any online source, though I suspect that Perl Monks may have a tutorial. I personally learned references from O'Reilly's Intermediate Perl.

    However, some of the basics:

    Basically, a scalar variable can hold a reference to another value. In this way, it acts basically as a pointer in C: the variable does not hold the value, but rather the location of the value. You can follow the reference to manipulate the value.

    Because the variable only holds the location of the value, this means that you can essentially store non-scalar data in a scalar variable. Let's see an example:

    Suppose I want a function to accept two different arrays. Without references, this would be rather difficult, since I would need to know how to separate the arrays in the resultant @_. Therefore, my function will accept two arrayrefs instead:
    Code:
    sub my_sub
    {
        my($a1, $a2) = @_;
        # $a1 and $a2 now both hold arrayrefs
    
        push @{$a1}, "val1";
        push @{$a2}, "val2";
    }
    Do you see the @{...} syntax? This means "take the value inside here and dereference it to an array". Because I am using the array, I can do array-specific operations, like push.

    Now then: how do I do something like take the index of an arrayref? This is done with a C-based syntax:
    Code:
    sub arrayref_index
    {
        my $a = shift;
    
        print "The first value is: ", $a->[0], "\n";
    }
    The "->" syntax means "dereference this value as appropriate. Here, since I'm taking an array index (note the "[...]"), it dereferences to an array. Had I used "$a->{'val'}", it would have dereferenced to a hash. The same applies above: "&#37;{$a1}" would dereference to a hash.

    Now then, in the code you have posted:

    1) First off, it looks like you're initializing values, and this is generally unnecessary in Perl. Thanks to autovivification, values will spring into existence when you need them.

    Anyway, let's look at this line:
    Code:
    $fb_r1->{"$m1"}->{"$m1"}="";
    Here, $fb_r1 is being used as a hashref. You are looking at the value for the key $m1, which in turn is a hashref. You are then assigning to the value for the key $m1 in this second hashref.

    This can be extrapolated to the other two lines.

    2) There are two things here. First, you have the lines such as:
    Code:
    $dbh->prepare($sql)
    $sth->execute()
    Even though these use the "->" syntax, these are actually method calls using object-oriented Perl. I suggest that you learn about object-oriented programming, but we will disregard it here.

    As for what's going on in the loop, we already discussed this at length in the other thread you made a while back. The syntax is rather strange here, because array slices are being taken where they are unnecessary. However, at its most simple:
    Code:
    $list->{@$row[0]}->{$source}=@$row[1];
    $list is a hashref, and you are taking one of its values (and the key for that value is in turn taken from $row, which is an arrayref). This value is itself a hashref, and you are taking the value for the key $source. You assign to this from the arrayref that is $row.

    I hope that this makes sense, and hopefully someone can suggest a good online source.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Here's a link for understanding references:

    perldsc - perldoc.perl.org
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    ghostdog74's citation (perlreftut - perldoc.perl.org) is better than mine as an intro, though after you've read that one, you'll probably want to read the one I suggested (perldsc - perldoc.perl.org) as well. I'm repeating both of them to improve their form slightly.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  6. #6
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    Thanks all great help
    Switched to Scripting

  7. #7
    Linux Newbie
    Join Date
    Jan 2008
    Posts
    114
    Now I can understand

    $ips->{"@$row[0]"}=test_ip(@$row[1]);

    $ips was the scalar now which has become a hash ref and we are intializing the key value. But following is little tricky, please help me in understand.

    $ips->{"@$row[0]"}->{"@$row[1]"}=test_ip(@$row[1]);

    and later in any other function how can I use $ips???
    Switched to Scripting

Posting Permissions

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