These scripts are the same forwards and backwards, excluding whitespace characters and newlines.

Making a JAPH palindrome was fairly easy using comments.

#!/usr/bin/perl #__ATAD__ print 'Just another Perl hacker,'#; #',rekcah lreP rehtona tsuJ' tnirp __DATA__# lrep/nib/rsu/!#

It took me a bit of thinking to make one without the crutch of comment lines.

#!/usr/bin/perl open fi, ">&STDOUT"; use constant X__ATAD__; print fi 'Just another Perl hacker,'; ',rekcah lreP rehtona tsuJ' if tnirp ;__DATA__ X tnatsnoc esu ;"TUODTS&>" ,if nepo lrep/nib/rsu/!#

Now, trying to create a JAPH palindrome without the use of comments or DATA? :shudder:

EDIT:Fixed based on blokhead's advice/corrections.

Replies are listed 'Best First'.
Re: JAPH palindrome.
by blokhead (Monsignor) on Sep 24, 2010 at 23:11 UTC
    Cute idea, but the reverse of this line:
    print fi ('Just another Perl hacker,');
    is not what you have written; it's this unfortunate thing:
    ;)',rekcah lreP rehtona tsuJ'( if tnirp
    But the code you have does not rely on those parentheses being there, so you are safe.

    Also, you have no space in X__ATAD__, but you have a space in __DATA__ X. I guess the "plain-language" definition of a palindrome does ignore spaces. Presumably that can only make the task easier in Perl..

    blokhead

      Thanks, I didn't notice the inverted parenthesis, obviously. That's what I get for checking manually. I also didn't realize, as this script evolved, that I no longer had use for the parenthesis at all. At one point, adding them was necessary for a successful compile.

      Yes, I went with the rule that whitespace and newlines did not have to match.

Re: JAPH palindrome.
by JavaFan (Canon) on Sep 25, 2010 at 00:19 UTC
    Now, trying to create a JAPH palindrome without the use of comments or DATA?
    ;;print "Just another Perl hacker";s;"rekcah lreP rehtona tsuJ" tnirp; +;
    It also mirrors the spaces.

    Extended to use the shebang line:

    #!/usr/bin/perl -- !m ;;;print "Just another Perl hacker"; s ;"rekcah lreP rehtona tsuJ" tnirp;;; m! -- lrep/nib/rsu/!#

      That truly is awesome looking, but it isn't working on my Perl 5.10

      jbm@Foucault:~/Code/perl $ ./javafan.pl Can't open perl script "!m": No such file or directory jbm@Foucault:~/Code/perl $ perl -v This is perl, v5.10.0 built for darwin-thread-multi-2level (with 2 registered patches, see perl -V for more detail) jbm@Foucault:~/Code/perl $ cat javafan.pl #!/usr/bin/perl -- !m ;;;print "Just another Perl hacker"; s ;"rekcah lreP rehtona tsuJ" tnirp;;; m! -- lrep/nib/rsu/!#
        I'm a bit surprised darwin doesn't like that. Anyway, here's a modification that doesn't use anything after '/usr/bin/perl' on the same line:
        #!/usr/bin/perl !m;;;print "Just another Perl hacker" ;s; "rekcah lreP rehtona tsuJ" tnirp;;;m! lrep/nib/rsu/!#
        An alternative:
        #!/usr/bin/perl print "Just another Perl hacker" ;!q!; "rekcah lreP rehtona tsuJ" tnirp lrep/nib/rsu/!#
Re: JAPH palindrome.
by JavaFan (Canon) on Sep 25, 2010 at 10:26 UTC
    #!/usr/bin/perl print "Just another Perl hacker";__END__ __DNE__;"rekcah lreP rehtona tsuJ" tnirp lrep/nib/rsu/!#
      I'm awestuck by the flipped significance of END and DNE
      ++
Re: JAPH palindrome.
by JavaFan (Canon) on Sep 25, 2010 at 10:24 UTC
    #!/usr/bin/perl BEGIN{close STDERR;print "Just another Perl hacker"} }"rekcah lreP rehtona tsuJ" tnirp;RREDTS close{NIGEB lrep/nib/rsu/!#
    Update: fixed braces, as explained below.

      You made the same mistake I originally did whilst visually checking my own palindrome: flipping the paired punctuation, that is parenthesis, braces, brackets. See blokhead's initial reply to my OP.

      My checker scripts makes it show up a bit better.

      jbm@Foucault:~/Code/perl $ debug=1 ./check_palindrome.pl javafan2.pl text = #!/usr/bin/perlBEGIN{closeSTDERR;print"JustanotherPerlhacker"}{ +"rekcahlrePrehtonatsuJ"tnirp;RREDTSclose}NIGEBlrep/nib/rsu/!# length = 124 middle = 62 half1 = #!/usr/bin/perlBEGIN{closeSTDERR;print"JustanotherPerlhacker"} half2 = {"rekcahlrePrehtonatsuJ"tnirp;RREDTSclose}NIGEBlrep/nib/rsu/!# flip = #!/usr/bin/perlBEGIN}esolcSTDERR;print"JustanotherPerlhacker"{ javafan2.pl: NOT a palindrome.

      Your others check out fine.

      jbm@Foucault:~/Code/perl $ ./check_palindrome.pl javafan* javafan.pl: Palindome OK. javafan2.pl: NOT a palindrome. javafan3.pl: Palindome OK. javafan4.pl: Palindome OK.
Re: JAPH palindrome.
by jffry (Hermit) on Sep 25, 2010 at 16:44 UTC

    With all these cool responses, it is clear I needed to write myself a palindrome checker. So here goes.

    #!/usr/bin/perl -w use warnings; use strict; my $text; for my $file (@ARGV) { { local $/=undef; open my $fh, '<', $file or die; $text = <$fh>; close $fh; } $text =~ s/\s//g; if ($ENV{'debug'}) { print "forwards = $text\n"; print "backwards = " . reverse($text) . "\n"; } if ($text eq reverse $text) { print "$file: Palindome OK.\n"; } else { print "$file: NOT a palindrome.\n"; } } exit 0;

    This didn't seem worthy of a separate Cool Uses for Perl posting.

    EDIT: Fixed based on JavaFan's comments. I no longer split the text and flip it to compare, and I do a string "is equal" compare instead of re match.

      if ($half1 =~ $flip)
      Really? It doesn't matter if $flip contain regexp special characters, or if $flip matches $half1 partially?

      I'm also wondering why you bother splitting the text into two. Wouldn't something like:

      use Test::More; use autodie; open my $fh, "<", $file; undef $/; $text = <$fh>; chomp $text; $text =~ s/\s+//g if $lax_in_whitespace; ok $text eq reverse $text, "palindrome"; done_testing;
      be enough?

        Regarding splitting and flipping it, I guess I was too hung up on the mental steps I was taking to create a palindrome in the first place. I must have coded some analog of what my brain was doing. Twelve hours later, it looks very foolish.

        The =~ vs eq bug is just an outright screw up. I have no excuse for that one.