Re: Return value from subroutine Johnson, Reginald (GTI) wrote:
>
> I don't see what I'm doing wrong here. I just want to compare the value
> of $_ and return the indicated string if there is a match.
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $temp_proc;
> my $mnt_proc = "AVI";
> $temp_proc = convert_mnt_proc($mnt_proc);
> 
> #*************************************************************************
> sub convert_mnt_proc {
>   $_ = shift;
>   if ($_ eq /AVI/) {
>     return = "Audit Volume";
>   }
> # elsif (/BKP/) {
> #   return = "BACKUP";
> # }
> # elsif (/DBB/) {
> #   return = "Database Backup";
> # }
> # elsif (/RCL/) {
> #   return = "Reclamation";
> # }
> # elsif (/BSP/) {
> #   return = "Backup Storage Pool";
> # }
> # elsif (/MIG/) {
> #   return = "Migration";
> # }
> # elsif (/MDT/) {
> #   return = "Move Data";
> # }
> # elsif (/IMP/) {
> #   return = "Import Node";
> # }
> } #end convert_
>
> Can't modify return in scalar assignment at fixit.pl line 12, near
> ""Audit Volume";"
> Execution of fixit.pl aborted due to compilation errors.

Your error message is because you are trying to assign to the return()
function, which is invalid syntax. You should be writing

  return "Audit Volume";

Furthermore your function will fail because the test

  $_ eq /AVI/

is equivalent to

  $_ eq ($_ =~ m/AVI/)

which is either

  $_ eq '1'

or

  $_ eq ''

depending on whether the regex pattern matched against $_.

What you need is simply

  if ($_ eq 'AVI') {
    return "Audit Volume";
  }

and finally, this whole subroutine is pretending to be a hash:

use strict;
use warnings;

my %convert_mnt_proc = (
  AVI => 'Audit Volume',
  BKP => 'BACKUP',
  DBB => 'Database Backup',
  RCL => 'Reclamation',
  BSP => 'Backup Storage Pool',
  MIG => 'Migration',
  MDT => 'Move Data',
  IMP => 'Import Node',
);

my $mnt_proc = 'AVI';
my $temp_proc = $convert_mnt_proc{$mnt_proc};



HTH,

Rob

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


