Return to Snippet

Revision: 63165
at April 13, 2013 02:39 by alfirth


Initial Code
#!perl
use strict; use warnings;
use Data::Dumper;

my @a = (16, 32);

#call main
main();
#program execution stops here

#subroutine main
sub main {

	#call subroutine "function1" with a hash as args
	function1( 
			arg1 => 'foo',
			arg2 => 'bar',
			arg3 => \@a,
		 );
}

sub function1 {
	my (%args) = @_;
	#define the output record separator and the array separator for printing
	local ($\, $,) = ("\n", ', ');

	#can be used in strings without spaces
	print "$args{arg1} baz$args{arg2}";

	#sometimes the inline dereferences get ugly, so you can deref to a var if you want
	print "deref3: " . ($args{arg3}[1] - $args{arg3}[0]);
	my @ref3 = @{$args{arg3}};
	print "ref3 " . ($ref3[1] - $ref3[0]);

	#use Data Dumper (core module) to print everything. Can be evaled in from a file to recreate struct
	print Dumper( \%args );
}

Initial URL


Initial Description
Use anonymous hash to keep arguments to a sub sane

Initial Title
call sub with hash args

Initial Tags


Initial Language
Perl