Skip to content

Instantly share code, notes, and snippets.

@xtetsuji
Created September 7, 2017 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xtetsuji/6c319b9be190dae7d958564e0e2e860d to your computer and use it in GitHub Desktop.
Save xtetsuji/6c319b9be190dae7d958564e0e2e860d to your computer and use it in GitHub Desktop.
How to write subroutine which gives reference of its argument.
#!/usr/bin/perl
use strict;
use warnings;
# chomp2
# Is this chomp?
# chomp erases only ONE "\n" on end of scalar.
# chomp2 erases ALL continuous "\n" on end of scalar.
sub chomp2 (\$) {
my $str_ref = shift;
warn "chomp2: arg is $str_ref\n";
$$str_ref =~ s/(\n+)\z//;
return $1;
}
my $text = "mogemoge\nfuga\n\n";
chomp2 $text;
print qq{"$text"\n};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment