himanshu.padmanabhi has asked for the wisdom of the Perl Monks concerning the following question:

As I posted earlier,"^@" gets appended to my perl variable.Only way I know is to use "chop" to remote it.I still haven't got how it is coming,is there any way to know if variable is appended by "^@",then only I will chop it.because in some cases,it doesn't append,that time I should not "chop" it.

Replies are listed 'Best First'.
Re: Sensing of Null character
by moritz (Cardinal) on Feb 23, 2009 at 10:32 UTC
    Since you haven't asked any question, I can only assume it is "how do I remove a trailing Null character?".

    If that's the case, you have several options:

    { local $/ = chr 0; chomp $your_string; } # or $your_string =~ s/\x00\z//;

    See $/ and perlretut.

    I'm sure there are even more possibilites.

Re: Sensing of Null character
by almut (Canon) on Feb 23, 2009 at 10:35 UTC

    Instead of chopping only if it's there, your could do

    $variable =~ s/\0\z//;

    assuming that ^@ is the visual 'control character' representation of the zero byte that you get in some editors/tools...

Re: Sensing of Null character
by Anonymous Monk on Feb 23, 2009 at 10:39 UTC
    #!/usr/bin/perl -- use strict; use warnings; my $str = " a b c d 1 2 3\0"; if( $str =~ /\0\z/ ){ print '$str ends with null hexdump( ', unpack 'H*', $str; print ")\n"; } __END__ $str ends with null hexdump( 206120622063206420312032203300)
Re: Sensing of Null character
by tweetiepooh (Hermit) on Feb 23, 2009 at 13:40 UTC
    I've had null byte terminators from some databases. Was a real pain until I really looked closely as you can't see them using `print`.

    My solution as already mentions was to use a regex to remove the beastie.