in reply to Scope of a Hash

Typeglobs... I work for a small company whereby this is how the code I've used was done. Since the apps aren't really complex in nature I keep going with it and being that I "learned" how to program in perl from these I had stuck with it. Now I'm trying to find more efficient ways of doing things in the apps since we are getting involved with more complex applications. So basically "escaping" (sort of I know) grabs the memory location and $$, %$, or @$ will grab the appropriate reference to this location? I had tried using this just prior to posting this actually but it didnt seem to work. I'll try again making sure all the "i"s are dotted. Thanks again.

Replies are listed 'Best First'.
Re^2: Scope of a Hash
by ikegami (Patriarch) on Dec 15, 2005 at 16:22 UTC
Re^2: Scope of a Hash
by Anonymous Monk on Dec 15, 2005 at 16:39 UTC
    I'm still not getting exactly what I wanted here are some lines of code that I am using. I've posted the lines related the hash that I'm having trouble with). The code is not as efficient as you'll probably think but I'm working on it. On the main application:
    %returnVals = (); %X_Axis = (); %Y_Axis = (); $ExcelLink = &swOpenExcel("FILE", $lAppName, $wbFullDirectory, $wbChar +tDirectory, $wbExcelName, $wbExcelData, $dataCol, $strtRow, $labelCol +, $endRow, $typeGraph, $ChartName, $lChartTitle, 'JPG', $lLegendKey, +$lData_Labels, $fields{$q->param(X_Col)}, $fields{$q->param(Y_Col)}, +\%returnVals, \%X_Axis, \%Y_Axis);
    In the module:
    ($typeData, $AppName, $wbFullDirectory, $wbChartDirectory, $wbExcelNam +e, $wbExcelData, $dataCol, $strtRow, $labelCol, $endRow, $typeGraph, +$ChartName, $ChartTitle, $filter, $LegendKey, $LegendType, $rVals, $X +, $Y) = @_; my %X_Axis = %$X; my %Y_Axis = %$Y; my %returnValues = %$rVals; $returnValues{"ExcelLink"} = "/Web$AppName/Charts/" . $wbExcelName . " +_" . $extension . "\.xls";
      my %returnValues=%$rVals; does not setup an alias, as you seem to assume, but copies the $rVals hash. If you change the copy afterward with $returnValue{"ExcelLink"}=... the original doesn't follow. Either copy back with %$rVals=%returnValues; or change the original instead with $rVals->{"ExcelLink"}=...
      I'm still not getting exactly what I wanted here

      If you don't get it, then how do you expect us to? ;-)

      Jokes apart, at first sight your code seems more complex then it may actually be. Can you consider refactoring it in a more "efficient" (human-wise!) way? Said this, an advice that pairs well with the good "do not use typeglobs for this" one is: do not use the &-form of sub call: it is not necessary and obsolete in modern perls. It may be safe enough, but it doesn't do what you want and it's still there for another purpose: see perldoc perlsub.

      $ExcelLink = &swOpenExcel( .. 21 variables .. ); sub swOpenExcel { ## I assume ( ... 19 variables ... ) = @_;

      That is going to cause you some severe problems.

      I would also recomend you rename $rVals, $X, and $Y to something like $rVals_ref, $X_ref, and $Y_ref just to remind you that you are using refs into the future.