[lug] Perl - How to pass by Reference?

Bill Thoen bthoen at gisnet.com
Wed Mar 1 15:36:44 MST 2006


In this contrived example, I want to pass $test by reference so the
changeit() subroutine can change it, but I can't figure out how to do this
with 'strict' and 'warnings' turned on. Can anyone here tell me what I need
to do to fix this? (as you can tell, I'm trying to learn Perl)

#!/usr/bin/perl

use strict;
use warnings;

&main();

sub main {
  my $test='Hello';

  if (&changeit ($test)) {
    print "$test\n";
  }
  else {
    print "failed\n";
  }
}

sub changeit {
  my $val = pop @_;

  if ($val eq 'Hello') {
    $val = 'Goodbye';
    return 1;
  }
  else {
    return 0;
  }
}





More information about the LUG mailing list