Comparación (Comparing) PHP and Perl
| PHP | Perl |
|---|---|
| file.php | file.cgi |
| scripts in <?php..?> | whole file is script |
| Puede incluir raw HTML | must print all output |
| NO es necesario indicar el interprete | Primera línea ruta interprete es #!/var/bin/perl |
|
Protecciones |
|
| Archivo no ejecutable | Archivo ejecutable |
| chmod 644 file.php | chmod 755 file.cgi |
| Imprimir en pantalla | |
| echo “textoPrueba”;
print “textoPrueba”; |
print “textoPrueba”; |
| Imprimir en error log | |
| error_log “stuff”; | warn “stuff”; |
| Variables | |
| All variables $var | $var es una variables escalar |
| $var Siempre significa algún tipo de variable | $var, @var, %var, var son diferentes tipos de variables |
| Arreglos | |
| $var = array(1,2,3); | @var = (1,2,3); |
| $var[1] el segundo elemento de $var | $var[1] el segundo elemento de @var |
| count($var) largo del arreglo | scalar(@var) largo del arreglo |
| for ($i=0; $i<count($var); $i++) echo $var[$i]; | foreach $thing (@var) { print $thing; } |
| join(“:”,array(1,2,3)) | join(“:”,(1,2,3)) |
| Objetos | |
| associative arrays are arrays | associative arrays are their own type |
| $var = array(1=>”ho”,”hi”=>4); | %var = (1=>”ho”,”hi”=>4); |
| $var['ho'] es elemento asociado a ‘ho’ | $var{‘ho’} es elemento asociado a %var |
| while (list($k,$v) = each $var)) {…} | while (($k,$v) = each %var) {…} |
| Alcance de variables – Scoping | |
| Por defecto es local – default scope is local | Por defecto es global – default scope is global |
| $var; Np hay declaración explícita para variable local | my $var; Declaración explícita variable local |
| global $foo; Declaración variable global | No hay declaración explícita para variable local |
| static $foo; declardeclares a static variable | No variables estáticas |
| no dynamically scoped variables | local $var; declares a dynamic variable |
| Strings | |
| ‘textoPrueba’ no es evaluado | ‘textoPrueba’ no es evaluado |
| “textoPrueba $var”, se sustituye por el valorque tiene asociada la variable $var (es evaluado) | “textoPrueba $var” se sustituye por el valorque tiene asociada la variable $var (es evaluado) |
| Concatenación de strings
$a=”abc”;$b=”def”; $c=$a.$b; print ($c); //abcdef |
$a . $b concatenación de strings (ídem php) |
| $array = split(“[ \t]+”,$thing) | @array = split(/[ \t]+/,$thing) |
| Archivos – Files | |
| filehandles are just variables | filehandles are a special type |
| Archivo sintaxis lenguaje C – c file syntax | Archivo sintaxis sh/csh/tcsh – sh/csh/tcsh file syntax |
| $fd=fopen(“file.txt”,”r”); //lectura | $success=open(FILE,”<file.txt”); |
| $fd=fopen(“file.txt”,”w”); //escritura | $success=open(FILE,”>file.txt”); |
| $fd=fopen(“file.txt”,”a”); | $success=open(FILE,”>>file.txt”); |
| $fd=fopen(“file.txt”,”r+”); | $success=open(FILE,”<+file.txt”); |
| $fd=fopen(“file.txt”,”w+”); | $success=open(FILE,”>+file.txt”); |
| $fd=fopen(“file.txt”,”a+”); | $success=open(FILE,”>>+file.txt”); |
| fwrite($fd,$stuff); | print FILE $stuff; |
| $stuff=fgets($fd,1024); | $stuff = <FILE>; |
| fclose($fd); | close(FILE); |
| $stuff = file(“junk.txt”); | open(FILE,”<junk.txt”); $stuff=(<FILE>); |
| Expresiones regulares – Regular expressions | |
| if (ereg(‘[ \t]‘,$thing)) { … } | if ($thing =~ /[ \t]/) { … } |
| $thing=ereg_replace(“[ \t]+”,’:',$thing); | $thing =~ s/[ \t]+/:/g; |
| $parts=split(‘:’,$thing); | @parts=split(/:/,$thing); |
| Funciones – Functions | |
| function foo() { echo “hi\n”; } | sub foo { print “hi\n”; } |
| explicit argument list | no argument list, ever |
| functions take a fixed number of arguments | function arguments in special array @_ |
| function foo($thing) { echo $thing; } | sub foo { my $thing = shift @_; print $thing; } |
| Llamado por referencia – call by reference | |
| can make reference arguments by using & | @_ consists of references. |
| function bar(&$thing) { $thing=1; } | sub bar { $_[0]=1; } |
| Objetos | |
| Objetos son arreglos o de tipo especial | Objetos son un tipo especialobjects have a special type (referencia) |
| $t = array(array(1,2),array(3,4,5)); | $t = [[1,2],[3,4,5]]; |
| $t[0][1] | $t->[0]->[1] |
| CGI | |
| primary function; built in | special extension |
| CGI variable ‘thing’ is $thing | $cgi=new CGI; $thing=$cgi->param(‘thing’); |
| Variable de ambiente $SCRIPT_NAME available | Script name is $ENV{‘SCRIPT_NAME’}; |
| SI, Automáticamente imprime encabezados HTTP,
esto sucede al ejecutar dentro del script |
Se debe especificar en cada script el encabezado HTTP
|
| General | |
| designed for the specific task of CGI | evolved from system administration into CGI |
| one way to accomplish any one thing | many confusing shorthands |
| must specify all parameters explicitly | anything that can be inferred can be left out |
| Cualquier llamado a funciones de la misma manera en cualquier contexto | Algunas llados a funciones cambian dependiendo del contexto |
| Programas fáciles para leer | Dificultar para entender las acciones implícitas |
This post is a copy of the following link and is just for supply the same information in other language.
http://www.alternateinterior.com/2006/10/php-and-perl-comparison.html
http://comp20.cs.tufts.edu/comp/20/notes/php_perl.php
Related posts:
No Comments »
RSS feed for comments on this post. TrackBack URL
