in reply to Re: Arrow operator usage in perl
in thread Arrow operator usage in perl

Thanks But i am not seeing any Hash reference in my code,

can u please explain

below is the part of my code

my @fcdevices = `ioscan -kfnNC fc | grep "/dev/" 2>/dev/null`; foreach my $fcdev(@fcdevices) {                 my @fcdump = `fcmsutil $fcdev`;                 foreach my $ln (@fcdump) {                   if($ln =~ /Hardware Path is/)                    {                  (my $pathname, $hwpath) =split('= ', $ln);                       chomp($hwpath);                    }                  elsif($ln =~ /N_Port Port World Wide Name/)                    {                    (my $hwwnname, $hwwn) = split('= ', $ln);                           chomp($hwwn);                    }                 }                  $hwwns->{$hwpath} = $hwwn;         }         return $hwwns;

Replies are listed 'Best First'.
Re^3: Arrow operator usage in perl
by choroba (Cardinal) on Nov 09, 2013 at 18:44 UTC
    Here is the hash reference:
    $hwwns->{$hwpath} = $hwwn;

    Even if the reference was not used before, Perl will happily create a new one for you. The process is called "autovivification".

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      So the concept is new to me, so can you please explain

      what is happening in my piece of code

        So have you read the documentation I linked to?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        "So the concept is new to me, so can you please explain..."

        OK, yet another answer with no code :-(

        When i had my first impact with Perl some years ago, i got stuck, what else.

        So i bought a book and started reading. This was the way i did it.

        I'm not a friend of this "self-taught, reverse-engineering is good bla, bla" approach. One needs sometimes a good advice.

        But before this, there must be a personal effort... AKA RTFM

        This is my recommendation - to you.

        Update:

        Oops, i published too fast...sorry.

        Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re^3: Arrow operator usage in perl
by kcott (Archbishop) on Nov 09, 2013 at 18:46 UTC
    "below is the part of my code"

    The fragment of your code that you've posted doesn't show the declaration (or subsequent assignment) of $hwwns. Look for that declaration in your code.

    If you haven't declared that variable, the 'vars' stricture of strict would have advised you.

    If you haven't used strict, you should have done.

    Additional Information Update (I just noticed this after posting my response.):

    Using strict would have also pointed out this:

    (my $hwwnname, $hwwn) = split('= ', $ln);

    which you probably intended to be this:

    my ($hwwnname, $hwwn) = split('= ', $ln);

    See the my documentation for the syntax for declaring a list of variables.

    -- Ken

Re^3: Arrow operator usage in perl
by three18ti (Monk) on Nov 09, 2013 at 18:56 UTC

    Look at where $hwwns is defined.

    As you've only pasted a bit of code out of context we really don't have much to go on here.

    $hwwns->{$hwpath} = $hwwn assigns the value of $hwwn to the KEY named with the value of $hwpath

    the hash ref $hwwns was "declared somewhere else"(TM) within your code that you have not shared with us

    Here's a quick example to show you how hash references work:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Declare the hash reference with an empty hash my $hash = {}; # assign the value "bar" to the key foo $hash->{foo} = 'bar'; # This prints the values in the hash # $VAR1 = { # 'foo' => 'bar' # }; print Dumper $hash; # you can retrieve the value by specifying the hash key directly also # prints "bar" print $hash->{foo} . "\n"; # we can use variables to assign key names: foreach my $key (qw(abc def lmnop zaxy)) { my $value = reverse $key; $hash->{$key} = $value; } # since this is test data, I've just reversed the key names # to show the differentiation between $KEY and $VALUE # Now our hash looks something like this: # $VAR1 = { # 'zaxy' => 'yxaz', # 'lmnop' => 'ponml', # 'def' => 'fed', # 'abc' => 'cba', # 'foo' => 'bar' # }; print Dumper $hash;

    If you can provide more information, like a full sub definition, we can probably provide a more definite answer.

Re^3: Arrow operator usage in perl
by Kenosis (Priest) on Nov 09, 2013 at 18:51 UTC

    The return $hwwns; at the end of your code fragment suggests the code is part of a subroutine. Was a hash reference passed to the subroutine, e.g., do you see something like my ($hwwns) = @_; at the top of the subroutine?

      this is my Code block may be u can help me out of this

      sub gethpux1131hwwns {         my $hwwns;         my $hwpath;         my $hwwn;         my @fcdevices = `ioscan -kfnNC fc | grep "/dev/" 2>/dev/null`;         foreach my $fcdev(@fcdevices) {                 my @fcdump = `fcmsutil $fcdev`;                 foreach my $ln (@fcdump) {               if($ln =~ /Hardware Path is/)               {               (my $pathname, $hwpath) = split('= ', $ln);                         chomp($hwpath);               }              elsif($ln =~ /N_Port Port World Wide Name/)                {                (my $hwwnname, $hwwn) = split('= ', $ln);                            chomp($hwwn);                }              }                $hwwns->{$hwpath} = $hwwn;         }         return $hwwns; }

        Ah! The subroutine only returns a hash reference, like the following:

        use strict; use warnings; use Data::Dumper; my $hashRef = getHashRef(); print Dumper \$hashRef; print $hashRef->{'Hello'}; sub getHashRef { my $hwwns; $hwwns->{'Hello'} = 'world!'; return $hwwns; }

        Output:

        $VAR1 = \{ 'Hello' => 'world!' }; world!