Perl: Returns a hash from a routine

I tried the examples for several hours, but I cannot figure out how to do what I want to do.

I want to return the hash from the subroutine, and I decided that the link was the best option. Here, where it gets a little trickier. I want to reference the hash as $ hash {$ x}. I still know in perl: /

1. First question: the examples I use show that it is normal to use $ hashTable {$ login}, should% hashTable {$ login} be used or does it not matter? Below is the code:

sub authUser  {
    $LocalPath = "/root/UserData";
    open(DATAFILE, "< $LocalPath");
    while( $linebuf = <DATAFILE> ) {
        chomp($linebuf);
        my @arr = split(/:/, $linebuf);
        my $login = $arr[1];        # arr[1] contains the user login names
        my $hashTable{ $login } = "$arr[0]";        #$arr[0] is account number
    }
    close DATAFILE;
    return \$hashTable{ $login };
}

Then I want to test this data to see if a login is present, here is my test method

# test login Dr. Brule which is present in UserData
my $test = "Dr. Brule";
my $authHash = &authUser();

if ( $authHash{ $test } )  {
    print "Match for user $test";
}
else  {
    print "No Match for user $test";
}

2. Should my $ authHash really be $ authHash {$ something}, I'm so confused about this

Edit: after some reading tips, still trying, but without the dice, any help would be greatly appreciated


Edit 2: Can someone modify my code to better understand the answers? I'm sorry that I cannot get this to work at all, I tried for hours, and I really want to know the right way to do this, I can post my various attempts, but I feel that it will be a waste of real estate.
+4
source share
3 answers

First of all, as noted in the comments mpapec, use strict; use warnings;. This will catch the most common mistakes, including a note of most of the problems you ask for here (and usually gives clues about what you should do).

, 1 2:

%hash . .

$hash{key} - .

, \%hash %hash, .. , , -, , . \$hash{key} .

, , , .

$hash_ref = \%hash

, , . ->, :

$hash_ref->{key}

, ->, ($hash_ref->{key}), , ($hash{key}).

( 2 & - authUser() &authUser(). & Perl 5+, , , , .)

3, , :

my $valid;
for my $username (@list_of_users) {
  if ($login eq $username) {
    $valid = 1;
    last; # end the loop since we found what we're looking for
  }
}

if ($valid) {
  print "Found valid username $login\n";
} else {
  print "Invalid user! $login does not exist!\n";
}
+6

perl

$scalar = 1;
@list = ( $scalar, $scalar, $scalar );

$list[1].
. : $hash{ name1 }

%hash = ( 'name1', $scalar, 'name2', $scalar, 'name3', $scalar )

, , NOTICE: () .
.

, $sigil , @ .

, $ sigil:

$scalar =  $hash{ name1 };
$scalar =  $list[ 1 ];

, @ sigil:

@list2 =  @list1;            # copy all items
@list2 =  @list[ 1, 3..5 ];  # copy four items with index 1,3,4,5
@list2 =  @hash{ 'name1', 'name3' }; #copy two items with index 'name1', 'name2'

perl . .

$ref = \$scalar;
$ref = \@list;
$ref = \%hash;

$ref , . , $ref, .

$scalar = $$ref;
@list   = @$ref;
%hash   = %$ref;

. - . ->
[], perl, ,
{}, perl, :

$scalar = $ref->[ 1 ];
$scalar = $ref->{ name1 };

: - $sigil

, @sigil. :

@list =  @$ref[ 1, 3..5 ];
@list =  @$ref{ 'name1', 'name2' };

1st: $ref - . $ , one 'ref'
2nd: @$ref - $ref. @ , .
3rd-a: "1,3,4,5" (: [])
3rd-b: 'name1', 'name2' (: {})

Ok. , . :

@list = ( 1, 2, 3, 4, 5 );
%hash = ( 'a', 1, b => 2 );
@list2 = ( \@list, \%hash, 3, 'y' );
%hash2 = ( name1 => \@list2, d => 4 );
%hash2 = ( 'name1', \@list2, 'd', 4 );  #same. no any difference.
$href  = \%hash2;

=> - , .

hash2:

