Skip to content

Instantly share code, notes, and snippets.

@2nd-player
Last active May 14, 2017 03:15
Show Gist options
  • Save 2nd-player/cef14d3d8cd6a1526a430b9165398128 to your computer and use it in GitHub Desktop.
Save 2nd-player/cef14d3d8cd6a1526a430b9165398128 to your computer and use it in GitHub Desktop.
A script that executable under either interpreter of PHP and Perl.
<?php /*> if 0;#perl
#this script is executable under either interpreter of PHP and Perl.
#
#Author: 2nd-player
#https://wiki.xn--rckteqa2e.com/
use strict;
my $lang = 'Perl';
my @in;
my $i;
my @label=('x','y');
sub trim($){
my $str = shift;
$str=~s/^\s+//;
$str=~s/\s*$//;
return $str;
}
sub fgets(*;$){
#かなり手抜き
my $fh = shift;
unless (@_){
return scalar <$fh>;
}
my ($ret, $ch);
for (1..$_[0]){
$ret .= $ch = getc $fh ;
last if $ch eq "\n";
}
return $ret;
}
=for php
*/
define ('cut', 0);
$lang = 'PHP';
$label = array('x','y');
if (0) $garbage
=cut
;
print "This script is running under $lang now.\n";
for($i=0;$i<2;$i++){
print "$label[$i]:=";
$in[$i] = trim(fgets(STDIN));
}#next $i
printf("gcd(x,y)=%d\n", gcd($in[0],$in[1]));
exit;
// if 0; sub gcd($$){my($m,$n) = @_; if(0){m#
/*#;}
=for php
*/
function gcd($m,$n){
if (0) $garbage
=cut
;
while($m&&$n){
if($m>$n){
$m %= $n;
}else{
$n %= $m;
}
}#wend
if($m){return $m;}
return $n;
}#end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment