John007 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

Appreciate if you can help me in understanding the meaning of below line in the following code. What exactly below means.

Thanks in advance.
$bakjobs->{"$2"}{"dbt"} = $1; $bakjobs->{"$2"}{"cdt"} =$3;
#!/usr/bin/perl my %bakjobs; &cdtcall(\%bakjobs); sub cdtcall(\%) { local($bakjobs) = shift; local($file); open (FILE, "./HD/PQ/c.bak"); while (<FILE>) { if ( /^BAK(\w+)+\s+([\w\_]+)\s*/i ) { $bakjobs->{"$2"}{"dbt"} = $1; $bakjobs->{"$2"}{"cdt"} =$3; } } close (FILE); }

Replies are listed 'Best First'.
Re: Understanding -> function
by ccn (Vicar) on Dec 28, 2008 at 19:26 UTC
    see:

    The sub cdtcall gets a reference to the %bakjobs hash as an argument and stores it in scalar $bakjobs. Note: %bakjobs and $bakjobs are different variables despite their similar looking names.

    Well, $bakjobs is a reference to a hash. $bakjobs->{"$2"} returns a value of a key "$2" for that hash. This value is a hash reference again. And $bakjobs->{"$2"}{"dbt"} is an accessor to value of "dbt" key of %{$bakjobs->{"$2"}} hash.

      Many Thanks for this help,
      Appreciate your further help..
      ** I understood that $bakjobs->{"$2"} takes value of $2 for hash %bakjobs and store it.
      ** I further understood that {"dbt"} becomes key of %{$bakjobs-> {"$2}.
      Then
      (1) what is the use of =$1 and =$3 at end of code?
      (2) This means the %bakjobs will have two different keys one as "dbt" and other as "cbt"
      $bakjobs->{"$2"}{"dbt"} = $1; Thanks in advance..
        $bakjobs is a reference to the hash %bakjobs. The $bakjobs->{"$2"} part is accessing the "$2" key of that hash. The added {"dbt"} takes the value of the "$2" key and interprets it as a reference to another hash (creating one if necessary) and the = $1; stores $1 as the value for the "dbt" key in that inner hash. So if the line being parsed was BAKfoo bar, %bakjobs looks something like this (in addition to anything else already in it):
        %bakjobs = ( "foo" => { "dbt" => "bar", "cdt" => undef, } );
        (The value for cdt is undef from $3 because there are only two sets of capturing parentheses in your regex.)
Re: Understanding -> function
by plobsing (Friar) on Dec 28, 2008 at 19:43 UTC

    Hi. (I would) Appreciate (it) if you would use <code> tags. Otherwise your code might be interpreted as markup.

    $bakjobs->{$2}{dbt} = $1; $bakjobs->{$2}{cdt} = $3;

    This is simply inserting into a hash of hashes based on the last matched regex. However, based on the regex provided, I suspect $3 will be undefined.

    Aside from that this code has numerous things wrong (or at least questionable) with it:

    • A prototype on a sub does nothing if you call it with &subname. You should probably be using neither of those.
    • local is for dynamic scoping, not local variables. Use my instead.
    • While you're at it, stop using global variables for filehandles (open( my $file, './HD/PQ/c.bak' ) or die;)
    • In (\w+)+, the second plus does nothing (nothing useful at least).

      Thanks Monks.. The formatting code will be below where there will be $1, $2 and $3 values.
      ( /^BAK(\w+)\s+(\d+)\s+([\w\_]+)\s*/i )
      But I still do not understand the use of =$1; and $3; at end of code for below two lines.
      $bakjobs->{"$2"}{"dbt"} = $1; $bakjobs->{"$2"}{"cdt"} =$3;
      Please explain.. Appreciate your help..

        That's an assignment. It puts the value of $1 into $backjobs->{$2}{dbt}. Likewise, it puts the value of $3 into $bakjobs->{$2}{cdt}.

        Also note that \w matches underscores, so the character class in the regex is redundant. See perlretut.

        $1, $2, $3 and so on are capture variables.

        Specifically, in the regex
            /^BAK(\w+)\s+(\d+)\s+([\w\_]+)\s*/i
        the sub-expressions (\w+), (\d+) and ([\w\_]+) (which, as already noted, is the same as (\w+) since \w is [a-zA-Z0-9_], but under the control of locale) are capturing groups, and whatever these groups match, if anything, is available through the capture variables. See perlre, perlretut (especially Extracting matches) and perlrequick for further discussion of capturing groups and capture variables; in particular, how the number of the variable ($1, $2, $3, etc.) relates to the order of the capture group in the regex.