$scalar =  $hash2{ name1 };
$scalar =  $href->{ name1 };

$href-> %hash2

hash2:

@list =  @hash2{ 'name1', 'd' };
@list =  @$href{ 'name1', 'd' };

@$href %hash2

$scalar =  $hash2{ name1 }; # <--- What does this mean???

$hash2 , % hash2. ;

$list_ref =  $hash2{ name1 };
$scalar   =  $list_ref->[ 1 ]; # <--- what we get here???

$list_ref , . ->[ , . $list_ref @list2, \%hash.
:

$scalar =  $hash2{ name1 }->[ 1 ];

, : '$ list_ref' '$ hash2 {name1}'

, [ 1 ] %hash. , , $:

$hash_ref  =  $hash2{ name1 }->[ 1 ];
$scalar    =  $hash_ref->{ b };

$hash_ref , . ->{ , . - $hash_ref %hash, 2.
:

$scalar =  $hash2{ name1 }->[ 1 ]->{ b };

, : '$ hash_ref' '$ hash2 {name1} → [1]'

hash2 %hash2. $href? :

$scalar =  $hash2{ name1 };
$scalar =  $href->{ name1 };

, , ->. :

@l = ( 1, 2, 3, 4 );
$scalar = $l[ 1 ];    # to access to second item of @l list
$hr = \@l;
$scalar = $hl->[ 1 ]; # to access to second item of @l list

%h = @l;
$scalar =  $h{ 1 }; 
$hr =  \%h;
$scalar =  $hr->{ 1 };

-> [ { -.

$href?

$scalar =  $hash2{ name1 }->[ 1 ]->{ b };
$scalar =  $href->{ name1 }->[ 1 ]->{ b };

->

$scalar =  $hash2{ name1 }[ 1 ]{ b };
                 ^-- first dereference
$scalar =  $href->{ name1 }[ 1 ]{ b };
                ^--first dereference

. perl LIST LIST.

sub test {
    return @_;
}

, .

return \%hash;  # fn()->{ name1 }; # actually all these is list of one item
return \@list;  # fn()->[ 1 ]; # so we may write: (fn())[0]->[ 1 ];
return $scalar; # fn(); # here also list of one item
return ( $scalar, \%hash, \@list ); 
    (fn())[ 0 ]; 
    (fn())[ 1 ]->{ name1 };
    (fn())[ 2 ]->[ 1 ];

$hashTable {$ login}, % hashTable {$ login} ?

. $ %hashTable. $hashTable {$ login} .
, @ sigil:

@list =  @hashTable{ 'login1', 'login2' };
# or
$l1 = 'login1';
$l2 = 'login2';
@list =  @hashTable{ $l1, $l2 };

return\$hashTable {$ login};

. . return $hashTable {$ login} .

2. $authHash $authHash {$ something},

, %hashTable - , $login. :

$login1 = { name => 'Vasiliy',   pass => 'secret' }  # ref to hash
%login2 = ( name => 'Petrovich', pass => '^&UDHJ' ); # just a hash
%hashTable = (
    vasya => $login1,  # items are always refs!!!
    piter => \%login2, # items are always refs!!!
)

, authUser sub

my $authHash = authUser( 'vasya' ); # & is not required at all

- $authHash, ->

if( $authHash->{ pass } eq $password ) {
...
}

authUser , loadUsers :   sub loadUsers {      ....      return\%hashTable;   }

my $usersDB =  loadUsers;

if( $usersDB->{ $login }->{ pass } eq $password ) {
     print 'You have granged access';
}
else { ... }

2: - , ?

. . , , .

AS ADVICE

:

  • hashref listref.
  • -> .
  • $ sigil char.

.

$list = [ 1, 2, 3 ];
$hash = { a => 1, b => 2 };
$list->[ 2 ];
$hash->{ b };

:

@l = @$list;
%h = %$hash;
@l = keys %$hash;
@l = values %$hash;
+2

, :

return \$hashTable{ $login };

() .

return $hashTable{$login} 

.

,

return\$hashTable

( {$ login}), .

:

   my $p = authUser()
   if ($p->{'Dr. Brule'})
   ...

→ . , .

0
source

All Articles