Wednesday, April 27, 2011

perl chomp backticks

When you use backticks in perl to get output from the system, you may end up with trailing newlines. I usually get rid of them with chomp.

Here is an example of getting the (base)name of the script being executed, from within the script.

chomp(my $SCRIPTNAME=`basename $0`);
I usually use the script name in the usage/help text of the script.

It is interesting to note that the parentheses are required, because if you did this:
chomp $SCRIPTNAME=`basename $0`

It would be interpreted as:
(chomp $SCRIPTNAME) =`basename $0`

which is not what you want.

No comments:

Post a Comment