in reply to Whats quicker - passing along as variable to sub, or global var?

The normal approach, irrespective of scope, is to have a single copy of the string and pass scalar references around. For example:
my $bigref = bigget; ... do something ... do_something_more( $bigref ); sub bigget { local $/ = undef(); # disable carriage control locally open my $fh, $ENV{BIG_STUFF} or die "$!: \$ENV{BIG_STUFF}\n"; my $slurp = <$fh>; close $fh; \$slurp; # return the reference to $slurp, which also keeps it ali +ve outside this scope!!! } sub do_something_more { my $bigref = shift; # copying the ref not the string $$bigref =~ /^\s+(\S+)/; # using a dereference my $first_word = $1; ... do more ... }

One world, one people

  • Comment on Re: Whats quicker - passing along as variable to sub, or global var?
  • Download Code